C++ STL迭代器 -- 插入迭代器
来源:互联网 发布:163邮箱设置 imap端口 编辑:程序博客网 时间:2024/04/29 13:49
#include "stdafx.h"#include <iostream>#include <vector>#include <list>#include <algorithm>#include <iterator>/** 算法库对所有在容器上的操作有个承诺:决不修改容器的大小(不插入、不删除)。* 有了插入迭代器,既使得算法库可以通过迭代器对容器插入新的元素,又不违反这一承诺,即保持了设计上的一致性。*/using namespace std;int main(){ //c++插入迭代器 vector<int> vInt = { 1,2,3,4,5 }; list<int> listInt; //向后插入迭代器,内部调用push_back back_insert_iterator<decltype(listInt)> backIter(listInt); *backIter = 30; //copy算法中的 运用 copy(vInt.begin(), vInt.end(), back_insert_iterator<decltype(listInt)>(listInt)); for (auto iter = listInt.begin(); iter != listInt.end(); ++iter) { cout << *iter << " "; } cout << endl; //向前插入迭代器,内部调用push_front front_insert_iterator<decltype(listInt)> frontIter(listInt); *backIter = 30; //copy算法中的 运用 copy(vInt.begin(), vInt.end(), front_insert_iterator<decltype(listInt)>(listInt)); for (auto iter = listInt.begin(); iter != listInt.end(); ++iter) { cout << *iter << " "; } cout << endl; //在第三个元素的位置插入元素 auto iter = listInt.begin(); int pos = 3; //获取位置 while (--pos) { ++iter; } //获取插入迭代器 auto iIter = insert_iterator<decltype(listInt)>(listInt,iter); *iIter = 35; for (auto iter = listInt.begin(); iter != listInt.end(); ++iter) { cout << *iter << " "; } cout << endl; return 0;}
阅读全文
0 0
- STL 之插入迭代器
- C++ STL迭代器 -- 插入迭代器
- C++STL之迭代器
- C++STL 之 迭代器
- [C++] STL迭代器失效
- c++STL的迭代器
- C++STL之迭代器
- C++STL 迭代器
- C++STL之迭代器
- 【stl】copy读入文件|插入迭代器
- C++STL之迭代器2
- C++ STL标准模板库中几种插入迭代器的用法
- STL之--插入迭代器(back_inserter,inserter,front_inserter的区别)
- STL 迭代器删除,插入元素发生的事情
- STL(二十六)插入/反向/存储迭代器
- STL之--插入迭代器(back_inserter,inserter,front_inserter的区别)
- C++ STL 迭代器的简单模拟
- 一步一步认识C++STL中的迭代器
- 论文阅读:《Natural Image Matting Using Deep CNN》ECCV 2016
- Lesson5 接口2
- ubuntu16.04 安装 Caffe
- 读取文件的编译乱码问题
- 求图像周长问题
- C++ STL迭代器 -- 插入迭代器
- 上机练习题——异常处理1
- java线程安全之synchronized锁重入及出现异常锁自动释放(五)
- 为什么创业者不怎么宣传他们的产品?
- 【greenplum】greenplum 常用数据库管理语句,sql工具
- 1129. Recommendation System (25)
- DoTWeen使用方法
- Laravel入门实战之一(打造一个PHP REPL)
- Tensorflow框架下识别手写字神经网络代码