Using "unique ()" on a vector of vectors in C ++

Hope this is not a duplicate question, but if so, feel free to point me in the right direction.

I have one vector<vector<int> >.

Is it possible to use unique()on this? Sort of:

vector<vector<int> > myvec;
//blah blah do something to myvec
vector<vector<int> >::interator it = unique(myvec.begin(), myvec.end());

Will the range myvec.begin()up be itunique?

+3
source share
2 answers

Yes, while your vector is sorted. See unique () STL for more details .

Here is a usage example:

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

using namespace std;

int main ()
{
    vector< vector<string> > v;

    v.push_back (vector<string> ());
    v.back ().push_back ("A");

    v.push_back (vector<string> ());
    v.back ().push_back ("A");

    v.push_back (vector<string> ());
    v.back ().push_back ("B");

    for (vector< vector<string> >::iterator it = v.begin (); it != v.end (); ++it)
        for (vector<string>::iterator j = it->begin (), j_end = it->end (); j != j_end; ++j)
            cout << *j << endl;

    cout << "-------" << endl;

    vector< vector<string> >::iterator new_end = unique (v.begin (), v.end ());
    for (vector< vector<string> >::iterator it = v.begin (); it != new_end; ++it)
        for (vector<string>::iterator j = it->begin (), j_end = it->end (); j != j_end; ++j)
            cout << *j << endl;
}
+5
source

It seems like it should work - it will call the == operator on two objects vector<int>to make it work.

, , , .

: http://www.sgi.com/tech/stl/unique.html

+2

Source: https://habr.com/ru/post/1766075/


All Articles