用谓词使用std :: find

编程入门 行业动态 更新时间:2024-10-11 09:21:18
本文介绍了用谓词使用std :: find的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用 std :: find 函数和一个谓词(不确定是否使用正确的单词)。这里是代码

#include< iostream> #include< vector> #include< algorithm> 使用namespace std; class foo { public: typedef pair< int,vector< int> >办法; typedef pair< int,int>指数; typedef pair< index,vector< way> >条目; vector<条目>表; void bar() { vector< int> V1; v1.push_back(1); v1.push_back(2); way w = make_pair(1,v1); vector< way> V2; v2.push_back(w); index id = make_pair(10,20); entry en = make_pair(id,v2); table.push_back(en); void insert() { index new_id = make_pair(10,20); if(table.begin(),table.end(),new_id)!= table.end()){ //表中匹配的索引 //然后I将推回一个新的对(方式) //到条目的第二部分} } }; int main() { foo f; f.bar(); f.insert(); 返回0; code $

正如你所看到的, find() code>应根据每个条目中的第一个元素搜索表。现在,它表示 == 不会被重载以比较对。

解决方案

您想 std :: find_if :

... $ b $如果(find_if(table.begin(),table.end(),[& new_id](const entry& arg){ return arg.first == new_id;})!= ...)

编辑:如果你没有C ++ 11因此不需要lambda表达式),你必须创建一个自定义函子(函数或函数对象)来比较 entry :: first 和搜索到的索引:

struct index_equal:std :: unary_function< entry,bool> { index_equal(const index& idx):idx_(idx){} bool operator()(const entry& arg)const {return arg.first == idx_; } const index& idx_; }; $ b $ ... if(find_if(table.begin(),table.end(),index_equal(new_id))!= ...)

I want to use std::find function along with a predicate (not sure if I use the correct word). Here is the code

#include <iostream> #include <vector> #include <algorithm> using namespace std; class foo { public: typedef pair< int, vector<int> > way; typedef pair< int, int > index; typedef pair< index, vector<way> > entry; vector< entry > table; void bar() { vector<int> v1; v1.push_back(1); v1.push_back(2); way w = make_pair( 1, v1 ); vector<way> v2; v2.push_back(w); index id = make_pair( 10, 20 ); entry en = make_pair( id, v2 ); table.push_back( en ); } void insert() { index new_id = make_pair( 10, 20 ); if ( find(table.begin(), table.end(), new_id) != table.end() ) { // index matched in the table // then I will push back a new pair (way) // to the second part of the entry } } }; int main() { foo f; f.bar(); f.insert(); return 0; }

As you can see, find() should search the table based on the first element in each entry. Right now, it says that == is not overloaded to compare a pair.

解决方案

You want std::find_if:

... if(find_if(table.begin(), table.end(), [&new_id](const entry &arg) { return arg.first == new_id; }) != ...)

EDIT: If you don't have C++11 (and therefore no lambdas), you have to create a custom functor (function or function object) to do the comparison of entry::first with the searched index:

struct index_equal : std::unary_function<entry,bool> { index_equal(const index &idx) : idx_(idx) {} bool operator()(const entry &arg) const { return arg.first == idx_; } const index &idx_; }; ... if(find_if(table.begin(), table.end(), index_equal(new_id)) != ...)

EDIT: Since an index is just a pair of ints, you may also just capture it by value than const reference, to keep the code clearer and more concise, but it doesn't really matter either.

更多推荐

用谓词使用std :: find

本文发布于:2023-11-30 14:36:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:谓词   std   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!