Files
python/C++专题课程/STL基础知识/STL-5-set关联式容器.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

40 lines
991 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
using namespace std;
/*
知识点内容:STL set详解
关于set,必须说明的是set关联式容器。 set作为一个容器也是用来存储同一数据类型的数据类型,
并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。
文档内容参考:
https://www.cnblogs.com/aiguona/p/7231399.html
* */
int main() {
int i;
set<int> set1;
for (i = 0; i < 10; ++i)
set1.insert(i);
//迭代器
set<int>::iterator it;
for (it = set1.begin(); it != set1.end(); it++)
cout << *it << "\t";
cout << endl;
//把3插入到set1中,插入成功则set1.insert(3).second返回1,否则返回0.
set1.erase(5);
if (set1.insert(3).second)
cout << "set insert success";
else
cout << "set insert failed";
cout << endl;
//迭代输出
set<int>::iterator itr;
for (itr = set1.begin(); itr != set1.end(); itr++)
cout << *itr << "\t";
set1.clear();
return 0;
}