文章目录 set容器 1.构造和赋值 2.大小和交换 4.插入和删除 5.查找和统计 6.multset 7.指定排序规则 8.自定义数据类型排序
set容器
所有元素都会在插入时,自动被排序。set/multset属于关联式容器,底层结构式用二叉树实现的。set容器不允许有重复的元素;multset容器允许有重复的元素。
1.构造和赋值
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <set> using namespace std;
void printSet ( const set< int > & s)
{ for ( set< int > :: const_iterator it = s. begin ( ) ; it != s. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ set< int > s; s. insert ( 20 ) ; s. insert ( 30 ) ; s. insert ( 10 ) ; s. insert ( 40 ) ; printSet ( s) ; set< int > s1 ( s) ; printSet ( s1) ; set< int > s2; s2 = s1; printSet ( s2) ; return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
2.大小和交换
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <set> using namespace std;
void printSet ( const set< int > & s)
{ for ( set< int > :: const_iterator it = s. begin ( ) ; it != s. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ set< int > s; s. insert ( 20 ) ; s. insert ( 30 ) ; s. insert ( 10 ) ; s. insert ( 40 ) ; printSet ( s) ; if ( 0 != s. empty ( ) ) { cout << "容器为空" << endl; return ; } cout << "容器的大小:" << s. size ( ) << endl; set< int > s1; s1. insert ( 200 ) ; s1. insert ( 300 ) ; s1. insert ( 100 ) ; s1. insert ( 400 ) ; cout << endl; cout << "交换两个容器的元素" << endl; cout << "交换前s容器元素:" << "\t" ; printSet ( s) ; cout << "交换前s1容器元素:" << "\t" ; printSet ( s1) ; s. swap ( s1) ; cout << "交换后s容器元素:" << "\t" ; printSet ( s) ; cout << "交换后s1容器元素:" << "\t" ; printSet ( s1) ; return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
4.插入和删除
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <set> using namespace std;
void printSet ( const set< int > & s)
{ for ( set< int > :: const_iterator it = s. begin ( ) ; it != s. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ set< int > s; s. insert ( 20 ) ; s. insert ( 30 ) ; s. insert ( 10 ) ; s. insert ( 40 ) ; printSet ( s) ; s. erase ( s. begin ( ) ) ; printSet ( s) ; s. erase ( 40 ) ; printSet ( s) ; s. clear ( ) ; printSet ( s) ; if ( 0 == s. empty ( ) ) { cout << "容器不为空" << endl; } else { cout << "容器为空" << endl; } return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
5.查找和统计
工程代码
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <set> using namespace std;
void printSet ( const set< int > & s)
{ for ( set< int > :: const_iterator it = s. begin ( ) ; it != s. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ set< int > s; s. insert ( 20 ) ; s. insert ( 30 ) ; s. insert ( 10 ) ; s. insert ( 40 ) ; printSet ( s) ; set< int > :: iterator pos = s. find ( 30 ) ; if ( s. end ( ) == pos) { cout << "没找到元素" << endl; } else { cout << * pos << endl; cout << "统计元素是20的个数:" << s. count ( 20 ) << endl; } return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
6.multset
multset可以插入重复的元素
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <set> using namespace std; void printMultset ( const multiset< int > & ms)
{ for ( multiset< int > :: const_iterator it = ms. begin ( ) ; it != ms. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ multiset< int > ms; ms. insert ( 20 ) ; ms. insert ( 30 ) ; ms. insert ( 30 ) ; ms. insert ( 10 ) ; printMultset ( ms) ; return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
7.指定排序规则
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <set> using namespace std; class MyCmp
{
public: bool operator ( ) ( int v1, int v2) const { return v1 > v2; }
} ; void printSet ( set< int , MyCmp> & s)
{ for ( set< int , MyCmp> :: iterator it = s. begin ( ) ; it != s. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ set< int , MyCmp> s; s. insert ( 20 ) ; s. insert ( 30 ) ; s. insert ( 10 ) ; s. insert ( 40 ) ; printSet ( s) ; return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
8.自定义数据类型排序
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <string>
# include <set> using namespace std; class Person
{
public: Person ( string name, int age) { my_name = name; my_age = age; } string my_name; int my_age;
} ; class MyCmp
{
public: bool operator ( ) ( Person p1, Person p2) const { if ( p1. my_age > p2. my_age) { return true; } else { return false; } }
} ; void printSet ( set< Person, MyCmp> & s)
{ for ( set< Person, MyCmp> :: iterator it = s. begin ( ) ; it != s. end ( ) ; it++ ) { cout << "姓名:" << it-> my_name << "\t" << "年龄" << it-> my_age << endl; }
} void test01 ( )
{ Person p1 ( "曹操" , 28 ) ; Person p2 ( "刘备" , 30 ) ; Person p3 ( "赵云" , 35 ) ; set< Person, MyCmp> s; s. insert ( p1) ; s. insert ( p2) ; s. insert ( p3) ; printSet ( s) ; return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果