STL Containers in OOP

Understanding Standard Template Library Containers in C++

What are STL Containers?

STL Containers are data structures that store collections of objects. They are part of the Standard Template Library (STL) and provide efficient implementations of common data structures. Each container has its own characteristics and is optimized for specific use cases.

STL Containers क्या हैं? (हिंदी में)

STL Containers वो data structures हैं जो objects के collections को store करते हैं। यह Standard Template Library (STL) का part हैं और common data structures के efficient implementations provide करते हैं। हर container की अपनी characteristics होती हैं और specific use cases के लिए optimize किया गया होता है।

Key Points:

  • Data Storage: Store collections of objects
  • Memory Management: Handle memory allocation
  • Access Methods: Provide ways to access elements
  • Performance: Optimized for specific operations

Types of Containers

Sequence Containers

vector, list, deque, array, forward_list

Associative Containers

set, multiset, map, multimap

Unordered Containers

unordered_set, unordered_multiset, unordered_map, unordered_multimap

Container Adapters

stack, queue, priority_queue

Container Rules

Rule Description
Element Requirements Must be copyable/movable
Memory Management Automatic allocation/deallocation
Iterator Support Must provide iterators
Exception Safety Must handle exceptions properly

Simple Example

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>

// Function demonstrating STL containers
void containerExample() {
    std::cout << "\nSTL Container Example:" << std::endl;
    
    // Vector (Sequence Container)
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    std::cout << "Vector elements: ";
    for (const auto& num : numbers) {
        std::cout << num << " ";
    }
    
    // Map (Associative Container)
    std::map<std::string, int> scores;
    scores["Alice"] = 95;
    scores["Bob"] = 87;
    scores["Charlie"] = 92;
    
    std::cout << "\n\nMap contents:" << std::endl;
    for (const auto& [name, score] : scores) {
        std::cout << name << ": " << score << std::endl;
    }
    
    // Set (Associative Container)
    std::set<int> uniqueNumbers = {3, 1, 4, 1, 5, 9, 2, 6};
    std::cout << "\nSet elements (unique): ";
    for (const auto& num : uniqueNumbers) {
        std::cout << num << " ";
    }
    
    // Queue (Container Adapter)
    std::queue<std::string> tasks;
    tasks.push("Task 1");
    tasks.push("Task 2");
    tasks.push("Task 3");
    
    std::cout << "\n\nQueue contents:" << std::endl;
    while (!tasks.empty()) {
        std::cout << tasks.front() << std::endl;
        tasks.pop();
    }
    
    // Vector operations
    numbers.push_back(6);
    numbers.insert(numbers.begin() + 2, 10);
    
    std::cout << "\nModified vector: ";
    for (const auto& num : numbers) {
        std::cout << num << " ";
    }
    
    // Map operations
    scores["David"] = 88;
    scores.erase("Bob");
    
    std::cout << "\n\nModified map:" << std::endl;
    for (const auto& [name, score] : scores) {
        std::cout << name << ": " << score << std::endl;
    }
    
    // Set operations
    uniqueNumbers.insert(7);
    uniqueNumbers.erase(3);
    
    std::cout << "\nModified set: ";
    for (const auto& num : uniqueNumbers) {
        std::cout << num << " ";
    }
}

int main() {
    containerExample();
    return 0;
}

उदाहरण की व्याख्या (Example Explanation)

इस उदाहरण में STL containers के different aspects को demonstrate किया गया है:

  • Sequence Container: vector का use
  • Associative Container: map और set का use
  • Container Adapter: queue का use
  • Container Operations: insertion, deletion, और traversal
  • Range-based For: Modern C++ syntax का use
  • Structured Bindings: map iteration में use

Benefits of STL Containers

STL Containers के फायदे

  1. Efficiency
    • Optimized implementations
    • Better performance than manual code
  2. Safety
    • Automatic memory management
    • Exception safety guarantees
  3. Flexibility
    • Different container types
    • Custom element types

Best Practices

  • Choose appropriate container for the task
  • Consider container performance characteristics
  • Use container-specific operations
  • Be aware of iterator invalidation
  • Use range-based for loops when possible