Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Vector of Maps in C++ with Examples

  • Last Updated : 22 Jun, 2021

Map in STL: Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values.

Vector in STL: Vector is the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Vector of Maps in STL: Vector of maps can be used to design complex and efficient data structures.


Vector of Ordered Map:
vector<map<datatype, datatype> > VM;

Vector of Unordered map:
vector<unordered_map<datatype, datatype> > VUM;

Given a string. The task is to find the frequency of characters up to each index.


// C++ program to demonstrate the use
// of vector of maps
#include <bits/stdc++.h>
using namespace std;
// Function to count frequency
// up to each index
void findOccurences(string s)
    // Vector of map
    vector<map<char, int> > mp(s.length());
    // Traverse the string s
    for (int i = 0; i < s.length(); i++) {
        // Update the frequency
        for (int j = 0; j <= i; j++) {
    // Print the vector of map
    for (int i = 0; i < s.length(); i++) {
        cout << "Frequency upto "
             << "position " << i + 1
             << endl;
        // Traverse the map
        for (auto x : mp[i])
            cout << x.first << "-"
                 << x.second << endl;
// Driver Code
int main()
    // Input string S
    string S = "geeks";
    // Function Call
    return 0;
Frequency upto position 1
Frequency upto position 2
Frequency upto position 3
Frequency upto position 4
Frequency upto position 5


My Personal Notes arrow_drop_up
Recommended Articles
Page :