Introduction: CSDN blog expert, focusing on Android/Linux System, share multi-mic voice solutions, audio and video, codec and other technologies, and grow with everyone!
Quality Column:Audio Engineer Advanced Series[Original information is being updated continuously… ]
Life motto: There are never shortcuts in life, only actions It is the only cure for fear and laziness.
For more original works, welcome to follow: Android System Siege Lion
1. Preface
The purpose of this article: Understand the usage of map access elements empty, size, max_size, operator[], at, insert, erase, swap, clear, emplace, and emplace_hint in C++.
-
empty()
: Check whethermap
is empty. Ifmap
is empty, returntrue
; otherwise, returnfalse
.
-
size()
: Get the number of key-value pairs inmap
.
-
max_size()
: Gets the maximum number of elements thatmap
may contain.
-
operator[]
: Access or modify the value corresponding to a given key. If the key does not exist, the key is inserted into themap
and associated with a default constructed value.
-
at()
: Access the value corresponding to a given key. Similar tooperator[]
, but if the key does not exist, astd::out_of_range
exception will be thrown.
-
insert()
: Insert a key-value pair intomap
.
-
erase()
: Delete one or more key-value pairs frommap
.
-
swap()
: Exchange the contents of twomap
.
-
clear()
: Remove all key-value pairs frommap
, making it an empty container.
-
emplace()
: Constructs a new key-value pair in place inmap
.
-
emplace_hint()
: Constructs a new key-value pair in-place at the hint position at the given position.
2. Application examples
1. empty()
function: Check whether map
is empty. If map
is empty, returns true
; otherwise, returns 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()
function: Get the number of key-value pairs in 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()
function: Get the maximum number of elements that map
may contain.
#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[]
: Access or modify the value corresponding to a given key. If the key does not exist, the key is inserted into the map
and associated with a default-constructed value.
#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()
function: access the value corresponding to a given key. Similar to operator[]
, but if the key does not exist, a std::out_of_range
exception will be thrown.
#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 & amp; e) {<!-- --> std::cout << "Key not found in map" << std::endl; } return 0; }
6. insert()
function: Insert a key-value pair into 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()
function: delete one or more key-value pairs from 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()
function: exchange the contents of two 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()
function: Remove all key-value pairs from map
, making it an empty container.
#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()
function: Construct a new key-value pair in place in 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()
Function: Constructs a new key-value pair in place at the hint position at a given position.
#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; }