如何高效掌握C++ STL中set集合的运用技巧?
- 内容介绍
- 文章标签
- 相关推荐
本文共计555个文字,预计阅读时间需要3分钟。
一、简介集合(Set)是一种包含已排序对象的关联容器,不允许有重复元素。
二、完整程序代码pythondef main(): # 创建一个集合 my_set={1, 2, 3, 4, 5}
# 打印集合 print(集合内容:, my_set)
# 添加元素 my_set.add(6) print(添加元素后:, my_set)
# 删除元素 my_set.discard(3) print(删除元素后:, my_set)
# 检查元素 print(元素4在集合中:, 4 in my_set)
# 集合运算 set1={1, 2, 3} set2={3, 4, 5} print(集合1和集合2的并集:, set1 | set2) print(集合1和集合2的交集:, set1 & set2) print(集合1和集合2的差集:, set1 - set2) print(集合1和集合2的对称差集:, set1 ^ set2)
if __name__==__main__: main()
一、简介
集合(Set)是一种包含已排序对象的关联容器,不允许有重复元素。
二、完整程序代码
/*请务必运行以下程序后对照阅读*/ #include <set> #include <iostream> using namespace std; int main() { ///1. 初始化 set<int> num; set<int>::iterator iter; cout << num.max_size() << endl;///set容纳上限 cout << endl; ///2. 添加元素 for (int i = 0; i < 10; i++) num.insert(i); cout << num.size() << endl; cout << endl; ///3. 遍历 ///不同于map,set容器不提供下标操作符 for (iter = num.begin(); iter != num.end(); iter++) cout << *iter << " " ; cout << endl; cout << endl; ///4. 查询 iter = num.find(1); if (iter != num.end()) cout << *iter << endl; else cout << -1 << endl; iter = num.find(99); if (iter != num.end()) cout << *iter << endl; else cout << -1 << endl; cout << endl; ///5. 删除 iter = num.find(1); num.erase(iter); cout << num.size() << endl; for (iter = num.begin(); iter != num.end(); iter++) cout << *iter << " " ; cout << endl; cout << endl; ///6. 判空与清空 if (!num.empty()) num.clear(); }
三、补充
map容器是键-值对的集合,好比以人名为键的地址和电话号码。相反地,set容器只是单纯的键的集合。当我们想知道某位用户是否存在时,使用set容器是最合适的。
参考网址:www.cplusplus.com/reference/set/set/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计555个文字,预计阅读时间需要3分钟。
一、简介集合(Set)是一种包含已排序对象的关联容器,不允许有重复元素。
二、完整程序代码pythondef main(): # 创建一个集合 my_set={1, 2, 3, 4, 5}
# 打印集合 print(集合内容:, my_set)
# 添加元素 my_set.add(6) print(添加元素后:, my_set)
# 删除元素 my_set.discard(3) print(删除元素后:, my_set)
# 检查元素 print(元素4在集合中:, 4 in my_set)
# 集合运算 set1={1, 2, 3} set2={3, 4, 5} print(集合1和集合2的并集:, set1 | set2) print(集合1和集合2的交集:, set1 & set2) print(集合1和集合2的差集:, set1 - set2) print(集合1和集合2的对称差集:, set1 ^ set2)
if __name__==__main__: main()
一、简介
集合(Set)是一种包含已排序对象的关联容器,不允许有重复元素。
二、完整程序代码
/*请务必运行以下程序后对照阅读*/ #include <set> #include <iostream> using namespace std; int main() { ///1. 初始化 set<int> num; set<int>::iterator iter; cout << num.max_size() << endl;///set容纳上限 cout << endl; ///2. 添加元素 for (int i = 0; i < 10; i++) num.insert(i); cout << num.size() << endl; cout << endl; ///3. 遍历 ///不同于map,set容器不提供下标操作符 for (iter = num.begin(); iter != num.end(); iter++) cout << *iter << " " ; cout << endl; cout << endl; ///4. 查询 iter = num.find(1); if (iter != num.end()) cout << *iter << endl; else cout << -1 << endl; iter = num.find(99); if (iter != num.end()) cout << *iter << endl; else cout << -1 << endl; cout << endl; ///5. 删除 iter = num.find(1); num.erase(iter); cout << num.size() << endl; for (iter = num.begin(); iter != num.end(); iter++) cout << *iter << " " ; cout << endl; cout << endl; ///6. 判空与清空 if (!num.empty()) num.clear(); }
三、补充
map容器是键-值对的集合,好比以人名为键的地址和电话号码。相反地,set容器只是单纯的键的集合。当我们想知道某位用户是否存在时,使用set容器是最合适的。
参考网址:www.cplusplus.com/reference/set/set/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

