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"<
returnoutput;
}
int main()
{
vector
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
copy(vecTc.begin(),vecTc.end(),ostream_iterator
vector
cout<<"printvector
if (max_cIter != vecTc.end() )
{
cout<<*max_cIter<<"\n";
}
vector
cout<<"printvector
if (min_cIter != vecTc.end() )
{
cout<<*min_cIter<<"\n";
}
TestClass cls( 10006,"测a试º?","女?" );
vector
if (cIter != vecTc.end() )
{
cout<<"find element"<<"\n";
}
sort( vecTc.begin(),vecTc.end() );
cout<<"printvector
copy(vecTc.begin(),vecTc.end(),ostream_iterator
reverse( vecTc.begin(),vecTc.end() );
cout<<"printvector
copy(vecTc.begin(),vecTc.end(),ostream_iterator
system("pause");
return0;
}
输出结果:
print vector
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
m_iNo10012
m_strName刘德华
m_strSex男
print vector
m_iNo10002
m_strName郭富城
m_strSex男
find element
print vector
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
m_iNo10012
m_strName刘德华
m_strSex男
m_iNo10011
m_strName袁孙良
m_strSex男
m_iNo10006
m_strName张学友
m_strSex男
m_iNo10002
m_strName郭富城
m_strSex男
请按任意键继续. . .