How there is Multiple map elements in single index of vector of map

50 Views Asked by At

In my main function, I use the variable t of type trie, which is a vector of maps:

#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <utility>

using std::map;
using std::vector;
using std::string;
using std::pair;
using namespace std;

typedef map<char, int> edges;
typedef vector<edges> trie;

trie build_trie(vector<string> &patterns) {
    trie t;
    // write your code here
    for (int i = 0; i < patterns.size(); i++) {
        int x = 0;
        for (int j = 0; j < patterns[i].size(); j++) {
            bool match_found = false;
            if (x < t.size()) {
                for (const auto &k : t[x]) {
                    if (k.first == patterns[i][j]) {
                        x = k.second;
                        match_found = true;
                        break;
                    }
                }
                if (!match_found) {
                    t[x].insert(pair<char, int>(patterns[i][j], t.size()));
                    x = t.size();
                }
            } else {
                map<char, int> m;
                m.insert(pair<char, int>(patterns[i][j], t.size() + 1));
                t.push_back(m);
                x = t.size();
            }
        }
        map<char, int> m;
        m.insert(pair<char, int>('$', -1));
        t.push_back(m);
    }
    return t;
}

int main() {
    size_t n;
    std::cin >> n;
    vector<string> patterns;
    for (size_t i = 0; i < n; i++) {
        string s;
        std::cin >> s;
        patterns.push_back(s);
    }

    trie t = build_trie(patterns);
    for (size_t i = 0; i < t.size(); ++i) {
        for (const auto &j : t[i]) {
            if (j.first != '$') {
                std::cout << i << "->" << j.second << ":" << j.first << "\n";
            }
        }
    }

    return 0;
}

On providing the following input

3 AT AG AC

while debugging the program, I see multiple key-value pairs at one single index of t:

Screenshot

Please, can anyone explain how that is possible.

0

There are 0 best solutions below