mapの使い方----------------C++
map–一般的なSTLコンテナのマッピング
mapコンテナ内の要素へのアクセス
1、下付き文字でアクセスする例:mapmp;mp[‘c’]=20;
2、反復器によるアクセス
it同時アクセスキーと値mapではit->firstアクセスキー、it->secondアクセス値を使用できます
注意:mapはキーが小さい順に自動的にソートされます
map共通関数の例
1、find()
//find(key)戻りキーがkeyのマッピング反復器、時間複雑度がO(logn)、Nがmapでマッピングされた個数
2、erase()
2つの使い方:1つの要素を削除し、1つの区間内の左右の要素を削除します.
1)、個々の要素mapを削除する.Erase(it),itは要素を削除する必要がある反復器であり,時間的複雑度はO(1)である.
map.erase(key),keyはマッピングを削除しようとするキーであり,時間的複雑度はO(logn),Nはmap内の要素の個数である.
2)、1区間の全ての要素mpを削除する.erase(first,last),firstは削除が必要な区間開始反復器,lastは削除が必要な区間の末尾反復器の次のアドレス,すなわち左閉右開[first,last),時間複雑度O(last-first)を削除する
3、size()
size()はmapにマッピングされた対数,時間複雑度O(1)を得るために用いられる.
4、clear()
clear()はmap中のすべての要素をクリアするために使用され、複雑度O(N)、Nはmap中の要素の個数である.
mapの一般的な用途
1、文字(または文字列)と整数のマッピングを確立する必要があるテーマで、mapを使ってコード量を減らすことができる.
2、大きい整数や他のタイプのデータが存在するかどうかを判断するテーマは、mapをbool配列として用いることができる
3、文字列と文字列のマッピングも発生する可能性がある
注意:
mapキーと値は一意で、1つのキーに複数の値が必要な場合はmultimapしか使用できません.
#include
using namespace std;
//map
map<typename1,typename2>mp;// ( key), ( value)
//
map<string,int>mp;
// set
map<set<int>,string>mp;
mapコンテナ内の要素へのアクセス
1、下付き文字でアクセスする例:mapmp;mp[‘c’]=20;
#include
#include
using namespace std;
int main(){
map<char,int>mp;
mp['c']=20;
mp['c']=30;//20
printf("%d
",mp['c']);// 30
return 0;
}
2、反復器によるアクセス
// STL
map<typename1,typename2>::iterator it;
it同時アクセスキーと値mapではit->firstアクセスキー、it->secondアクセス値を使用できます
#include
#include
using namespace std;
int main(){
map<char,int>mp;
mp['m']=20;
mp['r']=30;
mp['a']=40;
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++){
printf("%c %d
",it->first,it->second);
}
return 0;
}
注意:mapはキーが小さい順に自動的にソートされます
map共通関数の例
1、find()
//find(key)戻りキーがkeyのマッピング反復器、時間複雑度がO(logn)、Nがmapでマッピングされた個数
#include
#include
using namespace std;
int main(){
map<char,int>mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int>::iterator
it=mp.find('b');
printf("%c %d
",it->first,it->second);
return 0;
}
2、erase()
2つの使い方:1つの要素を削除し、1つの区間内の左右の要素を削除します.
1)、個々の要素mapを削除する.Erase(it),itは要素を削除する必要がある反復器であり,時間的複雑度はO(1)である.
#include
#include
using namespace std;
int main(){
map<char,int>mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int>::iterator it=mp.find('b');
mp.erase(it);
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++){
printf("%c %d
",it->first,it->second);
}
return 0;
}
map.erase(key),keyはマッピングを削除しようとするキーであり,時間的複雑度はO(logn),Nはmap内の要素の個数である.
#include
#include
using namespace std;
int main(){
map<char,int>mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
mp.erase('b');// b , b 2
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++){
printf("%c %d
",it->first,it->second);
}
return 0;
}
2)、1区間の全ての要素mpを削除する.erase(first,last),firstは削除が必要な区間開始反復器,lastは削除が必要な区間の末尾反復器の次のアドレス,すなわち左閉右開[first,last),時間複雑度O(last-first)を削除する
#include
#include
using namespace std;
int main(){
map<char,int>mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int>::iterator it=mp.find('b');
mp.erase(it,mp.end()); // it , b 2,c 3
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++){
printf("%c %d
",it->first,it->second);
}
return 0;
}
3、size()
size()はmapにマッピングされた対数,時間複雑度O(1)を得るために用いられる.
#include
#include
using namespace std;
int main(){
map<char,int>mp;
mp['a']=10;
mp['b']=20;
mp['c']=30;
printf("%d
",mp.size());//3
return 0;
}
4、clear()
clear()はmap中のすべての要素をクリアするために使用され、複雑度O(N)、Nはmap中の要素の個数である.
#include
#include
using namespace std;
int main(){
map<char,int>mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
mp.clear(); // map
printf("%d
",mp.size());
return 0;
}
mapの一般的な用途
1、文字(または文字列)と整数のマッピングを確立する必要があるテーマで、mapを使ってコード量を減らすことができる.
2、大きい整数や他のタイプのデータが存在するかどうかを判断するテーマは、mapをbool配列として用いることができる
3、文字列と文字列のマッピングも発生する可能性がある
注意:
mapキーと値は一意で、1つのキーに複数の値が必要な場合はmultimapしか使用できません.