#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void adjacent_find_()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(3);
vec.push_back(3);
vec.push_back(5);
//adjacent_find:查找相邻的重复元素,找到则返回指向这对元素的第一个元素的迭代器,否则返回past-the-end
vector<int>::iterator it = adjacent_find(vec.begin(), vec.end());
if (it == vec.end())
{
cout << "没有找到重复元素" << endl;
}
else
{
cout << "重复元素为:" << *it << endl;
}
int n = distance(vec.begin(), it);
cout << "重复元素是第" << n+1 << "个" << endl;
}
void binary_search_()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(8);
//binary_search:二分查找,只可以应用在有序序列中,返回布尔类型值
//输入开始以及结束查找的位置,第三个参数为要查找的元素
bool bo = binary_search(vec.begin(),vec.end(),5);
if (bo == true)
{
cout << "找到了" << endl;
}
else
{
cout << "没找到" << endl;
}
}
void count_()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(3);
vec.push_back(4);
vec.push_back(3);
vec.push_back(8);
vec.push_back(3);
vec.push_back(3);
//count:返回容器中与输入的值相等的总数
//输入开始以及结束查找的位置,第三个参数为要比对的元素
int cou = count(vec.begin(), vec.end(), 3);
cout << cou << endl;
}
bool GreaterThree(int &num)
{
return (num >= 3);
}
void count_if_()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(8);
vec.push_back(10);
vec.push_back(12);
int num = count_if(vec.begin(), vec.end(), GreaterThree);
cout << "大于等于3的元素的个数为:" << num << endl;
}
void find_()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(8);
vec.push_back(10);
vec.push_back(12);
//find:查找元素为5的迭代器的位置
vector<int>::iterator it = find(vec.begin(), vec.end(), 5);
if (it == vec.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "it:" << *it << endl;
}
//find_if:查找元素大于等于3的迭代器的位置
vector<int>::iterator it2 = find_if(vec.begin(), vec.end(), GreaterThree);
if (it2 == vec.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "it2:" << *it2 << endl;
}
}
void main()
{
adjacent_find_();
binary_search_();
count_();
count_if_();
find_();
system("pause");
}
adjacent_find:查找相邻的重复元素,找到则返回指向这对元素的第一个元素的迭代器,否则返回past-the-end
binary_search:二分查找,只可以应用在有序序列中,返回布尔类型值,输入开始以及结束查找的位置,第三个参数为要查找的元素
count:返回容器中与输入的值相等的总数,输入开始以及结束查找的位置,第三个参数为要比对的元素
find:查找某元素的迭代器的位置