1. c++ vector 每个元素加上一个特定值 (c++ vector add a constant value for each element)
https://stackoverflow.com/questions/4461446/stl-way-to-add-a-constant-value-to-a-stdvector
vector<int> x = {0, 30, 80, 100, 120, 150, 190, 220, 250};//transform可以将函数应用到序列的元素上,bind2nd通过绑定其中一个参数把二元函数转换成一元函数transform(x.begin(), x.end(), x.begin(), bind2nd(plus<int>(), 1));//显示x的值copy(x.begin(), x.end(), ostream_iterator<int>(cout, " "));结果: x = {1 31 81 101 121 151 191 221 251}
2. c++判断vector中是否存在某个元素(c++ judge whether an element exists in the vector)
https:///erase-elements-vector-cpp/
vector<int> x = {0, 30, 150, 30, 220, 80};
//vector中的remove的作用是将等于value的元素放到vector的尾部,但并不减少vector的size
//vector中erase的作用是删除掉某个位置position或一段区域(begin, end)中的元素,减少其size
x.erase(remove(x.begin(), x.end(), 30), x.end());
结果: x = {0 150 220 80}
10. c++ 统计 vector 某个元素出现的次数 (C++ count the number of occurrences of an element in vector)
https://www.geeksforgeeks.org/std-count-cpp-stl/
vector<int> x = { 0, 3, 5, 6, 3, 2, 3 };
int n = count(x.begin(), x.end(), 3);
结果:n = 3
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。