C++ Primer(第5版) 练习 10.24
练习 10.24 给定一个string,使用bind和check_size在一个int的vector中查找第一个大于string长度的值。。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
# include <iostream>
# include <string>
# include <vector>
# include <algorithm>
# include <functional>
using namespace std;
using namespace placeholders;
bool check_size ( const int & n, string:: size_type sz) {
return n > sz;
}
int main ( ) {
string str;
cout<< "Enter string: " ;
cin>> str;
string:: size_type sz = str. size ( ) ;
vector< int > number;
int num;
cout<< "Enter numbers: " ;
while ( cin>> num) {
number. push_back ( num) ;
if ( cin. get ( ) == '\n' ) {
break ;
}
}
auto wc = find_if ( number. begin ( ) , number. end ( ) , bind ( check_size, _1, sz) ) ;
cout<< * wc<< endl;
return 0 ;
}
运行结果显示如下