for_each--遍历
vector<int> v1;
for (size_t i = 0; i < 10; i++)
{
v1.push_back(i);
}
for_each(v1.begin(), v1.end(), prinvector);
find
找到与否都返回一个迭代器
vector<int> v1;
for (size_t i = 0; i < 10; i++)
{
v1.push_back(i);
}
for_each(v1.begin(), v1.end(), prinvector);
vector<int>::iterator it = find(v1.begin(), v1.end(), 9);
if (it==v1.end())
cout << "没找到!" << endl;
else
cout << "找到了," << *it << endl;
}
find_if
与find区别在于可以使用条件进行查找
class FindCondition
{
bool operator()(int val)
{
return 10==val;
}
};
find_if(v1.begin(),v1.end(),FindCondition());
adjaent_find
查找相邻重复元素,如果找到返回第一个元素迭代器,未找到返回end()
binary_search
二分查找容器元素,容器必须是有序的,返回bool类型
vector<int> v1;
for (size_t i = 0; i < 10; i++)
{
v1.push_back(i);
}
bool ret = binary_search(v1.begin(), v1.end(), 10);
if (ret)
cout << "找到了" << endl;
else
cout << "没找到" << endl;
count
统计相同元素的个数
vector<int> v1;
for (size_t i = 0; i < 10; i++)
{
v1.push_back(i);
v1.push_back(i + 1);
}
int num =count(v1.begin(), v1.end(), 9);
cout << num<<endl;
count_if
按条件统计相同元素个数
class FindCondition
{
bool operator()(int val)
{
return 10==val;
}
};
vector<int> v1;
for (size_t i = 0; i < 10; i++)
{
v1.push_back(i);
v1.push_back(i + 1);
}
int num =count_if(v1.begin(), v1.end(), FindCondition());
cout << num<<endl;