STL之查找算法
1.adjacent_find()
在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器。否则返回past-the-end。
vector<int> vecInt;
vecInt.push_back(1);
vecInt.push_back(2);
vecInt.push_back(2);
vecInt.push_back(4);
vecInt.push_back(5);
vecInt.push_back(5);
vector<int>::iterator it = adjacent_find(vecInt.begin(), vecInt.end()); //*it == 2
2.binary_search
在有序序列
中查找value,找到则返回true。注意:在无序序列中,不可使用。
set<int> setInt;
setInt.insert(3);
setInt.insert(1);
setInt.insert(7);
setInt.insert(5);
setInt.insert(9);
bool bFind = binary_search(setInt.begin(),setInt.end(),5);
3.count()
利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。
vector<int> vecInt;
vecInt.push_back(1);
vecInt.push_back(2);
vecInt.push_back(2);
vecInt.push_back(4);
vecInt.push_back(2);
vecInt.push_back(5);
int iCount = count(vecInt.begin(),vecInt.end(),2); //iCount==3
4.count_if()
假设vector vecIntA,vecIntA包含1,3,5,7,9元素
//先定义比较函数
bool GreaterThree(int iNum)
{
if(iNum>=3)
{
return true;
}
else
{
return false;
}
}
int iCount = count_if(vecIntA.begin(), vecIntA.end(), GreaterThree);
//此时iCount == 4
4.find()
find
: 利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的迭代器。
equal_range
: 返回一对iterator,第一个表示lower_bound,第二个表示upper_bound。
vector<int> vecInt;
vecInt.push_back(1);
vecInt.push_back(3);
vecInt.push_back(5);
vecInt.push_back(7);
vecInt.push_back(9);
vector<int>::iterator it = find(vecInt.begin(), vecInt.end(), 5); //*it == 5
5.find_if()
find_if: 使用输入的函数代替等于操作符执行find。返回被找到的元素的迭代器。
假设vector vecIntA,vecIntA包含1,3,5,3,9元素
vector<int>::it = find_if(vecInt.begin(),vecInt.end(),GreaterThree);
//此时 *it==3, *(it+1)==5, *(it+2)==3, *(it+3)==9
6.综合代码
void main44_adjacent_find()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(2);
v1.push_back(3);
v1.push_back(5);
vector<int>::iterator it = adjacent_find(v1.begin(), v1.end() );
if (it == v1.end())
{
cout << "没有找到 重复的元素" << endl;
}
else
{
cout << *it << endl;
}
int index = distance(v1.begin(), it);
cout << index << endl;
}
// 0 1 2 3 ......n-1
//二分法 1K = 1024 10次 速度快
void main45_binary_search()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(7);
v1.push_back(9);
bool b = binary_search(v1.begin(), v1.end(), 7);
if (b == true)
{
cout << "找到了" << endl;
}
else
{
cout << "没到了" << endl;
}
}
void main46_count()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(7);
v1.push_back(7);
v1.push_back(9);
v1.push_back(7);
int num = count(v1.begin(), v1.end(), 7);
cout << num << endl;
}
bool GreatThree(int iNum)
{
if (iNum > 3)
{
return true;
}
return false;
}
void main46_countif()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(7);
v1.push_back(7);
v1.push_back(9);
v1.push_back(7);
int num = count_if(v1.begin(), v1.end(), GreatThree);
cout << "num:" << num << endl;
}
void main47_find_findif()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(7);
v1.push_back(7);
v1.push_back(9);
v1.push_back(7);
vector<int>::iterator it = find(v1.begin(), v1.end(), 5);
cout << "*it:" << *it << endl;
//第一个大于3的位置
vector<int>::iterator it2 = find_if(v1.begin(), v1.end(), GreatThree);
cout << "*it2:" << *it2 << endl;
}