首页 > 其他 > 详细

STL之set

时间:2021-02-01 21:47:38      阅读:27      评论:0      收藏:0      [点我收藏+]

特点

有序且唯一

#include<stdio.h>
//1.头文件引入 
#include<set>
using namespace std;
int main(){
	//2.set的定义
    //	set<类型> 名称 
	set<int> test;
	//3.元素插入
	for(int i = 0;i < 3;i++){
		test.insert(10-i);
	}
	test.insert(5);
	test.insert(5);
	//4.元素访问,只能使用迭代器
	for(set<int>::iterator it = test.begin();it != test.end();it ++)
		printf("%d ",*(it));
	//5.find返回指定值的迭代器
	printf("\nfind返回指定值的迭代器%d\n",*(test.find(9)));
	//6.erase删除元素
	//删除迭代器
	test.erase(test.find(5));
	//删除指定值
	test.erase(8);
	for(set<int>::iterator it = test.begin();it != test.end();it ++)
		printf("%d ",*(it));
	//删除一个区间 
	test.insert(5);
	test.insert(6);	 
	test.insert(7);
	test.insert(8);
	test.erase(test.find(6),test.find(8)) ;
	printf("\n删除区间[6,8)"); 
	for(set<int>::iterator it = test.begin();it != test.end();it ++)
		printf("%d ",*(it));
	//7.长度和清空
	printf("\n请空前长度%d",test.size()) ;
	test.clear();
	printf("\n请空后长度%d",test.size()) ;
	}

技术分享图片

STL之set

原文:https://www.cnblogs.com/weilongZhang/p/14358872.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!