使用"独特的()"在C语言载体的载体++载体、独特、语言、QUOT

2023-09-11 02:35:12 作者:软喵酱メ

我希望这不是一个重复的问题,但如果是,请随时点我在正确的方向。

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

我有一个矢量<矢量< INT> >

是否可以使用独特的()对此怎么看?是这样的:

Is it possible to use unique() on this? Something like:

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

请问范围 myvec.begin()是唯一的?

推荐答案

是的,只要你的载体进行排序。请参见独特的()的 STL 文档的详细信息。

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

下面是使用的例子:

#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;
}
 
精彩推荐