使用"独特的()"在C语言载体的载体++ [英] Using "unique()" on a vector of vectors in C++

查看:178
本文介绍了使用"独特的()"在C语言载体的载体++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

这篇关于使用&QUOT;独特的()&QUOT;在C语言载体的载体++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆