赞
踩
#include <set>
每个元素最多只出现一次,并且set中的元素已经从小到大排好序。
所有元素都会在插入时自动被排序
set<T> st;
//默认构造函数:set(const set &st);
//拷贝构造函数set& operator=(const set &st);
//重载等号操作符#include <set> void printSet(set<int> & s) { for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; } //构造和赋值 void test01() { set<int> s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); printSet(s1); //拷贝构造 set<int>s2(s1); printSet(s2); //赋值 set<int>s3; s3 = s2; printSet(s3); } int main() { test01(); system("pause"); return 0; }
总结:
size();
//返回容器中元素的数目empty();
//判断容器是否为空swap(st);
//交换两个集合容器#include <set> void printSet(set<int> & s) { for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; } //大小 void test01() { set<int> s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); if (s1.empty()) { cout << "s1为空" << endl; } else { cout << "s1不为空" << endl; cout << "s1的大小为: " << s1.size() << endl; } } //交换 void test02() { set<int> s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); set<int> s2; s2.insert(100); s2.insert(300); s2.insert(200); s2.insert(400); cout << "交换前" << endl; printSet(s1); printSet(s2); cout << endl; cout << "交换后" << endl; s1.swap(s2); printSet(s1); printSet(s2); } int main() { //test01(); test02(); system("pause"); return 0; }
总结:
insert(elem);
//在容器中插入元素。clear();
//清除所有元素erase(pos);
//删除pos迭代器所指的元素,返回下一个元素的迭代器。erase(beg, end);
//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。erase(elem);
//删除容器中值为elem的元素。#include <set> void printSet(set<int> & s) { for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; } //插入和删除 void test01() { set<int> s1; //插入 s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); printSet(s1); //删除 s1.erase(s1.begin()); printSet(s1); s1.erase(30); printSet(s1); //清空 //s1.erase(s1.begin(), s1.end()); s1.clear(); printSet(s1); } int main() { test01(); system("pause"); return 0; }
总结:
find(key);
//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();count(key);
//统计key的元素个数#include <set> //查找和统计 void test01() { set<int> s1; //插入 s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); //查找 set<int>::iterator pos = s1.find(30); if (pos != s1.end()) { cout << "找到了元素 : " << *pos << endl; } else { cout << "未找到元素" << endl; } //统计 int num = s1.count(30); cout << "num = " << num << endl; } int main() { test01(); system("pause"); return 0; }
总结:
#include <iostream> 2 #include<set> 3 using namespace std; 4 5 6 int main() 7 { 8 set<int> s; //创建一个int类型的set 9 10 s.insert(10); //插入数据 11 s.insert(30); 12 s.insert(20); 13 s.insert(40); 14 15 //遍历数据,用迭代器遍历数据 16 for (set<int>::iterator it = s.begin(); it != s.end(); ++it) 17 { 18 cout << *it << endl; 19 } 20 //这里用到了set中的元素已经从小到大排好序的性质 21 22 return 0; 23 }
结构体类型struct的set,使用时必须重载‘<’运算符
1 #include<iostream> 2 #include<set> 3 #include<string> 4 using namespace std; 5 struct Info 6 { 7 string name; 8 double score; 9 bool operator < (const Info &a) const // 重载“<”操作符,自定义排序规则 10 { 11 //按score由大到小排序。如果要由小到大排序,使用“>”即可。 12 return a.score < score; 13 } 14 }; 15 int main() 16 { 17 set<Info> s; 18 Info info; 19 20 //插入三个元素 21 info.name = "Jack"; 22 info.score = 80; 23 s.insert(info); 24 info.name = "Tom"; 25 info.score = 99; 26 s.insert(info); 27 info.name = "Steaven"; 28 info.score = 60; 29 s.insert(info); 30 31 set<Info>::iterator it; 32 for(it = s.begin(); it != s.end(); it++) 33 cout << (*it).name << " : " << (*it).score << endl; 34 return 0; 35 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。