map以及类似指针iterator
生活随笔
收集整理的這篇文章主要介紹了
map以及类似指针iterator
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.
map的構造函數
Map<int, string> mapStudent;
2. 數據的插入
在構造 map容器后
第一種:用insert函數插入pair數據
#pragma warning (disable:4786) )
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce 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->se cond<<end;
}
}
第二種:用insert函數插入value_type數據,下面舉例說明
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce 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->se cond<<end;
}
}
第三種:用數組方式插入數據,下面舉例說明
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce 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->se cond<<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.se cond應該是true的,否則為false。
下面給出完成代碼,演示插入成功與否問題
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce 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.se cond == true)
{
Cout<<”Insert Su ccessfully”<<endl;
}
Else
{
Cout<<”Insert Failure”<<endl;
} 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
Map<int, string> mapStudent;
2. 數據的插入
在構造 map容器后
第一種:用insert函數插入pair數據
#pragma warning (disable:4786) )
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce 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->se cond<<end;
}
}
第二種:用insert函數插入value_type數據,下面舉例說明
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce 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->se cond<<end;
}
}
第三種:用數組方式插入數據,下面舉例說明
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce 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->se cond<<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.se cond應該是true的,否則為false。
下面給出完成代碼,演示插入成功與否問題
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce 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.se cond == true)
{
Cout<<”Insert Su ccessfully”<<endl;
}
Else
{
Cout<<”Insert Failure”<<endl;
} 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的map以及类似指针iterator的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++中有string类,string是
- 下一篇: var_export()函数的使用举例(