作者 CppLive | 发表于 2011-06-09
分章分类 : C++, 标准模板库
标签: C++, STL, 容器
1、构造函数
#include <iostream>
#include <set>
int main ()
{
int ary[] = { 5,3,7,5,2,3,7,5,5,4 };
set<int> s1;
set<int, greater<int> > s2;
for ( int i=0; i<sizeof(ary)/sizeof(int); i++ )
{
s1.insert(ary[i]);
s2.insert(ary[i]);
}
set<int>::iterator It = s1.begin();
cout << "s1 : ";
while ( It != s1.end() )
cout << *(It++) << " ";
cout << endl;
It = s2.begin();
cout << "s2 : ";
while ( It != s2.end() )
cout << *(It++) << " ";
cout << endl;
// 第二种形式的构造
set<int> s3(ary,ary+3);
It = s3.begin();
cout << "s3 : ";
while ( It != s3.end() )
cout << *(It++) << " ";
cout << endl;
//拷贝构造
set<int, less > s4(s1);
It = s4.begin();
cout << "s4 : ";
while ( It != s4.end() )
cout << *(It++) << " ";
cout << endl;
return 0;
}
运行结果:
// s1 : 2 3 4 5 7
// s2 : 7 5 4 3 2
// s3 : 3 5 7
// s4 : 2 3 4 5 7
阅读全文