qfedu-cpp-level/day10/stl_algorithm_demo/f6.cpp

31 lines
680 B
C++
Raw Permalink Normal View History

2023-08-14 17:20:39 +08:00
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace std::placeholders;
int main()
{
int m[] = {1, 2, 2, 3, 5, 10};
vector<int> v;
v.assign(m, m + sizeof(m) / sizeof(m[0]));
int target = 3;
// 使用 std::bind 适配 std::equal_to
vector<int>::iterator ret = adjacent_find(v.begin(), v.end(), bind(equal_to<int>(), _1, 3));
cout << *(++ret) << endl;
if (ret != v.end())
{
cout << "Found adjacent elements equal to " << target << "." << endl;
}
else
{
cout << "No adjacent elements equal to " << target << " found." << endl;
}
return 0;
}