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

前端 未结 2 1593
情深已故
情深已故 2021-01-21 02:27

I hope this is not a duplicate question, but if it is, feel free to point me in the right direction.

I have a vector >.

Is i

2条回答
  •  天命终不由人
    2021-01-21 02:52

    Yes, as long as your vector is sorted. See unique () STL documentation for details.

    Here is an example of usage:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main ()
    {
        vector< vector > v;
    
        v.push_back (vector ());
        v.back ().push_back ("A");
    
        v.push_back (vector ());
        v.back ().push_back ("A");
    
        v.push_back (vector ());
        v.back ().push_back ("B");
    
        for (vector< vector >::iterator it = v.begin (); it != v.end (); ++it)
            for (vector::iterator j = it->begin (), j_end = it->end (); j != j_end; ++j)
                cout << *j << endl;
    
        cout << "-------" << endl;
    
        vector< vector >::iterator new_end = unique (v.begin (), v.end ());
        for (vector< vector >::iterator it = v.begin (); it != new_end; ++it)
            for (vector::iterator j = it->begin (), j_end = it->end (); j != j_end; ++j)
                cout << *j << endl;
    }
    

提交回复
热议问题