ชุดเป็นประเภทข้อมูลนามธรรมซึ่งแต่ละองค์ประกอบต้องไม่ซ้ำกันเนื่องจากค่าขององค์ประกอบจะระบุ เมื่อเพิ่มเข้าไปในชุดแล้ว ค่าขององค์ประกอบจะไม่สามารถแก้ไขได้ แต่จะลบและเพิ่มค่าที่แก้ไขขององค์ประกอบนั้นได้
แผนที่คือคอนเทนเนอร์ที่เชื่อมโยงกันซึ่งจัดเก็บองค์ประกอบในรูปแบบแผนที่ แต่ละองค์ประกอบมีค่าคีย์และค่าที่แมป ไม่มีค่าที่แมปสองค่าใดที่สามารถมีค่าคีย์ที่เหมือนกันได้
ดังนั้น จากข้างบนจึงเป็นที่ชัดเจนว่า set มีคีย์เดียว และ map มีค่าที่มีคีย์ ทั้งคู่ควรมีค่าที่ไม่ซ้ำกันและจัดเรียงไว้
สำหรับองค์ประกอบที่ไม่เรียงลำดับและไม่เรียงลำดับ จะมี unordered_set/unordered_map,multiset/multimap
โค้ดตัวอย่าง
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s; //initializing a empty set container
set<int>::iterator it; //Initializing a set container as iterator
s.insert(7); //inserting elements in the set container s
s.insert(6);
s.insert(1);
s.insert(4);
s.insert(2);
s.insert(9);
s.insert(10);
cout << "Elements are in set:\n";
for ( auto it : s)
cout << it << " "; //printing elements of the set container
return 0;
} ผลลัพธ์
1 2 4 6 7 9 10
โค้ดตัวอย่าง
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<char, int> m; //initialize a map
map<char, int>::iterator iter; //initializing a map as iterator
m.insert (pair<char, int>('a', 10)); //inserting values to the map
m.insert (pair<char, int>('b', 20));
cout << "Elements in map:\n";
for (iter=m.begin();iter!=m.end();iter++)
cout << "[ " << iter->first << ", "<< iter->second << "]\n"; //printing the values of the map
return 0;
} ผลลัพธ์
Elements in map: [ a, 10] [ b, 20]