使用“unique()”在 C++ 中的向量向量上

发布于 2024-09-24 16:43:46 字数 396 浏览 1 评论 0 原文

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

我有一个 vector; >

是否可以对此使用 unique() ?例如:

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

myvec.begin()it 的范围是否唯一?

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<vector<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());

Would the range myvec.begin() to it be unique?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情定在深秋 2024-10-01 16:43:46

是的,只要你的向量是排序的。请参阅unique () STL 文档了解详细信息。

这是一个用法示例:

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

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

Here is an example of usage:

#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;
}
爱她像谁 2024-10-01 16:43:46

看起来它应该可以工作——它会在两个 vector 对象上调用 == 运算符,这样就应该可以工作。

请注意,该运算符适用于重复项组,因此如果您的重复项尚未分组,您可能必须对外部向量进行排序。

参考:http://www.sgi.com/tech/stl/unique.html< /a>

Looks like it should work -- it would call the == operator on two vector<int> objects so that should work.

Note that the operator works on groups of duplicates so you may have to sort your outer vector if your duplicates are not grouped already.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文