snazziii
snazziii

Reputation: 491

How to search for common elements in 2 arrays of a vector which is a key in a map?

I have a map which takes in a vector<int> of size 3 as the key and a string as value. This map contains about 2000 entries which are parsed from a file. I need to check if each element in vector 1 is present in vector 2.

int index;
map<vector<int>,string>::iterator i2;

for ( i2=my_map.begin() ; i2 != my_map.end(); i2++)
{
    int id_cnt = (*i2).first.at(0);
    int prev_cnt = (*i2).first.at(1);

    for (index=0; index < my_map.size(); index++)
    {
        if (prev_cnt==id_cnt[index])                     //error
        {
            //code to do something
        }
    }

    cout << "hi" <<endl;
    cout << (*i2).second << endl;
}

The error i get is:

"Susbscripted value is not an array, pointer or vector.

I've been working on C++ only since a week and I don't understand anything! :(

Edit:

I have 4 columns:

4 6 65 gdsg
6 1 64 sggg
1 2 34 wetw
7 4 25 wtwt
8 5 25 heyq
5 7 23 fheh
2 5 12 fetd

I have to check if each number from the 1st column is present in column 2. So, in the case of the 1st element of column 1: 4 is present in column 2. Now I take the corresponding 3rd column number (25) and place it in a vector of 26 alphabets under the 20th element. 20th because I need to get the last character of the corresponding string (wtwt). T is the 20th letter.

I have to do this process for elements in column 1.

Upvotes: 0

Views: 1165

Answers (2)

Fozi
Fozi

Reputation: 5135

Based on your edit I suggest you change your data structure to something like this:

struct Entry {
  int next;
  int index;
  string text;
};

typedef map<int, Entry> MyMap;

for(MyMap::iterator i = my_map.begin(); i < my_map.end(); ++i) {
  MyMap::iterator next = my_map.find(i->second.next);
  if(next == my_map.end()) {
    // not found!
    break;
  }
}

Your first column goes in next, your third column goes in index. The second column is the map key.

Upvotes: 1

VThapa
VThapa

Reputation: 9

Would suggest you to pass the comparison function as well when defining the map.

Please look at the definition of map

http://www.cplusplus.com/reference/stl/map/map/

Upvotes: 0

Related Questions