title: C++ Trick Methods
tags: C++
categories: C++
C++ Trick Methods
介绍c++中一些简化编码/提高效率/单纯有趣的函数
iota()
http://c.biancheng.net/view/681.html
定义在 numeric 头文件中的 iota() 函数模板会用连续的 T 类型值填充序列。前两个参数是定义序列的正向迭代器,第三个参数是初始的 T 值。 T 类型必须支持 operator++()
相当于使用enum为容器进行初始化
1 2 3 4 5
| #include <numeric> std::vector<double> data(9); double initial {-4}; std::iota (std::begin (data) , std::end (data) , initial);
|
使用ostream_iterator输出容器
https://www.jianshu.com/p/c14f60ee3527
头文件:#include <iterator>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <iostream> #include <vector> #include <algorithm> #include <iterator>
int main() { std::vector<int> data = { 1, 21, 31, 41, 51, 61, 71, 81 }; std::ostream_iterator<int> dataIter(std::cout, ", "); std::copy(data.begin(), data.end(), dataIter);
return 0; }
|
STL汇总
数组反转
1
| reverse(stl.begin(), stl.end());
|