算法库(一)

 1.      max_element:返回键值最大的元素

2.       min_element: 返回键值最小的元素

3.       sort:以升序排序

4.       copy:复制元素

5.       reverse:反转元素

6.       find:查找元素

class TestClass

{

public:

         //constructionfuction

         TestClass(){

                   reset();

         }

         voidreset()

         {

                   m_iNo = 0;

                   m_strName = "";

                   m_strSex = "";

         }

 

         TestClass( inti_NO,string str_name,string str_sex ):m_iNo( i_NO ),m_strSex(str_sex),m_strName( str_name ){}

 

         voidsetValue(const TestClass& tc )

         {

                   m_iNo = tc.m_iNo;

                   m_strSex = tc.m_strSex;

                   m_strName = tc.m_strName;

         }

         TestClass( constTestClass& tc )

         {

                   setValue( tc );

         }

        

         //assignoperator

         TestClass& operator=(const TestClass& tc )

         {

                   setValue( tc );

                   return*this;

         }

         //compare fuction

         booloperator<( constTestClass& tc )

         {

                   returnm_iNo < tc.m_iNo;

         }

         //printfriend fuction

         friendostream&operator<<( ostream&output,const TestClass& ts );

private:

         int              m_iNo;

         string         m_strName;

         string         m_strSex;

};

 

ostream& operator<<( ostream& output,const TestClass& tc )

{

         output<<"m_iNo"<"\nm_strName"<"\nm_strSex"<"\n";

         returnoutput;

}

 

int main()

{

         vector vecTc;

         TestClass tc1( 10011,"??¢?","D" );

         vecTc.push_back( tc1 );

 

         TestClass tc2( 10012,"¢?Ì?a","D" );

         vecTc.push_back( tc2 );

 

         TestClass tc3( 10006,"?¡ì®?","D" );

         vecTc.push_back( tc3 );

 

         TestClass tc4( 10002,"¨´??","D" );

         vecTc.push_back( tc4 );

 

         cout<<"printvector member\n";

         copy(vecTc.begin(),vecTc.end(),ostream_iterator( cout,"\n" ));

 

         vector::const_iteratormax_cIter = max_element( vecTc.begin(),vecTc.end() );

         cout<<"printvector max_member"<<"\n";

         if (max_cIter != vecTc.end() )

         {

                   cout<<*max_cIter<<"\n";

         }

 

         vector::const_iteratormin_cIter = min_element( vecTc.begin(),vecTc.end() );

         cout<<"printvector min_member"<<"\n";

         if (min_cIter != vecTc.end() )

         {

                   cout<<*min_cIter<<"\n";

         }

 

         TestClass cls( 10006,"aº?","?" );

         vector::const_iteratorcIter = find( vecTc.begin(),vecTc.end());

         if (cIter != vecTc.end() )

         {

                   cout<<"find element"<<"\n";

         }

         sort( vecTc.begin(),vecTc.end() );

         cout<<"printvector member"<<"\n";

         copy(vecTc.begin(),vecTc.end(),ostream_iterator( cout,"\n" ));

 

         reverse( vecTc.begin(),vecTc.end() );

         cout<<"printvector member"<<"\n";

         copy(vecTc.begin(),vecTc.end(),ostream_iterator( cout,"\n" ));

         system("pause");

         return0;

}

 

输出结果:

 

print vector member

m_iNo10011

m_strName袁孙良

m_strSex男

 

m_iNo10012

m_strName刘德华

m_strSex男

 

m_iNo10006

m_strName张学友

m_strSex男

 

m_iNo10002

m_strName郭富城

m_strSex男

 

print vector max_member

m_iNo10012

m_strName刘德华

m_strSex男

 

print vector min_member

m_iNo10002

m_strName郭富城

m_strSex男

 

find element

print vector member

m_iNo10002

m_strName郭富城

m_strSex男

 

m_iNo10006

m_strName张学友

m_strSex男

 

m_iNo10011

m_strName袁孙良

m_strSex男

 

m_iNo10012

m_strName刘德华

m_strSex男

 

print vector member

m_iNo10012

m_strName刘德华

m_strSex男

 

m_iNo10011

m_strName袁孙良

m_strSex男

 

m_iNo10006

m_strName张学友

m_strSex男

 

m_iNo10002

m_strName郭富城

m_strSex男

 

请按任意键继续. . .

 


你可能感兴趣的:(STL,C++标准库)