文章目录 遍历算法 1. adjacent_find 2. binary_search() 3. count() 4. count_if()
遍历算法
1. adjacent_find
代码工程
查找相邻元素是否存在, 不存在返回容器最后位置的迭代器
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <string>
# include <vector>
# include <algorithm> using namespace std; void printVector ( const vector< int > & v)
{ for ( vector< int > :: const_iterator it = v. begin ( ) ; it != v. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ vector< int > v; v. push_back ( 10 ) ; v. push_back ( 50 ) ; v. push_back ( 30 ) ; v. push_back ( 20 ) ; v. push_back ( 40 ) ; v. push_back ( 40 ) ; printVector ( v) ; vector< int > :: iterator pos = adjacent_find ( v. begin ( ) , v. end ( ) ) ; if ( pos != v. end ( ) ) { cout << "找到相邻元素" << * pos << endl; } else { cout << "没有找到相邻元素" << endl; } return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
2. binary_search()
二分查找法,不加仿函数默认升序判断。下边程序是加仿函数的降序判断版本。
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <string>
# include <vector>
# include <algorithm> using namespace std; void printVector ( const vector< int > & v)
{ for ( vector< int > :: const_iterator it = v. begin ( ) ; it != v. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ vector< int > v; v. push_back ( 50 ) ; v. push_back ( 40 ) ; v. push_back ( 30 ) ; v. push_back ( 20 ) ; v. push_back ( 10 ) ; printVector ( v) ; bool ret = binary_search ( v. begin ( ) , v. end ( ) , 40 , greater< int > ( ) ) ; if ( ret == true) { cout << "找到元素" << endl; } else { cout << "没有找到元素" << endl; } return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
3. count()
返回值为符合条件个数
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <string>
# include <vector>
# include <algorithm> using namespace std; void printVector ( const vector< int > & v)
{ for ( vector< int > :: const_iterator it = v. begin ( ) ; it != v. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} class Person
{
public: Person ( string name, int age) { m_name = name; m_age = age; } bool operator== ( const Person & p) { if ( this-> m_age == p. m_age) { return true; } else { return false; } } string m_name; int m_age;
} ; void test01 ( )
{ int num = 0 ; vector< int > v; v. push_back ( 10 ) ; v. push_back ( 50 ) ; v. push_back ( 30 ) ; v. push_back ( 20 ) ; v. push_back ( 40 ) ; v. push_back ( 40 ) ; printVector ( v) ; num = count ( v. begin ( ) , v. end ( ) , 40 ) ; cout << "元素为40的个数为:" << num << endl; return ;
} void test02 ( )
{ int num = 0 ; vector< Person> v; Person p1 ( "赵云" , 25 ) ; Person p2 ( "刘备" , 26 ) ; Person p3 ( "曹操" , 28 ) ; Person p4 ( "张飞" , 26 ) ; Person p5 ( "关羽" , 29 ) ; Person p6 ( "黄忠" , 30 ) ; Person pp ( "马超" , 26 ) ; v. push_back ( p1) ; v. push_back ( p2) ; v. push_back ( p3) ; v. push_back ( p4) ; v. push_back ( p5) ; v. push_back ( p6) ; num = count ( v. begin ( ) , v. end ( ) , pp) ; cout << "与马超年龄相同的人有" << num << "个" << endl; return ;
} int main ( )
{ test01 ( ) ; cout << endl << "自定义元素类型查找" << endl; test02 ( ) ; return 0 ;
}
运行结果
4. count_if()
返回值为符合条件范围的个数
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <string>
# include <vector>
# include <algorithm> using namespace std; void printVector ( const vector< int > & v)
{ for ( vector< int > :: const_iterator it = v. begin ( ) ; it != v. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} class Person
{
public: Person ( string name, int age) { m_name = name; m_age = age; } string m_name; int m_age;
} ; class GreaterTwenty
{
public: bool operator ( ) ( int val) { if ( val > 20 ) { return true; } else { return false; } } bool operator ( ) ( const Person & p) { if ( p. m_age > 20 ) { return true; } else { return false; } }
} ; void test01 ( )
{ int num = 0 ; vector< int > v; v. push_back ( 10 ) ; v. push_back ( 50 ) ; v. push_back ( 30 ) ; v. push_back ( 70 ) ; v. push_back ( 40 ) ; v. push_back ( 60 ) ; printVector ( v) ; num = count_if ( v. begin ( ) , v. end ( ) , GreaterTwenty ( ) ) ; cout << "元素大于20的个数为:" << num << endl; return ;
} void test02 ( )
{ int num = 0 ; vector< Person> v; Person p1 ( "赵云" , 18 ) ; Person p2 ( "刘备" , 20 ) ; Person p3 ( "曹操" , 28 ) ; Person p4 ( "张飞" , 26 ) ; Person p5 ( "关羽" , 29 ) ; Person p6 ( "黄忠" , 19 ) ; v. push_back ( p1) ; v. push_back ( p2) ; v. push_back ( p3) ; v. push_back ( p4) ; v. push_back ( p5) ; v. push_back ( p6) ; num = count_if ( v. begin ( ) , v. end ( ) , GreaterTwenty ( ) ) ; cout << "年龄大于20的人有" << num << "个" << endl; return ;
} int main ( )
{ test01 ( ) ; cout << endl << "自定义元素类型查找" << endl; test02 ( ) ; return 0 ;
}
运行结果