C++ 如何将向量转换为集合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20052674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:53:38  来源:igfitidea点击:

How to convert vector to set?

c++vectorset

提问by Marius

I have a vector, in which I save objects. I need to convert it to set. I have been reading about set, but I still have a couple of questions:

我有一个向量,我在其中保存对象。我需要将其转换为设置。我一直在阅读有关 set 的内容,但我仍然有几个问题:

How to correctly initialize it? Honestly, some tutorials say it is fine to initialize it like set<ObjectName> something. Others say that you need an iterator there too, like set<Iterator, ObjectName> something.

如何正确初始化它?老实说,一些教程说可以像set<ObjectName> something. 其他人说你也需要一个迭代器,比如set<Iterator, ObjectName> something.

How to insert them correctly. Again, is it enough to just write something.insert(object)and that's all?

如何正确插入它们。再一次,仅仅写就足够了something.insert(object)吗?

How to get specific object (for example object, which has name variable in it, which is equal to "ben") from set?

如何从集合中获取特定对象(例如对象,其中包含名称变量,等于“ben”)?

P.S. I have convert vector it self to be as a set (a.k.a. I have to use set rather then vector). Only set can be in my code.

PS 我已经将向量本身转换为一个集合(也就是我必须使用集合而不是向量)。只有 set 可以在我的代码中。

采纳答案by Beta

You haven't told us much about your objects, but suppose you have a class like this:

您还没有告诉我们很多关于您的对象的信息,但假设您有一个这样的类:

class Thing
{
public:
  int n;
  double x;
  string name;
};

You want to put some Things into a set, so you try this:

你想把一些东西放到一个集合中,所以你试试这个:

Thing A;
set<Thing> S;
S.insert(A);

This fails, because sets are sorted, and there's no way to sort Things, because there's no way to compare two of them. You must provide either an operator<:

这失败了,因为集合已排序,并且无法对事物进行排序,因为无法比较其中的两个。您必须提供operator<

class Thing
{
public:
  int n;
  double x;
  string name;

  bool operator<(const Thing &Other) const;
};

bool Thing::operator<(const Thing &Other) const
{
  return(Other.n<n);
}

...
set<Thing> S;

or a comparison function object:

比较函数对象

class Thing
{
public:
  int n;
  double x;
  string name;
};

struct ltThing
{
  bool operator()(const Thing &T1, const Thing &T2) const
  {
    return(T1.x < T2.x);
  }
};

...
set<Thing, ltThing> S;

To find the Thing whose name is "ben", you can iterate over the set, but it would really help if you told us more specifically what you want to do.

要找到名称为“ben”的 Thing,您可以遍历该集合,但如果您更具体地告诉我们您想要做什么,那真的很有帮助。

回答by masoud

Suppose you have a vector of strings, to convert it to a set you can:

假设您有一个字符串向量,要将其转换为一个集合,您可以:

std::vector<std::string> v;

std::set<std::string> s(v.begin(), v.end());

For other types, you must have operator<defined.

对于其他类型,您必须已operator<定义。

回答by David

All of the answers so far have copied a vectorto a set. Since you asked to 'convert' a vectorto a set, I'll show a more optimized method which moves each element into a setinstead of copying each element:

到目前为止,所有答案都已将 a 复制vector到 a set。由于您要求将 a '转换'vector为 a set,我将展示一种更优化的方法,该方法将每个元素移动到 aset而不是复制每个元素:

std::vector<T> v = /*...*/;

std::set<T> s(std::make_move_iterator(v.begin()),
              std::make_move_iterator(v.end()));

Note, you need C++11 support for this.

请注意,您需要 C++11 支持。

回答by Ivaylo Strandjev

You can initialize a set using the objects in a vector in the following manner:

您可以通过以下方式使用向量中的对象初始化集合:

vector<T> a;
... some stuff ...
set<T> s(a.begin(), a.end());

This is the easy part. Now, you have to realize that in order to have elements stored in a set, you need to have bool operator<(const T&a, const T& b)operator overloaded. Also in a set you can have no more then one element with a given value acording to the operator definition. So in the set syou can not have two elements for which neither operator<(a,b)nor operator<(b,a)is true. As long as you know and realize that you should be good to go.

这是简单的部分。现在,您必须意识到为了将元素存储在集合中,您需要bool operator<(const T&a, const T& b)重载运算符。同样在一个集合中,根据运算符定义,您不能有多个具有给定值的元素。所以在集合中s你不能有两个既不是operator<(a,b)也不operator<(b,a)是真的元素。只要你知道并意识到你应该很好去。

回答by Zac Howland

If all you want to do is store the elements you already have in a vector, in a set:

如果您想要做的只是将您已有的元素存储在一个向量中,在一个集合中:

std::vector<int> vec;
// fill the vector
std::set<int> myset(vec.begin(), vec.end());

回答by Pete Becker

Creating a set is just like creating a vector. Where you have

创建一个集合就像创建一个向量。你在哪里

std::vector<int> my_vec;

(or some other type rather than int) replace it with

(或其他类型而不是int)将其替换为

std::set<int> my_set;

To add elements to the set, use insert:

要将元素添加到集合中,请使用insert

my_set.insert(3);
my_set.insert(2);
my_set.insert(1);

回答by Johan

How to correctly initialize it?

如何正确初始化它?

std::set<YourType> set;

The only condition is that YourTypemust have bool operator<(const YourType&) constand by copyable (default constructor + assignment operator). For std::vectorcopyable is enough.

唯一的条件是YourType必须具有bool operator<(const YourType&) const和 by 可复制(默认构造函数 + 赋值运算符)。对于可std::vector复制就足够了。

How to insert them correctly.

如何正确插入它们。

set.insert(my_elem);

How to get specific object (for example object, which has name variable in it, which is equal to "ben") from set?

如何从集合中获取特定对象(例如对象,其中包含名称变量,等于“ben”)?

That's maybe the point. A set is just a bunch of object, if you can just check that an object is inside or iterate throught the whole set.

这也许就是重点。一个集合只是一堆对象,如果你可以检查一个对象是否在里面或遍历整个集合。