C++之STL整理(3)之map 用法(創(chuàng)建、賦值、方法)整理
注:整理一些突然學到的C++知識,隨時mark一下
例如:忘記的關(guān)鍵字用法,新關(guān)鍵字,新數(shù)據(jù)結(jié)構(gòu)
提示:本文為 C++ 中 map構(gòu)造、賦值、接口 的寫法和舉例
一、map的初始化
??STL 中的map是一個關(guān)聯(lián)容器,它存儲的元素都是鍵值對(key-value pair),并且根據(jù)鍵(key)自動排序的容器。map不允許鍵重復,每個鍵在map中只能出現(xiàn)一次。map容器的每一個元素都是一個pair結(jié)構(gòu)(pair<t1,t2>
)的數(shù)據(jù)。
以下對map的初始化用法及舉例:
1、 map構(gòu)造函數(shù)
默認構(gòu)造函數(shù)
std::map<int, std::string> myMap; // 創(chuàng)建一個空的map,鍵類型為int,值類型為std::string
拷貝構(gòu)造函數(shù)
std::map<int, std::string> myMap1 = { {1, "one"}, {2, "two"} };
std::map<int, std::string> myMap2(myMap1); // 使用myMap1初始化myMap2,myMap2現(xiàn)在包含myMap1的所有元素
2、 map賦值操作(=,swap)
重載等號操作符
std::map<int, std::string> myMap1 = { {1, "one"}, {2, "two"} };
std::map<int, std::string> myMap2;
myMap2 = myMap1; // 使用重載的等號操作符將myMap1的內(nèi)容賦值給myMap2
swap函數(shù)
std::map<int, std::string> myMap1 = { {1, "one"}, {2, "two"} };
std::map<int, std::string> myMap2 = { {3, "three"}, {4, "four"} };
myMap1.swap(myMap2); // 交換myMap1和myMap2的內(nèi)容,現(xiàn)在myMap1包含{3, "three"}, {4, "four"},myMap2包含{1, "one"}, {2, "two"}
3、 map的容量(size、empty)
size函數(shù)
std::map<int, std::string> myMap = { {1, "one"}, {2, "two"}, {3, "three"} };
std::size_t size = myMap.size(); // size現(xiàn)在是3,因為myMap中有3個元素
empty函數(shù)
std::map<int, std::string> myMap;
if (myMap.empty()) {
std::cout << "The map is empty." << std::endl; // 輸出"The map is empty.",因為myMap是空的
} else {
std::cout << "The map is not empty." << std::endl;
}
這些函數(shù)展示了map的一些基本用法,包括創(chuàng)建map、添加元素、復制map、交換map內(nèi)容以及查詢map的大小和是否為空。map的鍵和值可以是任何數(shù)據(jù)類型,包括自定義類型,只要鍵類型支持比較操作即可
二、map的增刪查改
以下是針對map插入和刪除操作的示例代碼:
1、map插入數(shù)據(jù)元素操作
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> mapStu;
// 第一種 通過pair的方式插入對象
mapStu.insert(std::pair<int, std::string>(3, "小張"));
// 第二種 通過make_pair的方式插入對象(注意:您的示例中寫成了inset,這是錯誤的,應該是insert)
mapStu.insert(std::make_pair(-1, "校長"));
// 第三種 通過value_type的方式插入對象
mapStu.insert(std::map<int, std::string>::value_type(1, "小李"));
// 第四種 通過數(shù)組的方式插入值
// 這種方式在鍵已存在時更新對應的值,在鍵不存在時插入新的鍵值對
mapStu[3] = "小劉"; // 注意:這會替換掉key為3的原始值"小張"
mapStu[5] = "小王";
// 輸出map內(nèi)容
for (const auto& kv : mapStu) {
std::cout << kv.first << ": " << kv.second << std::endl;
}
return 0;
}
2、map鍵值對value的修改
map容器中,改變某個鍵(key)對應的值(value)可以通過以下方式實現(xiàn)。
使用下標操作符(operator[])
下標操作符可以用于訪問或修改map中的元素。如果鍵已經(jīng)存在,它會返回對應的引用,你可以直接通過這個引用修改值。如果鍵不存在,它會創(chuàng)建一個新的鍵值對,鍵為你提供的鍵,值為該類型的默認值。
std::map<int, std::string> mapStu;
mapStu[3] = "小張"; // 如果鍵3不存在,會創(chuàng)建一個新的鍵值對(3, "小張")
mapStu[3] = "小劉"; // 如果鍵3已經(jīng)存在,會更新其對應的值為"小劉"
使用find方法和迭代器
首先,使用find方法查找鍵在map中的位置,然后檢查返回的迭代器是否指向map的結(jié)束位置(即鍵是否不存在)。如果鍵存在,可以通過迭代器訪問并修改其對應的值。
std::map<int, std::string> mapStu;
mapStu[3] = "小張";
// 使用find查找鍵3
auto it = mapStu.find(3);
if (it != mapStu.end()) {
// 鍵存在,修改其對應的值
it->second = "小劉";
} else {
// 鍵不存在,可以選擇插入新的鍵值對
mapStu[3] = "小劉";
}
在這個例子中,it->second就是鍵為3的元素的值的引用,你可以直接給它賦新的值來修改它。
使用find方法則更加安全,因為它不會在你嘗試修改不存在的鍵時創(chuàng)建新的鍵值對。
在大多數(shù)情況下,如果只是想修改現(xiàn)有鍵的值,并且確定該鍵一定存在,使用下標操作符可能是最簡潔的方式。但如果你不確定鍵是否存在,或者不想在鍵不存在時創(chuàng)建新的鍵值對,那么使用find方法會更加合適。
3、map的刪操作(erase、clear)
假設mapStu是一個map容器,則:
mapStu.clear(); //刪所有
mapStu.erase(it);//刪迭代器指定
mapStu.erase(beg, end);//刪key區(qū)間
mapStu.erase(key1); //刪key對應
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> mapStu;
// 假設已經(jīng)插入了一些元素
mapStu.insert(std::pair<int, std::string>(3, "小張"));
mapStu.insert(std::pair<int, std::string>(5, "小王"));
mapStu.insert(std::pair<int, std::string>(7, "小趙"));
// 刪除所有元素
mapStu.clear();
std::cout << "After clear(): ";
for (const auto& kv : mapStu) {
std::cout << kv.first << ": " << kv.second << " ";
}
std::cout << std::endl;
// 重新插入一些元素
mapStu.insert(std::pair<int, std::string>(3, "小張"));
mapStu.insert(std::pair<int, std::string>(5, "小王"));
mapStu.insert(std::pair<int, std::string>(7, "小趙"));
// 刪除指定迭代器所指的元素
auto it = mapStu.find(5);
if (it != mapStu.end()) {
mapStu.erase(it);
}
// 輸出map內(nèi)容
std::cout << "After erase(pos): ";
for (const auto& kv : mapStu) {
std::cout << kv.first << ": " << kv.second << " ";
}
std::cout << std::endl;
// 重新插入一些元素
mapStu.insert(std::pair<int, std::string>(3, "小張"));
mapStu.insert(std::pair<int, std::string>(5, "小王"));
mapStu.insert(std::pair<int, std::string>(7, "小趙"));
// 刪除指定區(qū)間的元素
auto beg = mapStu.find(3);
auto end = mapStu.find(7);
mapStu.erase(beg, end); // 注意:這里刪除的是[beg, end)區(qū)間
// 輸出map內(nèi)容
std::cout << "After erase(beg, end): ";
for (const auto& kv : mapStu) {
std::cout << kv.first << ": " << kv.second << " ";
}
std::cout << std::endl;
// 重新插入一些元素
mapStu.insert(std::pair<int, std::string>(3, "小張"));
mapStu.insert(std::pair<int, std::string>(5, "小王"));
// 刪除指定key的元素
mapStu.erase(5);
// 輸出map內(nèi)容
std::cout << "After erase(keyElem): ";
for (const auto& kv : mapSt
三 、指定鍵的排序規(guī)則
std::map
默認是按照鍵(key)的升序進行排序的。然而,你可以通過提供自定義的比較函數(shù)或仿函數(shù)(functor)來改變這一排序規(guī)則。這里其實map的定義語法模板要填三個參數(shù),map<T1,T2,R> ,第三個參數(shù)就是比較規(guī)則的函數(shù)(仿函數(shù)、lamda表達式、謂詞等函數(shù)對象)
以下是一個例子,展示了如何使用std::greater仿函數(shù)來創(chuàng)建一個鍵為降序的map:
#include <map>
#include <string>
#include <iostream>
#include <functional> // 為了使用std::greater
int main() {
// 創(chuàng)建一個鍵為降序的map,使用std::greater<int>作為比較函數(shù)
std::map<int, std::string, std::greater<int>> mapStuDesc;
mapStuDesc[3] = "小張";
mapStuDesc[5] = "小王";
mapStuDesc[1] = "小李";
// 遍歷map并輸出元素
for (const auto& pair : mapStuDesc) {
std::cout << "鍵: " << pair.first << ", 值: " << pair.second << std::endl;
}
return 0;
}
在這個例子中,std::greater<int>
是一個仿函數(shù),它告訴map按照鍵的降序來存儲元素。當你遍歷這個map時,你會看到鍵是按照從大到小的順序輸出的。還可以創(chuàng)建自己的仿函數(shù)來實現(xiàn)更復雜的排序規(guī)則。例如,如果想要根據(jù)某個自定義的條件(假設是鍵的絕對值)來排序鍵,可以這樣做:
struct MyCompare {
bool operator()(const int& a, const int& b) const {
// 在這里實現(xiàn)你的比較邏輯
// 例如,假設我們想要按照鍵的絕對值來排序
return std::abs(a) < std::abs(b);
}
};
int main() {
std::map<int, std::string, MyCompare> mapAbsDesc;
mapAbsDesc[-3] = "小張";
mapAbsDesc[5] = "小王";
mapAbsDesc[-1] = "小李";
// 遍歷map并輸出元素
for (const auto& pair : mapAbsDesc) {
std::cout << "鍵: " << pair.first << ", 值: " << pair.second << std::endl;
}
return 0;
}
在這個例子中,MyCompare
是一個自定義的仿函數(shù),它根據(jù)鍵的絕對值來進行比較。因此,當你遍歷mapAbsDesc
時,元素會按照鍵的絕對值的升序輸出。
四、pair結(jié)構(gòu)
pair
通常用于將兩個值組合成一個單一的實體,以便可以作為一個整體進行傳遞或返回。std::pair 的兩個元素可以是任何類型,包括基本數(shù)據(jù)類型(如 int、double 等)或自定義類型(如類、結(jié)構(gòu)體等)。這兩個元素分別被稱為 first 和 second
。
使用方法是先包含頭文件:#include <utility>
定義和使用 std::pair:文章來源:http://www.zghlxwxcb.cn/news/detail-859275.html
std::pair<int, std::string> myPair;
myPair.first = 10;
myPair.second = "Hello";
使用 make_pair 函數(shù)創(chuàng)建 std::pair對象:文章來源地址http://www.zghlxwxcb.cn/news/detail-859275.html
std::pair<int, std::string> myPair = std::make_pair(10, "Hello");
總結(jié)
到了這里,關(guān)于C++之STL整理(3)之map 用法(創(chuàng)建、賦值、方法)整理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!