在2D Vector C ++中查找副本

时间:2014-03-29 07:38:41

标签: c++ stl duplicates stdvector

我编写了一个程序来查找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;
    }

}

2 个答案:

答案 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
}

答案 1 :(得分:0)

您的方法没问题,但如果您想提高效率,可以使用散列。它会在恒定的时间内给你结果。