c++++ stl set 是一种有序、唯一元素集合容器,它允许插入、删除、查找和判断空等基本操作。它可以存储各种类型的数据,如整数或字符串。例如,要存储整数集合,可以使用 set<int>,而要存储字符串集合,可以使用 set<string>。

C++ 函数的 STL set 怎么用
简介
STL set 是 C++ 标准模板库 (STL) 中的一个容器类,它存储一个唯一的元素集合。set 的特质:
立即学习“C++免费学习笔记(深入)”;
使用语法
#include <set> using namespace std; set<type> mySet;
其中:
type 是要存储在 set 中的元素类型。基本操作
实战案例
存储整数集合
#include <set>
using namespace std;
int main() {
set<int> mySet;
mySet.insert(1);
mySet.insert(3);
mySet.insert(2);
// 输出 set 中的值(因为是 ordered,所以按升序输出)
for (int num : mySet) {
cout << num << " ";
}
// 输出:1 2 3
// 搜索并输出 3
auto it = mySet.find(3);
if (it != mySet.end()) {
cout << "Found 3" << endl;
}
return 0;
}存储字符串集合
#include <set>
#include <string>
using namespace std;
int main() {
set<string> mySet;
mySet.insert("Apple");
mySet.insert("Banana");
mySet.insert("Orange");
// 输出 set 中的值(因为是 ordered,所以按字母顺序输出)
for (const string& fruit : mySet) {
cout << fruit << " ";
}
// 输出:Apple Banana Orange
// 搜索并输出 "Banana"
auto it = mySet.find("Banana");
if (it != mySet.end()) {
cout << "Found Banana" << endl;
}
return 0;
}以上就是C++ 函数的 STL set 怎么用?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号