简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
1.前言
本篇目的:理解C++之map访问元素empty、size、max_size、operator[]、at、insert、erase、swap、clear、emplace、emplace_hint用法。
-
empty()
:检查map
是否为空。如果map
为空,返回true
;否则返回false
。
-
size()
:获取map
中键值对的数量。
-
max_size()
:获取map
可能包含的最大元素数量。
-
operator[]
:访问或修改给定键对应的值。如果键不存在,会将该键插入map
并关联一个默认构造的值。
-
at()
:访问给定键对应的值。与operator[]
类似,但如果键不存在,会抛出std::out_of_range
异常。
-
insert()
:向map
中插入一个键值对。
-
erase()
:从map
中删除一个或多个键值对。
-
swap()
:交换两个map
的内容。
-
clear()
:从map
中移除所有的键值对,使之成为空容器。
-
emplace()
:在map
中就地构造一个新的键值对。
-
emplace_hint()
:在给定位置的提示位置上就地构造一个新的键值对。
2.应用实例
1. empty()
函数:检查 map
是否为空。如果 map
为空,返回 true
;否则返回 false
。
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap;
if (myMap.empty()) {
std::cout << "Map is empty" << std::endl;
} else {
std::cout << "Map is not empty" << std::endl;
}
return 0;
}
2. size()
函数:获取 map
中键值对的数量。
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "orange"}};
std::cout << "Size of map: " << myMap.size() << std::endl;
return 0;
}
3. max_size()
函数:获取 map
可能包含的最大元素数量。
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap;
std::cout << "Max size of map: " << myMap.max_size() << std::endl;
return 0;
}
4. operator[]
:访问或修改给定键对应的值。如果键不存在,会将该键插入 map
并关联一个默认构造的值。
#include <iostream>
#include <map>
int main() {
std::map<char, int> myMap;
myMap['a'] = 1;
myMap['b'] = 2;
std::cout << "Value of key 'a': " << myMap['a'] << std::endl;
std::cout << "Value of key 'c': " << myMap['c'] << std::endl;
return 0;
}
5. at()
函数:访问给定键对应的值。与 operator[]
类似,但如果键不存在,会抛出 std::out_of_range
异常。
#include <iostream>
#include <map>
int main() {
std::map<char, int> myMap = {{'a', 1}, {'b', 2}};
try {
std::cout << "Value of key 'a': " << myMap.at('a') << std::endl;
std::cout << "Value of key 'c': " << myMap.at('c') << std::endl;
} catch(const std::out_of_range& e) {
std::cout << "Key not found in map" << std::endl;
}
return 0;
}
6. insert()
函数:向 map
中插入一个键值对。
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap;
myMap.insert({1, "apple"});
myMap.insert({2, "banana"});
myMap.insert({3, "orange"});
std::cout << "Size of map: " << myMap.size() << std::endl;
return 0;
}
7. erase()
函数:从 map
中删除一个或多个键值对。
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "orange"}};
myMap.erase(2);
std::cout << "Size of map: " << myMap.size() << std::endl;
return 0;
}
8. swap()
函数:交换两个 map
的内容。
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> firstMap = {{1, "apple"}, {2, "banana"}};
std::map<int, std::string> secondMap = {{3, "orange"}, {4, "grape"}};
firstMap.swap(secondMap);
std::cout << "Size of first map: " << firstMap.size() << std::endl;
std::cout << "Size of second map: " << secondMap.size() << std::endl;
return 0;
}
9. clear()
函数:从 map
中移除所有的键值对,使之成为空容器。
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "orange"}};
myMap.clear();
std::cout << "Size of map after clearing: " << myMap.size() << std::endl;
return 0;
}
10. emplace()
函数:在 map
中就地构造一个新的键值对。
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap;
myMap.emplace(1, "apple");
myMap.emplace(2, "banana");
myMap.emplace(3, "orange");
std::cout << "Size of map: " << myMap.size() << std::endl;
return 0;
}
11. emplace_hint()
函数:在给定位置的提示位置上就地构造一个新的键值对。
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap = {{1, "apple"}, {3, "orange"}};
auto it = myMap.find(3);
myMap.emplace_hint(it, 2, "banana");
std::cout << "Size of map: " << myMap.size() << std::endl;
return 0;
}