C++ の連想配列クラス map は、Python のディクショナリーや Perl のハッシュと似た機能を持ち、キーと値を 1 セットとしてデータを保存することができる。ただし、ディクショナリーとハッシュとの違いとして、map は 2 分木とよばれるツリー構造で管理され、キーと値のほかに、枝分かれの情報も保持している。そのため、map は各要素のキーと値の他に、順序関係も保持している。
map クラスの基本的な使い方
map クラスの宣言は、std::map<std::string, int>
のようにして、キーと値の型を指定して宣言する。一度作成した map クラスの変数は、[]
を使って、キーと値を格納できる。
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
int main(void) {
std::map<std::string, int> seqLen;
seqLen["CGAGT"] = 5;
seqLen["TT"] = 2;
seqLen["CGATCGTGTC"] = 10;
std::cout << seqLen["TT"] << std::endl;
// 2
return 0;
}
for
文を利用して、すべてのキーと値を取得することもできる。for
文でイテレーターを繰り返すとき、イテレーターの型も指定する必要がある。ただし、C++11 以上を利用しているならば、イテレーターの型を明確に指定の代わりに auto
とすると、型が自動的に指定される。
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
int main(void) {
std::map<std::string, int> seqLen;
seqLen["CGAGT"] = 5;
seqLen["TT"] = 2;
seqLen["CGATCGTGTC"] = 10;
for (std::map<std::string, int>::iterator i = seqLen.begin(); i != seqLen.end(); ++i) {
std::cout << i->first << " => " << i->second << std::endl;
}
// CGAGT => 5
// CGATCGTGTC => 10
// TT => 2
// for C++11
for (auto i = seqLen.begin(); i != seqLen.end(); ++i) {
std::cout << i->first << " => " << i->second << std::endl;
}
// CGAGT => 5
// CGATCGTGTC => 10
// TT => 2
return 0;
}
キーの検索
キーの検索は、find
関数を利用する。
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
int main(void) {
std::map<std::string, int> seqLen;
seqLen["CGAGT"] = 5;
seqLen["TT"] = 2;
seqLen["CGATCGTGTC"] = 10;
std::map<std::string, int>::iterator itr = seqLen.find("AA");
//auto itr = seqLen.find("AA");
if (itr != seqLen.end()) {
std::cout << "found key." << std::endl;
} else {
std::cout << "key not flound." << std::endl;
}
return 0;
}
キーの削除
キーの削除は、erase
関数を利用する。
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
int main(void) {
std::map<std::string, int> seqLen;
seqLen["CGAGT"] = 5;
seqLen["TT"] = 2;
seqLen["CGATCGTGTC"] = 10;
std::map<std::string, int>::iterator itr = seqLen.find("TT");
//auto itr = seqLen.find("TT");
if (itr != seqLen.end()) {
seqLen.erase(itr);
}
return 0;
}
map クラスのメンバー関数
map クラスのメンバー関数は、ほかに size
、empty
、clear
などがある。それぞれ、size
は map クラスのオブジェクトの要素数を調べ関数である。emtpy
はオブジェクトが空かどうかを調べる関数であり、空であれば true を返し、そうでなければ false を返す。また、clear
はオブジェクトの全要素を削除する関数である。
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
int main(void) {
std::map<std::string, int> seqLen;
seqLen["CGAGT"] = 5;
seqLen["TT"] = 2;
seqLen["CGATCGTGTC"] = 10;
std::cout << seqLen.size() << std::endl;
// 3
std::cout << seqLen.empty() << std::endl;
// 0
seqLen.clear();
std::cout << seqLen.size() << std::endl;
// 0
std::cout << seqLen.empty() << std::endl;
// 1
return 0;
}
map クラスのコピー
map クラスをコピーする場合は、次のようにコピーコンストラクタを利用する。このとき、コピーで作成した map クラスのデータを書き換えても、コピー元には影響しない。
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
int main(void) {
// create seqLen
std::map<std::string, int> seqLen;
seqLen["CGAGT"] = 5;
seqLen["TT"] = 2;
seqLen["CGATCGTGTC"] = 10;
// copy seqLen to dnaSeqLen
std::map<std::string, int> dnaSeqLen(seqLen);
dnaSeqLen["TT"] = 0;
std::cout << seqLen["TT"] << std::endl;
// 2
std::cout << dnaSeqLen["TT"] << std::endl;
// 0
return 0;
}