简体   繁体   中英

Subtract one vector from another

I organized two vectors of structures. Now I need to delete what is in chosen from points .

#include <StdAfx.h>;
#include <iostream>;
#include <vector>;

using namespace std;
struct SPoint
{
    int id;
    int X;
    int Y;
};

vector<SPoint> points;
vector<SPoint> chosen;

void print_vect(const vector<SPoint> & vect)
{
    for (int i = 0; i < vect.size(); ++i)
    {
        cout << vect[i].id << " (" << vect[i].X << "," << vect[i].Y << ")"<<endl;               
    }           

    cout << endl;   
}

int _tmain(int argc, _TCHAR* argv[])
{
    SPoint temp;
    for (int i = 0; i < 10; i++)
    {
        temp.id = i;
        temp.X = i;
        temp.Y = i;
        points.push_back(temp);
    }

    for (int i = 5; i < 10; i++)
    {
        temp.id = i;
        temp.X = i;
        temp.Y = i;
        chosen.push_back(temp);
    }

    cout << "Points:" << endl;
    print_vect(points);
    cout << endl << endl;

    cout << "Chosen:" << endl;
    print_vect(chosen);

    system("pause");

    return 0;
}

There seems to be set_difference function. But the debugger tells me that I don't have a '<' method. It tells something like this:

error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'SPoint

I study procedural programming in C++. And I don't know what to do with this method. And it seems to me that it is impossible to do anything here with "<".

Could you help me execute the subtraction?

Yes, you have guessed correctly. The std::set_difference function needs the < operator to function. It uses it to check equality as (!a

The comparison to check for equivalence of values, uses either
operator< for the first version, or comp for the second, in order to
test this; The value of an element, a, is equivalent to another one,
b, when (!a<b && !b<a) or (!comp(a,b) && !comp(b,a)).

All you would need to do is to add a function like below

bool operator<(const SPoint& p1, const SPoint&p2){
    return p1.id <p2.id;
}

Assuming your id field is a unique field. Now you will be able to use the std::set_difference function. This compares two SPoint variables by their id fields.

Note that BOTH ranges need to be sorted for it to work correctly.

You could use eg std::remove_if :

std::remove_if(std::begin(points), std::end(points), [](const SPoint& point) {
    // Try to find the point in the `chosen` collection
    auto result = std::find_if(std::begin(chosen), std::end(chosen),
        [](const SPoint& p) {
            return (p.id == point.id)
        });

    // Return `true` if the point was found in `chosen`
    return (result != std::end(chosen));
});

Note that I use C++11 lambda functions in the above code.

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