vendredi 22 juillet 2016

How to copy last element of vector

I have a vector of set<pair<string, int>, comp>, which uses a custom comparator:

struct comp{
    bool operator()(const pair<string,int>& a, const pair<string,int>& b) const{
        return a.second > b.second || (a.second == b.second && a.first < b.first);
    }   
};

It sorts the pairs first by larger integer, and alphabetical order when integer is the same.

Now I tried to copy the last element of the vector v.back() to target(a new object, not reference) :

vector<set<pair<string,int>, comp>> v;
v.push_back(...);
v.push_back(...);
...
target = new set<pair<string,int>, comp>(v.back());// <--- compiler throws error on this line

This is the compiler error message: (Simplified, both type are the same except second one is a pointer)

no match for operator= (operand types are set<...> and set<...>* )

I am new to C++, if this is not the correct way to copy last vector element to another variable, what is the correct way to do it? Thanks!

Aucun commentaire:

Enregistrer un commentaire