STL 中的 set 和 map 是相似的,因为它们都使用红黑树(一种自平衡 BST)。请注意,搜索、插入和删除的时间复杂度为 O(Log n)。
区别:
不同之处在于 set 仅用于存储键,而 map 用于存储键值对。例如,在打印排序后的不同元素的问题中,我们使用 set 作为键需要值。而如果我们将问题更改为打印不同排序元素的频率,我们将使用 map。我们需要 map 将数组值存储为键,将频率存储为值。
CPP
// CPP program to demonstrate working of set
#include
using namespace std;
int main()
{
set s1;
s1.insert(2);
s1.insert(5);
s1.insert(3);
s1.insert(6);
cout << "Elements in set:\n";
for (auto it : s1)
cout << it << " "; // Sorted
return 0;
}
CPP
// CPP program to demonstrate working of map
#include
using namespace std;
int main()
{
map m;
m[1] = 2; // Insertion by indexing
// Direct pair insertion
m.insert({ 4, 5 });
// Insertion of pair by make_pair
m.insert(make_pair(8, 5));
cout << "Elements in map:\n";
for (auto it : m)
cout << "[ " << it.first << ", "
<< it.second << "]\n"; // Sorted
return 0;
}
CPP
// CPP program to demonstrate working of Multimap
#include
using namespace std;
int main()
{
multimap m;
m.insert({ 1, 2 });
m.insert({ 2, 3 });
m.insert({ 4, 5 });
m.insert({ 2, 3 });
m.insert({ 1, 2 });
cout << "Elements in Multimap:\n";
for (auto it : m)
cout << "[ " << it.first << ", "
<< it.second << "]\n"; // Sorted
return 0;
}
CPP
// CPP program to demonstrate working of Multiset
#include
using namespace std;
int main()
{
multiset ms;
ms.insert(1);
ms.insert(3);
ms.insert(4);
ms.insert(2);
ms.insert(2);
cout << "Elements in Multiset:\n";
for (auto it : ms)
cout << it << " ";
return 0;
}
CPP
// CPP program to demonstrate working of Unordered_set
#include
using namespace std;
int main()
{
unordered_set us;
us.insert(1);
us.insert(3);
us.insert(4);
us.insert(2);
us.insert(2);
cout << "Elements in unordered_set:\n";
for (auto it : us)
cout << it << " "; // Sorted
return 0;
}
CPP
// CPP program to demonstrate working of Unordered_map
#include
using namespace std;
int main()
{
unordered_map um;
um[1] = 2;
um[4] = 5;
um[2] = 3;
um[8] = 5;
um[3] = 6;
cout << "Elements in unordered_map:\n";
for (auto it : um)
cout << "[ " << it.first << ", " << it.second << "]\n";
return 0;
}
输出:
Elements in set:
2 3 5 6
CPP
// CPP program to demonstrate working of map
#include
using namespace std;
int main()
{
map m;
m[1] = 2; // Insertion by indexing
// Direct pair insertion
m.insert({ 4, 5 });
// Insertion of pair by make_pair
m.insert(make_pair(8, 5));
cout << "Elements in map:\n";
for (auto it : m)
cout << "[ " << it.first << ", "
<< it.second << "]\n"; // Sorted
return 0;
}
输出:
Elements in map:
[ 1, 2]
[ 4, 5]
[ 8, 5]
set 和 map 的变化:
Set 和 Map 都存储唯一值和排序值。但是如果我们没有这样的需求,我们使用multiset/multimap和unordered_set/unordered_map。
Multimap :Multimap 不允许通过索引存储元素。
CPP
// CPP program to demonstrate working of Multimap
#include
using namespace std;
int main()
{
multimap m;
m.insert({ 1, 2 });
m.insert({ 2, 3 });
m.insert({ 4, 5 });
m.insert({ 2, 3 });
m.insert({ 1, 2 });
cout << "Elements in Multimap:\n";
for (auto it : m)
cout << "[ " << it.first << ", "
<< it.second << "]\n"; // Sorted
return 0;
}
输出:
Elements in Multimap:
[ 1, 2]
[ 1, 2]
[ 2, 3]
[ 2, 3]
[ 4, 5]
多组:
CPP
// CPP program to demonstrate working of Multiset
#include
using namespace std;
int main()
{
multiset ms;
ms.insert(1);
ms.insert(3);
ms.insert(4);
ms.insert(2);
ms.insert(2);
cout << "Elements in Multiset:\n";
for (auto it : ms)
cout << it << " ";
return 0;
}
输出:
Elements in Multiset:
1 2 2 3 4
无序_set :
CPP
// CPP program to demonstrate working of Unordered_set
#include
using namespace std;
int main()
{
unordered_set us;
us.insert(1);
us.insert(3);
us.insert(4);
us.insert(2);
us.insert(2);
cout << "Elements in unordered_set:\n";
for (auto it : us)
cout << it << " "; // Sorted
return 0;
}
输出:
Elements in unordered_set:
2 4 1 3
无序_地图:
CPP
// CPP program to demonstrate working of Unordered_map
#include
using namespace std;
int main()
{
unordered_map um;
um[1] = 2;
um[4] = 5;
um[2] = 3;
um[8] = 5;
um[3] = 6;
cout << "Elements in unordered_map:\n";
for (auto it : um)
cout << "[ " << it.first << ", " << it.second << "]\n";
return 0;
}
输出:
Elements in unordered_map:
[ 3, 6]
[ 2, 3]
[ 8, 5]
[ 1, 2]
[ 4, 5]
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程。