iota
产生递增序列,C98
template< class ForwardIt, class T >
void iota( ForwardIt first, ForwardIt last, T value );
0-9
的序列 std::vector arr(10, 0);
std::iota(arr.begin(), arr.end(), 0);
for (int v:arr)
std::cout << " " << v;
accumulate
累加两个迭代器之间的值, 左开右闭。
C98
std::cout << std::accumulate(arr.begin(), arr.end(), 0);
adjacent_difference
数组中相邻元素的差值
C98
int bin_op(int a1, int a2)
{
return (a1 + a2) * (a1 - a2);
}
int main()
{
std::vector arr = {0,10,32,45,9,10,2,3};
std::vector<int> tmp(arr.size(), 0);
std::vector<int> tmp2(arr.size(), 0);
std::vector<int> tmp3(arr.size(), 0);
std::adjacent_difference(arr.begin(), arr.end(), tmp.begin());
std::adjacent_difference(arr.begin(), arr.end(), tmp2.begin(),std::multiplies<>());
std::adjacent_difference(arr.begin(), arr.end(), tmp3.begin(), bin_op);
for (int v:tmp)
std::cout << " " << v ;
std::cout << std::endl;
for (int v:tmp2)
std::cout << " " << v;
std::cout << std::endl;
for (int v:tmp3)
std::cout << " " << v;
std::cout << std::endl;
return 0;
}
向量内积,何以重载两个操作符。
std::vector<int> l = { 10,24};
std::vector<int> r = { 2,3,};
int init_val = 0;
int v = std::inner_product(l.begin(),l.end(),r.begin(), init_val);
// 0 + (10 * 2) + (24 * 3) = 92
int v2 = std::inner_product( l.begin(), l.end(), r.begin(), init_val,
std::minus<>(), std::divides<>());
// 0 - (10 /2) - (24/3) = -13
std::cout << v << std::endl;
std::cout << v2 << std::endl;
前缀和,也可以重载操作符。
std::vector<int> l = { 10,24,40,23};
std::vector<int> r = { 2,3,};
std::vector<int> preSum(l.size(), 0);
std::vector<int> preMul(l.size(), 0);
std::partial_sum(l.begin(), l.end(), preSum.begin());
std::partial_sum( l.begin(), l.end(), preMul.begin(),std::multiplies<>());
for (int v:preSum)
std::cout << v << " ";
std::cout << std::endl;
for (int v:preMul)
std::cout << v << " ";
std::cout << std::endl;
distance
两个迭代器间的距离,C98
std::cout << std::distance(arr.begin(), arr.end());
advance
C98
向前向后移动迭代器n
个单位
对双向迭代器或者随机访问迭代器,n
可以为负值。
std::list<int> l = { 0,2,1,4,3,-1,0};
std::list<int>::iterator it = l.begin();
std::advance(it, 4);
std::cout << *it << std::endl;
std::vector<int> arr = {10,9,-9,2,11,0};
std::vector<int>::iterator vit = arr.end();
std::advance(vit, -2);
std::cout << *vit << std::endl;
copy
将一个容器值拷贝到另一个容器。
注意拷贝到的容器大小必须大于拷贝的数目,否则拷贝不完整。
std::vector<int> l = { 10,24,40,23};
std::vector<int> r (l.size(), 0);
std::copy(l.begin(), l.end(), r.begin());
for (int v: r)
std::cout << v << " ";
std::cout << std::endl;
equal
比较两个容器内的值是否完全一致。
std::vector<int> l = { 10,24,40,23};
int myints[] = {10,24,40,23};
bool is_same = std::equal(l.begin(), l.end(), myints);
std::cout << is_same << std::endl;
fill
将容器中所有值填充为某个值
int myints[] = {10,24,40,23};
std::fill(myints, myints + 4, 10);
for (int i = 0; i < 4; ++i)
std::cout << myints[i] << std::endl;
find
再容器中查找某个值
int myints[] = {10,24,40,23};
int v = 23;
auto it = std::find(myints, myints + 4, v);
if ( it == myints + 4)
std::cout << "not find " << v << std::endl;
else
std::cout << "find at pos " << it - myints << std::endl;
for_each
对容器中每个元素实施的操作
std::unordered_map<char,int> um = { {'A', 100}, {'B',20}};
std::for_each(um.begin(), um.end(), [](std::pair<char,int> q) ->void
{ std::cout << q.first << "->" << q.second << std::endl;});
lower_bound
二分查找第一个不小于v
值的迭代器位置,找不到就返回end
位置
std::vector<int> arr{1,2,3,4,5,10};
int v = 3;
std::vector<int>::iterator it = std::lower_bound(arr.begin(), arr.end(), v);
if ( it != arr.begin()) {
std::cout << "first element no less than v's pos: "<<std::distance(arr.begin(),it) << std::endl;
}
else {
std::cout << "all element less than " << v << std::endl;
}
reference_cpp