C++ back_inserter
2015-11-16 23:05
381 查看
#include <iostream> #include <iterator> #include <vector> #include <algorithm> // g++ 4.9.2 x86_64 // g++ *.cpp -std=c++11 int main() { std::vector<int> vec; auto it = std::back_inserter(vec); // assigning through it adds elements to vec *it = 42; // vec now has one element with value 42 for (const auto &e : vec) { std::cout << e << " "; } std::cout << std::endl; std::vector<int> vec2; std::fill_n(std::back_inserter(vec2), 10, 0); // appends ten elements to vec2 for (const auto &e : vec2) { std::cout << e << " "; } std::cout << std::endl; return 0; }
From C++ Primer (5th) (Section 10.2.2)
back_inserter: Iterator adaptor that takes a reference to a container and
generates an insert iterator that uses push_back to add elements to the
specified container.
相关文章推荐
- C++面向对象高级开发_极客班_第一节课笔记_section2
- C语言--归并排序算法实现
- C++ 标准库类型-String,Vector and Bitset
- C++ Primer 变量和基本类型
- C++ Primer 快速入门
- c++基础要点整理(二)之——友元
- c++文件結束符
- C++中const迭代器 和 const_iterator的区别
- 对C++中异常处理的语法的认识
- 【C++】C++头文件
- C语言---快速排序算法实现
- debug assertion failed, file:dlgdata.cpp,line"43
- Ubuntu 12.04 安装eclipse for c++
- 16.c/c++程序员面试宝典-虚函数与纯虚函数
- C++11新特性之 std::future and std::async
- C++11新特性之 std::future and std::async
- c语言的不定长数组
- C++枚举与字符串之间的转换
- c语言3种链接属性: 外部(external), 内部(internal),无设置(none)
- C++不要在构造函数和析构函数中调用虚函数