练习 10.24:给定一个 string,使用 bind 和 check_size 在一个 int 的 vector 中查找第一个大于 string 长度的值。
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;
using namespace std::placeholders;
bool check_size(int length, string s)
{
return length > s.size();
}
int main()
{
string s;
cout << "请输入一个string:";
cin >> s;
vector<int> vint{0, 2, 4, 6, 8, 10, 12, 14, 16, 18};
auto firstBiggerValue = find_if(vint.begin(), vint.end(), bind(check_size, _1, s));
cout << "vector<int>中第一个大于string长度的值为:" << *firstBiggerValue << endl;
return 0;
}
本文介绍了一种利用C++标准库中的bind结合自定义函数check_size的方法,在整数向量中查找首个超过给定字符串长度的元素。通过具体代码示例展示了如何实现这一过程。
224

被折叠的 条评论
为什么被折叠?



