2014-03-29 86 views
0

我寫了一個程序來查找重複的2D矢量C++。我只檢查了一個字段,即名稱字段。這是限制重複輸入的有效方法嗎?要在2D向量中找到重複向量C++

typedef vector<vector<string>> table_t; 

    table_t myvec = {{"DINU","56","66","45","78","76"}, 
        {"HARI","56","78","54","85","62"}, 
        {"JAMES","65","34","54","64","98"}, 
        {"HEARTLY","76","76","87","34","76"}, 
        {"PETER","67","78","87","34","53"}}; 
int main(int argc, char *argv[]) 
{ 
    if(argc!=7) 
    { 
     cout<<"Enter 7 Fields"<<endl; 
     exit(0); 
    } 

    string name = argv[1]; 
    upperto(name); 

    for(size_t i=0;i<myvec.size();i++) 
    { 
     if(name == myvec[i][0]) 
     { 
      id++; 
     } 
    } 

    if(id==0) 
    { 
     myvec.push_back({name,argv[2],argv[3],argv[4],argv[5],argv[6]}); 
    } 
    else 
    { 
     cout<<'\n'<<"Already Exist"<<endl; 
    } 

} 
+0

你知道'的std ::向量<>'本來['運營商=='](HTTP://en.cppreference。 com/w/cpp/container/vector/operator_cmp),對吧? – WhozCraig

+1

爲什麼沒有std :: map >? –

+0

我想限制使用特定記錄的重複。我不希望整個記錄限制重複。 – user3475204

回答

0

你的方法沒問題,但如果你想使它更有效率,你可以使用哈希。它會在不變的時間給你結果。

+0

這應該是評論,絕對不是答案 – Shravan40

+0

他要求使它高效,所以我認爲這可以成爲答案 –

1

剛剛創建輸入向量和比較全矢量:

vector<string> input; 

for (int i = 0; i < argc; i++) 
    input.push_back(arg[i]); 

for(size_t i = 0; i < myvec.size(); i++) { 
    if(input == myvec[i]) 
     // check here 
}