map容器元素的三種插入方式
STL中的map容器是我經常用的,但是因為map跟別的容器不太一樣,每次用的時候對於map中元素的插入方式總是忘卻,故而發篇博文,提醒我也提醒所有人map容器的三種插入方式:
第一種:用insert函式插入pair資料。下面舉例說明:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<end;
}
}
第二種:用insert函式插入value_type資料,下面舉例說明:
#include <map>
#include <string>
#include <iostream>
using namespace std;
Int main()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1, "student_one"));
mapStudent.insert(map<int, string>::value_type (2, "student_two"));
mapStudent.insert(map<int, string>::value_type (3, "student_three"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<end;
}
}
第三種:用陣列方式插入資料,下面舉例說明:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[2] = "student_two";
mapStudent[3] = "student_three";
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<end;
}
}
以上三種用法,雖然都可以實現資料的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完成一樣的,用insert函式插入資料,在資料的插入上涉及到集合的唯一性這個概念,即當map中有這個關鍵字時,insert操作是插入資料不了的,但是用陣列方式就不同了,它可以覆蓋以前該關鍵字對應的值,用程式說明:
mapStudent.insert(map<int, string>::value_type (1, "student_one"));
mapStudent.insert(map<int, string>::value_type (1, "student_two"));
上面這兩條語句執行後,map中1這個關鍵字對應的值是“student_one”,第二條語句並沒有生效,那麼這就涉及到我們怎麼知道insert語句是否插入成功的問題了,可以用pair來獲得是否插入成功,程式如下
pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));
我們通過pair的第二個變數來知道是否插入成功,它的第一個變數返回的是一個map的迭代器,如果插入成功的話Insert_Pair.second應該是true的,否則為false。
下面給出完成程式碼,演示插入成功與否問題:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));
if(Insert_Pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
else
{
cout<<"Insert Failure"<<endl;
}
Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));
if(Insert_Pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
else
{
cout<<"Insert Failure"<<endl;
}
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<end;
}
}
大家可以用如下程式,看下用陣列插入在資料覆蓋上的效果
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[1] = "student_two";
mapStudent[2] = "student_three";
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<end;
}
}
相關推薦
map容器元素的三種插入方式
STL中的map容器是我經常用的,但是因為map跟別的容器不太一樣,每次用的時候對於map中元素的插入方式總是忘卻,故而發篇博文,提醒我也提醒所有人map容器的三種插入方式: 第一種:用insert函式插入pair資料。下面舉例說明: #include <ma
資料結構之頭結點連結串列的三種插入方式(頭插法,尾插法,在pos處插入)
建立頭結點 流程:首先建立頭結點表指標併為其分配空間——並將頭結點指向空,防止出現段錯誤。 程式碼: //建立頭結點 Node* Create_List () { //建立頭結點 Node* list = (Node*) malloc(
mybatis3.1-[topic-18-20]-_對映檔案_引數處理_單個引數&多個引數&命名引數 _POJO&Map&TO 三種方式及舉例
筆記要點出錯分析與總結 /**MyBatis_對映檔案_引數處理_單個引數&多個引數&命名引數 * _POJO&Map&TO 三種方式及舉例 _ * 單個引數 : #{引數名} ,取出引數值; [mybatis 不會做特殊處理]
Spring框架:啟動IOC容器的三種方式
前期準備工作 使用IDE:IDEA 建立專案時需要用到的框架有Java和Web 就像下圖的進行勾選。 建立專案完成之後的目錄 如下圖所示。 建立專案時,gradle中用到的依賴: // Java EE 通過本地Jar包
springboot物件注入IOC容器的三種方式
Springboot會自動載入resources檔案下面的application.yml或者application.properties配置檔案,因為yml格式可以替代xml格式,功能properties更強大,所以一般都使用yml格式進行書寫。 1.對於yml中載入了的b
Map集合的三種遍歷方式
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class
陣列元素的三種表示方式
1. 一維陣列 int a[8], *pa = a; 表示方式 0 1 2 3 4 5 6 7 下標變數 a[0] a[1] a[2]
遍歷Map集合的三種方式
package Day17; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; imp
spring ioc容器結構初始化步驟以及bean生命週期和三種建立方式
建立物件的三種方式: 1、採用預設的建構函式建立 2、採用靜態工廠方法 1、寫一個靜態工廠方法類 public class HelloWorldFactory {
C++函數的三種傳遞方式為:值傳遞、指針傳遞和引用傳遞
否則 方式 指針 指向 out 數據結構 logs 形參 使用 C++函數的三種傳遞方式為:值傳遞、指針傳遞和引用傳遞 值傳遞: void fun(int x){ x += 5; //修改的只是y在棧中copy x,x只是y的一個副本,在內存中重新開辟的一塊臨時空間把y
以下三種下載方式有什麽不同?如何用python模擬下載器下載?
get 瀏覽器 技術分享 ref tle port net class 但是 問題始於一個鏈接https://i1.pixiv.net/img-zip-...這個鏈接在瀏覽器打開,會直接下載一個不完整的zip文件 但是,使用下載器下載卻是完整文件 而當我嘗試使用py
sql註入過程中後臺數據庫類型的三種判斷方式
sql註入 安全測試 數據庫類型判斷 後臺數據庫類型判斷:一、通過頁面返回的報錯信息,一般情況下頁面報錯會顯示是什麽數據庫類型,在此不多說;二、通過各個數據庫特有的數據表來判斷: 1、mssql數據庫 http://127.0.0.1/test.php?id=1 and (sele
Python 函數的三種定義方式
true int 交互 方式 邏輯 pre pri [0 操作 無參:應用場景僅僅只是執行一些操作,比如與用戶交互,打印有參:需要根據外部傳進來的參數,才能執行相應的邏輯,比如統計長度,求最大值最小值空函數:設計代碼結構 def my_max(x,y): if x
Java 多線程 三種實現方式
() 例子 屬於 周期性 core www object 並且 check Java多線程實現方式主要有三種:繼承Thread類、實現Runnable接口、使用ExecutorService、Callable、Future實現有返回結果的多線程。其中前兩種方式線程執行完後都
mysql讀寫分離的三種實現方式
不能 span bsp 缺點 解決方案 使用 隨機 mas 均衡 1 程序修改mysql操作類可以參考PHP實現的Mysql讀寫分離,阿權開始的本項目,以php程序解決此需求。優點:直接和數據庫通信,簡單快捷的讀寫分離和隨機的方式實現的負載均衡,權限獨立分配缺點:自己維
webdriver 的三種等待方式
輪詢 common xxx implicit delay 定義 final load 超時時間 1、顯式等待 一個顯式等待是你定義的一段代碼,用於等待某個條件發生然後再繼續執行後續代碼。 from selenium import webdriverfrom seleniu
沈浸式狀態欄的三種實現方式
stemwin barh webp trac war nba schema 布局文件 adding 沈浸式算是目前Android行業比較流行的一種App設計風格,將菜單欄北京設置為導航欄的顏色,感覺頂部狀態欄像是被入侵了一樣,因此稱為沈浸式菜單欄。本文將介紹三種方式去實現沈
JavaScript實現接口的三種經典方式
pda osi 註釋 posit 檢查 form 兩個 const simple 1 /* 2 接口:提供一種說明一個對象應該有哪些方法的手段 3 js中有三種方式實現接口: 4 1 註釋描述接口 5 2 屬性檢測接口 6
jenkins介紹部署及三種構建方式配置
構建 簡單 nbsp adding linu tle ref pass 默認 [隱藏] 1前言 1.1jenkins介紹 1.2jenkins好處 1.3我的jenkins實踐 1.4jenkins使用前提 2jenkins部署 2.1下面內容介紹 2.2環境介紹
三種繼承方式和三種訪問權限
-a prot play 9.png png -h .cn orm font 也就是說子類只能訪問父類的保護和公有成員,而對象只能訪問公有成員。繼承方式 1. 三種繼承方式不影響子類對父類的訪問權限,子類對父類只看父類的訪問控制權。 2. 繼承