1. 程式人生 > 其它 >set容器之構造和賦值

set容器之構造和賦值

技術標籤:stl學習之set和multiset容器c++stl

set和multiset容器

set容器概念:所有容器在被插入時都會被自動排序
本質:set和multiset屬於關聯式容器,底層結構是用二叉樹實現
set和multiset容器的區別:

  1. set容器不可以有重複元素
  2. multiset容器可以有重複元素

set的構造和賦值

注意:set容器無法使用[]和at方式訪問在這裡插入圖片描述

#include<iostream>
using namespace std;
#include<set>
void p(set<int>& s)
{
	for (set<
int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; } void test() { set<int> s1 = {1,2,3}; //插入資料,只有用insert方式 s1.insert(4); s1.insert(6); s1.insert(6); s1.insert(5); //所有的元素在插入的時候會被自動排序 //set容器不能插入重複值(即使插入了重複的值,也不會報錯,但插入不成功)
p(s1); //拷貝構造 set<int> s2(s1); p(s2); //operator = set<int> s3; s3 = s2; p(s3); } int main() { test(); system("pause"); return 0; }

在這裡插入圖片描述