c ++ find

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

下面的代码有什么问题?如果结构体的第一个memebers == 0,它应该在结构体列表中找到一个元素。编译器会提示lambda参数不是Predicate类型。

#include< iostream> #include< stdint.h> #include< fstream> #include< list> #include< algorithm> struct S { int S1; int S2; }; using namespace std; int main() { list< S> l; S s1; s1.S1 = 0; s1.S2 = 0; S s2; s2.S1 = 1; s2.S2 = 1; l.push_back(s2); l.push_back(s1); list< S> :: iterator it = find_if(l.begin(),l.end(),[](S s){return s.S1 == 0;});在VS2012上,代码可以正常工作,但是这个代码可以正常工作,只有一个建议,通过引用传递对象而不是传递值:

list< S> :: iterator it = find_if .begin(),l.end(),[](const S& s){return s.S1 == 0;});

What is wrong with the code below? It is supposed to find an element in the list of structs if the first of the struct's memebers == 0. The compiler complains about the lambda argument not being of type Predicate.

#include <iostream> #include <stdint.h> #include <fstream> #include <list> #include <algorithm> struct S { int S1; int S2; }; using namespace std; int main() { list<S> l; S s1; s1.S1 = 0; s1.S2 = 0; S s2; s2.S1 = 1; s2.S2 = 1; l.push_back(s2); l.push_back(s1); list<S>::iterator it = find_if(l.begin(), l.end(), [] (S s) { return s.S1 == 0; } ); }

解决方案

Code works fine on VS2012, just one recommendation, pass object by reference instead of pass by value:

list<S>::iterator it = find_if(l.begin(), l.end(), [] (const S& s) { return s.S1 == 0; } );

更多推荐

c ++ find

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

发布评论

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

>www.elefans.com

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