WikiGalaxy

Personalize

C++ Vectors

Introduction to Vectors

Vectors in C++ are dynamic arrays that can resize themselves automatically when an element is inserted or deleted. They are part of the Standard Template Library (STL).

Basic Operations

Vectors support various operations such as adding elements, removing elements, and accessing elements using indices.

    
#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers;
    numbers.push_back(10);
    numbers.push_back(20);
    numbers.push_back(30);
    
    for(int i = 0; i < numbers.size(); ++i) {
        std::cout << numbers[i] << " ";
    }
    return 0;
}
    
  

Console Output:

      

10 20 30

C++ Maps

Understanding Maps

Maps in C++ are associative containers that store elements in key-value pairs. They allow fast retrieval of values based on their keys.

Inserting Elements

Elements can be inserted into a map using the insert function or the subscript operator.

    
#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> ageMap;
    ageMap["Alice"] = 30;
    ageMap["Bob"] = 25;
    
    for(const auto &pair : ageMap) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }
    return 0;
}
    
  

Console Output:

      

Alice: 30 Bob: 25

C++ Sets

Introduction to Sets

Sets in C++ are containers that store unique elements following a specific order. They do not allow duplicate elements.

Basic Operations

Sets support operations like insertion, deletion, and searching for elements.

    
#include <iostream>
#include <set>

int main() {
    std::set<int> numberSet;
    numberSet.insert(10);
    numberSet.insert(20);
    numberSet.insert(10); // Duplicate
    
    for(int num : numberSet) {
        std::cout << num << " ";
    }
    return 0;
}
    
  

Console Output:

      

10 20

C++ Stacks

Understanding Stacks

Stacks in C++ are containers that follow the Last In First Out (LIFO) principle. They allow elements to be added and removed from only one end.

Basic Operations

Stacks support operations like push, pop, and top to manage elements.

    
#include <iostream>
#include <stack>

int main() {
    std::stack<int> numberStack;
    numberStack.push(10);
    numberStack.push(20);
    
    while(!numberStack.empty()) {
        std::cout << numberStack.top() << " ";
        numberStack.pop();
    }
    return 0;
}
    
  

Console Output:

      

20 10

C++ Queues

Introduction to Queues

Queues in C++ are containers that follow the First In First Out (FIFO) principle. They allow elements to be added at the back and removed from the front.

Basic Operations

Queues support operations like enqueue, dequeue, and front to manage elements.

    
#include <iostream>
#include <queue>

int main() {
    std::queue<int> numberQueue;
    numberQueue.push(10);
    numberQueue.push(20);
    
    while(!numberQueue.empty()) {
        std::cout << numberQueue.front() << " ";
        numberQueue.pop();
    }
    return 0;
}
    
  

Console Output:

      

10 20

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025