C++ multimap 是标准模板库 (STL) 中的一个关联容器,它提供了 map 的功能,但允许多个元素具有相同的键。
创建 C++ Multimap
要创建 C++ 中的 multimap,我们首先需要包含 <map>
头文件。
#include <map>
导入此文件后,我们可以使用以下语法创建 multimap
std::multimap<key_type, value_type> multimap_name = {
{key1, value1},
{key2, value2},
...
};
这里,
key_type
- 键的数据类型value_type
- 值的数据类型{key1, value1}, {key2, value2}, ...
- 要分配给 multimap 的键值对
注意:在我们的教程中,我们将使用 std
命名空间。因此,我们可以直接写 multimap
而不是 std::multimap
。
例如,
// create a multimap with int key and string value
multimap<int, string> my_multimap = {
{1, "One"},
{1, "Uno"},
{2, "Two"},
};
这会创建一个名为 my_multimap 的 multimap,其中键是 int
类型,值是 string
类型。
示例:C++ Multimap
#include <iostream>
#include <map>
using namespace std;
int main() {
// create a multimap
multimap<int, string> my_multimap = {
{1, "Un"},
{1, "One"},
{2, "Two"},
{2, "Dos"},
{1, "Uno"},
{2, "Deux"}
};
cout << "Key - Value" << endl;
// loop across the multimap
// display the key-value pairs
for(const auto& key_value: my_multimap) {
int key = key_value.first;
string value = key_value.second;
cout << key << " - " << value << endl;
}
return 0;
}
输出
Key - Value 1 - Un 1 - One 1 - Uno 2 - Two 2 - Dos 2 - Deux
在上面的示例中,我们声明并初始化了一个名为 my_multimap 的 multimap。
请注意,multimap 已按其键排序,而不是按插入顺序排序。
注意:插入顺序仅对具有相同键的元素进行维护。
然后,我们使用 范围 for 循环 显示了 multimap 的内容。
for(const auto& key_value: my_multimap) {
int key = key_value.first;
string value = key_value.second;
cout << key << " - " << value << endl;
}
在上面的代码中,multimap 的每个元素(键值对)都存储在变量 key_value 中。
然后,我们使用以下方式获取键和值:
key_value.first
- 获取键key_value.second
- 获取值
C++ Multimap 方法
在 C++ 中,multimap
类提供了各种方法来对 multimap 执行不同的操作。
方法 | 描述 |
---|---|
insert() |
插入一个或多个键值对 |
count() |
返回指定键的出现总次数 |
find() |
返回指定键的元素的迭代器 |
size() |
返回元素数量 |
empty() |
如果 multimap 为空,则返回 true |
erase() |
删除具有指定键的元素 |
clear() |
移除所有元素 |
向 Multimap 中插入数据
我们可以使用 insert()
方法在 multimap 中插入一个**键值**对。例如,
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<string, int> numbers;
// insert a pair {"Odd", 1}
numbers.insert({"Odd", 1});
// insert multiple pairs
numbers.insert({{"Even", 2}, {"Even", 6}, {"Odd", 5}, {"Even", 4}});
cout << "Key - Value" << endl;
// display numbers
for(const auto& key_value: numbers) {
string key = key_value.first;
int value = key_value.second;
cout << key << " - " << value << endl;
}
return 0;
}
输出
Key - Value Even - 2 Even - 6 Even - 4 Odd - 1 Odd - 5
从 Multimap 中删除元素
我们可以使用以下方法从 multimap 中删除元素:
erase()
- 删除具有指定键的元素clear()
- 删除所有 multimap 元素
例如,
#include <iostream>
#include <map>
using namespace std;
// function prototype for display_multimap()
void display_multimap(const multimap<int, string> &);
int main() {
multimap<int, string> student {
{111, "John"},
{132, "Mark"},
{143, "Chris"},
{143, "Christopher"}
};
cout << "Initial Multimap:" << endl;
display_multimap(student);
// remove element with key: 143
student.erase(143);
cout << "\nMultimap After Erasing Key 143: " << endl;
display_multimap(student);
// remove all multimap elements
student.clear();
cout << "\nMultimap After Clearing: " << endl;
display_multimap(student);
return 0;
}
// function to print multimap elements
void display_multimap(const multimap<int, string> &umap){
for(const auto& key_value: umap) {
int key = key_value.first;
string value = key_value.second;
cout << key << " - " << value << endl;
}
}
输出
Initial Multimap: 111 - John 132 - Mark 143 - Chris 143 - Christopher Multimap After Erasing Key 143: 111 - John 132 - Mark Multimap After Clearing:
在上面的示例中,我们使用 erase()
方法从 multimap 中删除了键为 **143** 的元素。
然后,我们使用 clear()
方法删除了 multimap 的所有元素。
检查 Multimap 的大小
我们可以使用 empty()
方法检查 multimap 是否为空,并使用 size()
方法计算其大小。
empty()
方法返回
- true - 如果 multimap 为空。
- false - 如果 multimap 不为空。
size()
方法返回 multimap 中的元素数量。例如,
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<string, int> my_multimap;
// check capacity before insertion
string result = my_multimap.empty()? "Yes" : "No";
int multimap_size = my_multimap.size();
cout << "Before Insertion";
cout << "\nIs multimap empty? " << result << endl;
cout << "multimap size: " << multimap_size << endl;
// insert element
my_multimap.insert({"apple",2});
// check capacity after insertion
result = my_multimap.empty()? "Yes" : "No";
multimap_size = my_multimap.size();
cout << "\nAfter Insertion" << endl;
cout << "\Is multimap empty? " << result << endl;
cout << "multimap size: " << multimap_size << endl;
return 0;
}
输出
Before Insertion Is multimap empty? Yes multimap size: 0 After Insertion Is multimap empty? No multimap size: 1
在这里,使用 三元运算符,result 变量存储字符串
- 如果 multimap 为空(即,当
empty()
返回true
时),则为Yes
- 如果 multimap 不为空(即,当
empty()
返回false
时),则为Yes
在 Multimap 中查找键
我们可以使用 find()
函数查找具有特定键的元素。
find()
函数返回
- 如果**找到键**,则返回指向该元素的迭代器
- 如果**未找到键**,则返回指向容器末尾的迭代器(
end()
迭代器)
例如,
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<string, int> my_multimap;
my_multimap.insert({"apple", 5});
my_multimap.insert({"banana", 3});
my_multimap.insert({"apple", 2});
my_multimap.insert({"cherry", 4});
// find elements with the key "apple"
string key_to_find = "apple";
auto found = my_multimap.find(key_to_find);
// print the found key
if (found != my_multimap.end()) {
cout << "Found key " << key_to_find << ": " << endl;
cout << found->first << " - " << found->second << endl;
}
else {
cout << "Key " << key_to_find << " not found." << endl;
}
return 0;
}
输出
Found key apple: apple - 5
从示例可以看出,find()
方法仅返回 multimap 中键的第一个实例。
检查键是否存在于 Multimap 中
我们可以使用以下方法检查指定的键是否存在于 multimap 中。
find()
- 如果找到键,则返回指向该元素的迭代器,否则返回end()
迭代器count()
- 返回指定键的出现总次数
例如,
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<int, string> students {
{20, "Chris"},
{28, "Joseph"},
{20, "Blake"}
};
cout << "Using count():" << endl;
cout << "Does age " << 20 << " exist? ";
// count() returns the number of occurences of the specified key
int count_20 = students.count(20);
if (count_20) {
cout << "Yes";
cout << "\nTotal Students with age 20 = " << count_20 << endl;
}
else {
cout << "No";
}
cout << "\nUsing find():" << endl;
cout << "Does age " << 27 << " exist? ";
// find() returns students.end()
// if the key is not found
if (students.find(27) != students.end()) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
输出
Using count(): Does age 20 exist? Yes Total Students with age 20 = 2 Using find(): Does age 27 exist? No
在上面的示例中,我们使用了 find()
和 count()
方法来检查给定的键是否存在于 multimap 中。
最初,我们使用 count()
方法查找键 **20** 出现的总次数。
int count_20 = students.count(20);
然后,我们使用 find()
方法检查键 **27** 是否存在。
if (students.find(27) != students.end()) {
cout << "Yes";
}
else {
cout << "No";
}