在向量上使用equal(),find(); >

发布于 2024-09-25 07:17:45 字数 1075 浏览 4 评论 0 原文

这是一件非常简单的事情,但我一直在绞尽脑汁试图理解。我正在尝试比较 vector 的元素。 > vec 与complex num 来检查 num 是否已存在于 vec 上。如果存在,则不会添加。我尝试使用 equal() 和算法,但没有成功。有人知道快速的方法吗?

编辑2:我尝试对复数执行此操作作为简化,因为我还需要对结构执行相同的操作:

struct thing{
 int i;
 int j;
 complex <double> pos;
}typedef t_thing;

complex <double> new_num(2.0,2.0);
t_thing will_insert;
will_insert.i = 1;
will_insert.j = 1;
will_insert.pos = new_num;
vector<t_thing> vec_thing;
if(! (find(vec_thing.begin(),vec_thing.end(),will_insert) == vec_thing.end())){
  vec_thing.push_back(will_insert);
}else { 
 cout<<"element already on vec_thing"<<endl;
}

编辑3:我已经重载运算符 ==,但 find 不能使用它:

: error: no matching function for call to ‘find(__gnu_cxx::__normal_iterator<thing*, std::vector<thing, std::allocator<thing> > >, __gnu_cxx::__normal_iterator<thing*, std::vector<thing, std::allocator<thing> > >, t_thing&)’

This is a pretty straightforward thing, but I've been bashing my head trying to understand. I'm trying to compare the elements of a vector<complex <double> > vec with a complex <double> num to check if num already exists on vec. If it does, it is not added. I tried to use the equal() and algorithm, with no success. Does anybody knows a fast way to do that?

EDIT2 : I'm trying to do that for complex numbers as a simplification, as I also need to perform the same operation on a struct:

struct thing{
 int i;
 int j;
 complex <double> pos;
}typedef t_thing;

complex <double> new_num(2.0,2.0);
t_thing will_insert;
will_insert.i = 1;
will_insert.j = 1;
will_insert.pos = new_num;
vector<t_thing> vec_thing;
if(! (find(vec_thing.begin(),vec_thing.end(),will_insert) == vec_thing.end())){
  vec_thing.push_back(will_insert);
}else { 
 cout<<"element already on vec_thing"<<endl;
}

EDIT 3: I've overloaded the operator ==, but find cannot work with that:

: error: no matching function for call to ‘find(__gnu_cxx::__normal_iterator<thing*, std::vector<thing, std::allocator<thing> > >, __gnu_cxx::__normal_iterator<thing*, std::vector<thing, std::allocator<thing> > >, t_thing&)’

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

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

发布评论

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

评论(1

相对绾红妆 2024-10-02 07:17:45

std::equal 算法用于比较 2 个迭代器范围。因此,您可以使用它来比较,例如,两个向量,看看两个向量是否包含相同的元素。

在您的情况下,您只需要检查向量中是否有单个元素,您可以使用 std::find

if (std::find(vec.begin(), vec.end(), std::complex<double>(1,1)) == vec.end()) {
   /* did not find element */
}
else { /* found the element */ }

但请注意,std::vector 不是特别适合这样的查找算法,因为每次查找都会带来 O(N) 复杂度。您可能需要考虑使用 std::set,这样您就可以获得对数的查找复杂性,并自动确保没有任何重复元素。

The std::equal algorithm is used to compare 2 iterator ranges. So you would use it to compare, for example, 2 vectors to see if both vectors contain the same elements.

In your case, where you only need to check if a single element is inside the vector, you can just use std::find

if (std::find(vec.begin(), vec.end(), std::complex<double>(1,1)) == vec.end()) {
   /* did not find element */
}
else { /* found the element */ }

Note however that std::vector is not particularly well suited for lookup algorithms like this, since each lookup gives you O(N) complexity. You might want to think about using std::set, so you get logarithmic complexity for lookup, and automatic assurance that you don't have any duplicate elements.

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