adjacent_find() 搜索相邻的重复元素
//创建容器
vector<int> CreateVector()
{
vector<int> VeInt;
VeInt.push_back(1);
VeInt.push_back(2);
VeInt.push_back(2);
VeInt.push_back(3);
VeInt.push_back(3);
VeInt.push_back(4);
VeInt.push_back(5);
VeInt.push_back(5);
VeInt.push_back(6);
VeInt.push_back(1);
return VeInt;
}
//主函数
void main()
{
vector<int> intArr = CreateVector();
ShowVector(intArr);
vector<int>::iterator iter = adjacent_find(intArr.begin(), intArr.end());
if (iter != intArr.end())
{
cout << *iter << endl;
}
}
泛型算法名称 |
adjacent_find() |
作用 |
对比容器内的元素,相邻的元素相同,返回当前位置迭代器,否则返回迭代器结尾 |
参数 |
容器开头,容器结尾 |
binary_search() 二元搜索(二分查找)
void main()
{
vector<int> intArr = CreateVector1();
sort(intArr.begin(), intArr.end());//排序
ShowVector(intArr);
bool temp = binary_search(intArr.begin(), intArr.end(), 50);
if (temp)
{
cout << "有该元素" << endl;
}
else
{
cout << "没有找到元素50" << endl;
}
}
//在使用binary_search()搜索时需要容器中的元素是有序的,所以使用了sort()进行排序
sotr()也是C++STL中的算法。
泛型算法名称 |
binary_search() |
作用 |
查找值,有返回true,否则false |
参数 |
容器开头,容器结尾,需要查找的值 |
泛型算法名称 |
sotr() |
作用 |
进行升序排序 |
参数 |
容器开头,容器结尾 |