C++标准库使用范例

时间:2013-12-10 12:37:36
【文件属性】:

文件名称:C++标准库使用范例

文件大小:734KB

文件格式:DOC

更新时间:2013-12-10 12:37:36

STL ,C++

/************************** *范例编号:17_39 *范例说明:演示 unique 的 * 功能与使用方法 ***************************/ #include #include #include #include #include using namespace std; bool mod_equal ( int elem1, int elem2 ) { if ( elem1 < 0 ) elem1 = - elem1; if ( elem2 < 0 ) elem2 = - elem2; return elem1 == elem2; }; int main( ) { vector v1; vector ::iterator v1_Iter1, v1_Iter2, v1_Iter3, v1_NewEnd1, v1_NewEnd2, v1_NewEnd3; int i; for ( i = 0 ; i <= 3 ; i++ ) { v1.push_back( 5 ); v1.push_back( -5 ); } int ii; for ( ii = 0 ; ii <= 3 ; ii++ ) { v1.push_back( 4 ); } v1.push_back( 7 ); cout << "向量 v1=( " ; for ( v1_Iter1 = v1.begin( ) ; v1_Iter1 != v1.end( ) ; v1_Iter1++ ) cout << *v1_Iter1 << " "; cout << ")." << endl; v1_NewEnd1 = unique ( v1.begin ( ) , v1.end ( ) ); cout << "除去相连等价元素后 v1= ( " ; for ( v1_Iter1 = v1.begin( ) ; v1_Iter1 != v1_NewEnd1 ; v1_Iter1++ ) cout << *v1_Iter1 << " "; cout << ")." << endl; v1_NewEnd2 = unique ( v1.begin ( ) , v1_NewEnd1 , mod_equal ); cout << "在 mod_equal 意义下,除去相连等价元素后 v1= ( " ; for ( v1_Iter2 = v1.begin( ) ; v1_Iter2 != v1_NewEnd2 ; v1_Iter2++ ) cout << *v1_Iter2 << " "; cout << ")." << endl; v1_NewEnd3 = unique ( v1.begin ( ) , v1_NewEnd2, greater( ) ); cout << "在大于意义下,除去相连等价元素后 v1= ( " ; for ( v1_Iter3 = v1.begin( ) ; v1_Iter3 != v1_NewEnd3 ; v1_Iter3++ ) cout << *v1_Iter3 << " "; cout << ")." << endl; }


网友评论