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() |
作用 |
進行升序排序 |
參數 |
容器開頭,容器結尾 |