1.查找vector中某个元素是否存在?
vector<string> vStr;
int nRet = std::count(vStr.begin(), vStr.end(), "abc");//判断vector中是否有 "abc" 这个元素
vector<int>vector1;
count(vector1.begin(), vector1.end(), 10)//返回值为对应元素(10)的个数。
2.查找某个元素在向量中的位置?
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
vector<int>vector1;
vector1.resize(10);
for (int i = 0; i < vector1.size(); i++)
{
vector1[i] = vector1.size() - i;
}
int number = 3;
vector<int>::iterator iter;
iter = find(vector1.begin(), vector1.end(), number);
int dis = distance(vector1.begin(), iter);
cout << "number " << number << "在向量vector1中的位置为 " << dis << endl;
return 0;
}
3. 对vector进行排序
template<typename T>void
vector_sort(std::vector<T> vector_input, std::vector<size_t> &idx){
idx.resize(vector_input.size());
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&vector_input](size_t i1, size_t i2) { return vector_input[i1] < vector_input[i2]; });
}
- 对vector进行复制
template<typename T>vector<T>
RangeCopy(std::vector<T> v, int startIndex, int count)
{
return std::vector<T>(v.begin() + startIndex, v.begin() + count);
}
- 对vector进行去重,消除重复的元素
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cctype>
int main()
{
// 移除重复元素
std::vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7};
std::sort(v.begin(), v.end()); // 1 1 2 2 3 3 3 4 4 5 5 6 7 //先排序
auto last = std::unique(v.begin(), v.end());//去重
// v 现在保有 {1 2 3 4 5 6 7 x x x x x x} ,其中 x 不确定
v.erase(last, v.end()); //删除
for (int i : v)
std::cout << i << " ";
std::cout << "\n";
}
- 求向量最大值和最小值所在的位置
例 vector<int> vec
最大值:int maxValue = *max_element(v.begin(),v.end());
最小值:int minValue = *min_element(v.begin(),v.end());
最大值下标:int maxPosition = max_element(v.begin(),v.end()) - v.begin();
最小值下标:int minPosition = min_element(v.begin(),v.end()) - v.begin();
7.向量填充
头文件:#include <numeric>
vector<int>x_axis;
x_axis.resize(number_);
iota(x_axis.begin(),x_axis.end(), 0);//从0开始,步长为1进行填充
//x_axis are 0 1 2 3 4 5 6 7 8 9-------
std::iota(std::begin(data), std::end(data), -2.5);
// Values are -2.5 -1.5 -0.5 0.5 1.5 2.5 3.5 4.5 5.5
8.在向量后追加另一个向量
std::vector<int> src;
std::vector<int> dest;
dest.insert(dest.end(), src.begin(), src.end());
- 仿函数书写
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool compare_(pair<string, int> &v1, pair<string, int> &v2){/普通函数
if (v1.second==v2.second)
{
return v1.first[0] < v2.first[0];
}
return v1.second > v2.second;
}
int main(){
vector<pair<string,int>>vec1;
vec1.push_back(make_pair("刘三",10));
vec1.push_back(make_pair("按四", 30));
vec1.push_back(make_pair("王五", 50));
vec1.push_back(make_pair("钱六", 20));
vec1.push_back(make_pair("赵七", 10));
sort(vec1.begin(), vec1.end(),compare_);//普通函数只需要添加函数名即可,仿函数需要添加函数对象
for (vector<pair<string, int>>::iterator it = vec1.begin(); it != vec1.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
return 0;
}
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
class compare_{
public:
bool operator()(int v1, int v2){//仿函数,重载()
return v1 > v2;
}
};
int main(){
multimap<int, int, compare_>m_map;
m_map.insert(make_pair(10, 20));
m_map.insert(make_pair(20, 30));
m_map.insert(make_pair(50, 40));
m_map.insert(make_pair(20, 20));
for (multimap<int, int,compare_>::iterator it = m_map.begin(); it != m_map.end(); it++)//对于不连续的,在参数列表加仿函数的函数名即可
{
cout << (*it).first << " " << (*it).second << endl;
}
return 0;
}
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <functional>
using namespace std;
void compare_(int val){//普通函数
cout << val<<" ";
}
class compare_class{//仿函数书写
public:
void operator()(int val){
cout << val << " ";
}
};
int main(){
vector<int>vec;
for (int i = 0; i < 10;i++)
{
vec.push_back(i);
}
for_each(vec.begin(), vec.end(), compare_);//普通函数+函数名即可
cout << "\n"<<"------------------" << endl;
for_each(vec.begin(), vec.end(), compare_class());//仿函数+类名和重载的括号即可
return 0;
}