簡體   English   中英

如何在向量的2個數組中搜索共同元素,向量是地圖中的鍵?

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

我有一個映射,它使用大小為3的vector<int>作為鍵,並將字符串作為值。 該映射包含大約2000個從文件中解析的條目。 我需要檢查向量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;
}

我得到的錯誤是:

“后綴值不是數組,指針或向量。

我僅一個星期以來一直從事C ++工作,但我什么都不懂! :(

編輯:

我有4欄:

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

我必須檢查第2列中是否存在第1列中的每個數字。因此,在第1列的第1個元素中,第2列中存在4。現在,我取相應的第3列數字(25)並放置它在第20個元素下的26個字母的向量中。 20,因為我需要獲取相應字符串(wtwt)的最后一個字符。 T是第20個字母。

我必須對第1列中的元素執行此過程。

根據您的編輯,建議您將數據結構更改為以下形式:

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;
  }
}

第一列進入下一個,第三列進入索引。 第二列是地圖鍵。

建議您在定義地圖時也通過比較功能。

請看地圖的定義

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM