template <class Key, class T, class Comp = less<Key>,
class Allocator = allocator<Key>>
class multimap
#include <map>
using namespace std;
multimap <Key, T, Comp> 変数名;
multimap<int, string> x; // 空の multimap,昇順
multimap<int, string, greater<int>> x; // 空の multimap,降順
multimap<int, string> x(it1, it2); // it1 から it2 の範囲で初期化
multimap<int, string> x(y); // multimap y で初期化
multimap<int, string> x ({{1, "abc"}, {2, "efg"}}); // 初期化子リスト,C++11
multimap<int, string> x {{1, "abc"}, {2, "efg"}}; // 初期化子リスト,C++11
multimap<int, string> x = {{1, "abc"}, {2, "efg"}}; // 初期化子リスト,C++11
multimap<int, string> mp {{1, "abc"}, {3, "efg"}};
mp.emplace(0, "xxx");
mp.emplace(make_pair(3, "yyy"));
mp.insert(make_pair(2, "xyz"));
for (auto x : mp)
cout << " (" << x.first << ", " << x.second << ")";
cout << endl;
= == < <= != > >=
#include <iostream>
#include <map>
using namespace std;
void print(string str, multimap<string, int, greater<string> > &m) {
if (m.empty())
cout << " コンテナ " << str << " は空です\n";
else {
cout << str << " の要素数: " << m.size() << endl;
for (auto x : m)
cout << " " << x.first << " " << x.second;
// multimap<string, int, greater<string> >::iterator it;
// for (it = m.begin(); it != m.end(); it++)
// cout << " " << (*it).first << " " << (*it).second;
cout << endl;
}
}
int main()
{
// 要素を追加(サイズは自動的に増加)
cout << "**初期設定**\n";
multimap<string, int, greater<string>> m1; // キーが string,値が int,二項関数オブジェクト greater
m1.insert(pair<string, int>("kato", 80));
m1.insert(pair<string, int>("sato", 50));
m1.insert(pair<string, int>("sato", 30));
m1.insert(pair<string, int>("sato", 70));
m1.insert(pair<string, int>("kato", 90));
print("m1", m1);
// 2 番目の要素の前に要素を追加
cout << "**2 番目の要素の前に要素を追加**\n";
multimap<string, int, greater<string> >::iterator it = m1.begin();
it++;
m1.insert(it, pair<string, int>("suzuki", 20));
print("m1", m1);
// 2 番目の要素,及び,要素 kato を削除
cout << "**2 番目の要素,及び,要素 kato を削除**\n";
it = m1.begin();
it++;
m1.erase(it);
m1.erase("kato");
print("m1", m1);
// キー suzuki の値
cout << "**キー suzuki の値**\n";
it = m1.find("suzuki");
cout << "検索: " << it->second << endl;
// キー sato の要素
cout << "**キー sato の要素**\n";
int n = m1.count("sato");
cout <<" sato の検索: 数 = " << n << endl;
for (it = m1.lower_bound("sato"); it != m1.upper_bound("sato"); it++)
cout << " " << (*it).first << " " << (*it).second;
cout << endl;
// 交換
cout << "**交換**\n";
multimap<string, int, greater<string>> m2 = {{"aoki", 30}, {"yamada", 0}};
print("入れ替え前:m1", m1);
print("入れ替え前:m2", m2);
m1.swap(m2);
print("m1", m1);
print("m2", m2);
// 演算子で比較
cout << "**比較++\n";
m1.emplace("suzuki", 20);
m1.emplace("sato", 30);
m1.emplace("sato", 70);
m2.emplace("yamada", 0);
m2.emplace("aoki", 30);
print("m1", m1);
print("m2", m2);
if (m1 == m2)
cout << " 2 つのコンテナ内の要素はすべて等しい\n";
// m1 のすべての要素を削除
cout << "**m1 のすべての要素を削除**\n";
m1.clear();
print("m1", m1);
return 0;
}
**初期設定** m1 の要素数: 5 sato 50 sato 30 sato 70 kato 80 kato 90 **2 番目の要素の前に要素を追加** m1 の要素数: 6 suzuki 20 sato 50 sato 30 sato 70 kato 80 kato 90 **2 番目の要素,及び,要素 kato を削除** m1 の要素数: 3 suzuki 20 sato 30 sato 70 **キー suzuki の値** 検索: 20 **キー sato の要素** sato の検索: 数 = 2 sato 30 sato 70 **交換** 入れ替え前:m1 の要素数: 3 suzuki 20 sato 30 sato 70 入れ替え前:m2 の要素数: 2 yamada 0 aoki 30 m1 の要素数: 2 yamada 0 aoki 30 m2 の要素数: 3 suzuki 20 sato 30 sato 70 **比較++ m1 の要素数: 5 yamada 0 suzuki 20 sato 30 sato 70 aoki 30 m2 の要素数: 5 yamada 0 suzuki 20 sato 30 sato 70 aoki 30 2 つのコンテナ内の要素はすべて等しい **m1 のすべての要素を削除** コンテナ m1 は空です
#include <iostream>
#include <map>
#include <string>
using namespace std;
class Test {
public:
int n1, n2;
Test (int m1, int m2) {
n1 = m1;
n2 = m2;
}
};
// 二項関数オブジェクト
class GT
{
public:
bool operator() (Test t1, Test t2) const
{
return (t1.n1+t1.n2 > t2.n1+t2.n2) ? true : false;
}
};
// < 演算子のオーバーロード
//bool operator< (Test t1, Test t2)
//{
// return (t1.n1+t1.n2 > t2.n1+t2.n2) ? true : false;
//}
int main()
{
Test t1(20, 30), t2(30, 10), t3(40, 50), t4(30, 10), t5(30, 40);
// 二項関数オブジェクトを利用する場合
multimap<Test, string, GT> s1 = {{t1, "abc"}, {t2, "kkk"}, {t2, "kkk"}, {t3, "xyz"}}, s2;
// < 演算子のオーバーロードを利用する場合
// multimap<Test, string> s1 = {{t1, "abc"}, {t2, "kkk"}, {t2, "kkk"}, {t3, "xyz"}}, s2;
cout << "**s1**\n";
for (auto x : s1)
cout << " (" << (x.first).n1 << ", " << (x.first).n2 << ") " << x.second << endl;
// merge
s2 = {{t4, "xxx"}, {t5, "yyy"}};
cout << "**s2**\n";
for (auto x : s2)
cout << " (" << (x.first).n1 << ", " << (x.first).n2 << ") " << x.second << endl;
s1.merge(s2);
cout << "**after merge s1**\n";
if (s1.empty())
cout << " s1 は空です\n";
else {
for (auto x : s1)
cout << " (" << (x.first).n1 << ", " << (x.first).n2 << ") " << x.second << endl;
}
cout << "**after merge s2**\n";
if (s2.empty())
cout << " s2 は空です\n";
else {
for (auto x : s2)
cout << " (" << (x.first).n1 << ", " << (x.first).n2 << ") " << x.second << endl;
}
return 0;
}
**s1** (40, 50) xyz (20, 30) abc (30, 10) kkk (30, 10) kkk **s2** (30, 40) yyy (30, 10) xxx **after merge s1** (40, 50) xyz (30, 40) yyy (20, 30) abc (30, 10) kkk (30, 10) kkk (30, 10) xxx **after merge s2** s2 は空です
| 菅沼ホーム | 本文目次 | 演習問題解答例 | 付録目次 | 索引 |