在C++中,set容器是一个有序的容器,其中的元素是唯一的,不允许重复。
使用set容器需要包含头文件,然后可以按照以下步骤使用set容器:
- 创建set容器:
#include <set>
std::set<int> mySet;
- 插入元素到set容器中:
mySet.insert(10);
mySet.insert(20);
mySet.insert(30);
- 遍历set容器中的元素:
for(auto it = mySet.begin(); it != mySet.end(); ++it) {
std::cout << *it << " ";
}
- 查找元素是否存在于set容器中:
if(mySet.find(10) != mySet.end()) {
std::cout << "Element found in set" << std::endl;
} else {
std::cout << "Element not found in set" << std::endl;
}
- 删除元素:
mySet.erase(20);
以上是set容器的基本使用方法,可以根据具体情况进行扩展和修改。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/966167.html