简体   繁体   中英

How to check if a c++ vector already contains and element with a specific value (in this case a struct)?

I need to check if an element with a specific value already exists in a c++ vector before adding a new one (to avoid duplicates).

I checked everything and this solution (simplified code below) seems to be the most efficient, if only it did work.

The problem I'm having is specifically with this line:

"if(std::find(positions.begin(), positions.end(), pos) != positions.end())",

which gives me compile errors inside the library, saying "Invalid operands to binary expression ('position' and 'const position')".

I know I am a bit of a newbie in c++ and I am sorry if this is e bit of a dumb question, but can anyone tell me what am I doing wrong?

Is it the fact that the value is a struct? Does it have anything to do with values vs. pointers/references (I suspect it does)?

struct position
{
    int column;
    int row;
};


int main ()
{
    std::vector<position> positions = {{0,0}, {0,1}, {0,2}, {1,0}, {1,1}, {1,2}};

    position pos = {2,1};

    if(std::find(positions.begin(), positions.end(), pos) != positions.end())
    {

        positions.push_back(pos);
    }
    else
    {
        std::cout << "Value is already present" << std::endl;
    }

    return 0;
}

I am clueless and really stuck with this, which is preventing me to advance in my project.

Does any one have an idea of what am I doing wrong or how should I do this?

Many thanks!

Two things are wrong here (there may be others but these are the relevant ones).

First, there is no equality operator in your structure that would allow find to compare items. This can be added with something like:

struct position {
    int column;
    int row;
    bool operator==(const position &other) const {
        return column == other.column && row == other.row;
    }
};

Second, the sense of your comparison is wrong. The find will return end if the item is not found, so your if section should be:

if (std::find(positions.begin(), positions.end(), pos) == positions.end()) {
    positions.push_back(pos);
} else {
    std::cout << "Value is already present" << std::endl;
}

For completeness, here's a full program that shows what happens when you try to add a non-existant element three times:

#include <iostream>
#include <vector>
#include <algorithm>

struct position {
    int column;
    int row;
    bool operator==(const position &other) const {
        return column == other.column && row == other.row;
    }
};

int main () {
    std::vector<position> vec = {};

    position pos = {2,1};
    for (int i = 0; i < 3; ++i) {
        if (std::find(vec.begin(), vec.end(), pos) == vec.end()) {
            std::cout << "Adding value" << std::endl;
            vec.push_back(pos);
        } else {
            std::cout << "Value is already present" << std::endl;
        }
    }

    return 0;
}

From the ouput, you can see only the first one actually inserts:

Adding value
Value is already present
Value is already present

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM