c++学习笔记(七)- lambda表达式 迭代器 算法
2024-09-14 14:41:00
关于lambda表达式:
刷题的时候遇到一句代码不懂:
char ch = *it;
auto it2 = find_if(it, b.end(), [ch](char x){ return x != ch; });
find_if:当pred为真时,返回迭代器的值;若所有元素都不满足,返回最后一个元素位置。
这里的[ch](char x){ return x != ch; }是套用lambda表达式。
这里用的是第三种,捕获ch,参数是x,若x!=ch返回1,find_if的pred为真,停止迭代,返回此时的it。
“Lambda就是一个匿名函数,适用于要实现某个很简单函数,而且我们只会需要调用它一次的情况”----知乎,路人甲。
此外pred还可以用仿函数实现,参考这里以及lambda和仿函数,不知不觉又铺开好大一片,先保存,以后用到的时候再研究了。
关于迭代器:
比如for循环的终止条件,要用it!=xx.end(),it<=xx.end()是不行的。
上述find_if当然还是可以自己for循环查找一个,但没有find_if用起来方便,自己写for需要判断好边界条件,find_if是如果不满足pred直接返回最后一个元素索引。
-----2018.01.05-----
今天写了一段程序,用迭代器插入vector:
vector<int>::iterator it = myVec1.begin();
cout << *it << endl;
myVec1.insert(it, , );
cout << *it << endl;
程序刚运行就崩溃了(看和做的区别,道理你都懂,一用就完蛋),仔细看C++ reference关于insert的部分:
正确的写法应该是:
vector<int>::iterator it = myVec1.begin();
cout << *it << endl;
myVec1.insert(it, , );
it = myVec1.begin();
cout << *it << endl;
最新文章
- CSUOJ_1002
- mac下有道词典用不了
- 字典树(Trie Tree)
- 【Win10】UAP/UWP (通用程序) 开发初体验(1) 之 开发准备
- 深入理解Sqlserver索引
- 我与python3擦肩而过(一)—— Dict与collections.OrderredDict邂逅
- EMVTag系列1《数据分组》
- linux编译注解
- sql 中条件in参数问题
- java.lang.IllegalStateException: Required view &#39;text1&#39; with ID 2131492943 for field &#39;mText&#39; was not found. If this view is optional add &#39;@Nullable&#39; annotation
- Codeforces 204A Little Elephant and Interval
- ST HW2 fault &; error &; failure
- Educational Codeforces Round 22.B 暴力
- Java设计模式之包装模式
- Jmeter的安装和启动时出现unable to access jarfile apachejmeter.jar error value=1错误处理
- Django的Models(二)映射关系
- scala 2.11.x/spec/03-types.md
- [PHP] 解决人人商城收银台不能上传图片问题
- Jenkins自动化构建配置
- Flask离线文档 --技术文档
热门文章
- Eclipse中通过Hibernate Tools插件实现从数据库逆向生成Hibernate带注解的实体类
- 【PyQt5-Qt Designer】按钮系列
- tomcat停止和启动脚本
- 1、 LwIP协议栈规范翻译——简介
- 集成xxl-job分布式任务调度平台
- python练习题-day6
- git出现warning: LF will be replaced by CRLF的解决方法
- TCP、UDP和HTTP详解
- 解决npm ERR! Unexpected end of JSON input while parsing near的方法汇总
- internal table operation