みなさん、こんにちは!私はギターを弾くのが大好きなプログラマー、AIバクテリアです。私
热爱AI、热爱分享、热爱开源
!このブログは私の要約であり、学習についての考察です。あなたも深度学习、机器视觉、算法、Python、C++
興味があるなら、あなたは私のダイナミックに集中することができます、私たちは一緒に学び、一緒に進歩します-
私のブログのアドレスは次のとおりです:[AI]ブログのバクテリア
私のGithubプロジェクトのアドレスは:[バクテリアのGithubの]
目次
1. STLマップクラスの概要
マップとマルチマップは、データ構造のディクショナリクラスと同様に、キーに基づく検索をサポートするキーと値のペアのコンテナーです。
どちらのマップとmultimupはテンプレートクラスである。その差はマルチマップを保存できるということである重複旧缶のみを格納している間、キーをユニークなキーを。
高速検索を実現するために、マップとマルチマップの内部構造はバイナリツリーのように見えます。つまり、マップとマルチマップに要素を挿入すると、要素が並べ替えられます。また、他の要素を使用して、ベクトルなどの特定の位置にある要素を置き換えることはできません。マップ内の特定の位置にある要素を、異なる値を持つ新しい要素に置き換えることはできません。これは、マップが新しい要素をバイナリツリーの他の要素と比較するためです。 、それを別の場所に置きます。
さらに、STLマップおよびマルチマップクラスを使用する場合は、ヘッダーファイルをインクルードする必要があることに注意してください。
# include<map>
2.マップおよびマルチマップクラスの基本操作
2.1インスタンス化操作
典型的なマップのインスタンス化構文は次のとおりです。
map <keyType, valueType, Predicate=std::less <keyType> > mapObject;
multimap <keyType, valueType, Predicate=std::greater <keyType> >mmapObject;
- 最初のパラメーターkeyTypeはキーのタイプを表します
- 2番目のパラメーターvalueTypeは値のタイプを表します
- 3番目のパラメーターless / greaterは、ソート基準として使用されます。小さいほど昇順でソートされ、大きいほど降順でソートされます。
以下は、いくつかの異なるメソッドを使用して、キーと値のペア{string、int}型のマップまたはマルチマップをインスタンス化します。
#include <iostream>
#include <map>
#include <string>
using namespace std;
template<typename KeyType>
int main()
{
// 方法1
map<int, string> map1;
multimap<int, string> mmap1;
// 方法2
map<int, string> map2(map1);
multimap<int, string> mmap2(mmap1);
// 方法3
map<int, string> map3(map1.cbegin(), map1.cend());
multimap<int, string> mmap3(mmap1.cbegin(), mmap1.cend());
return 0;
}
2.2 2つの並べ替え方法
マップとマルチマップをインスタンス化するプロセスで、ソート述語が指定されていない場合、マップはデフォルトで要素を昇順でソートします。
マップのソート基準をカスタマイズするには、次の2つの方法を使用できます。
(1)ソート基準を決定するために、マップクラスによって提供される述語の少ない方と多い方を使用します
// 1.升序
map<int, string, less<int>> map1;
// 2.降序
multimap<int, string, greater<int>> mmap1;
(2)カスタム述語はソート基準として使用されます
#include <iostream>
#include <map>
#include <string>
using namespace std;
template<typename KeyType>
// 自定义词谓
struct ReverseSort
{
bool operator()(const KeyType& key1, const KeyType& key2)
{
return (key1 > key2);
}
};
int main()
{
// 实例化
map<int, string> map1;
multimap<int, string> mmap1;
// 降序排列
map<int, string, ReverseSort<int>> map5(map1.cbegin(), map1.cend());
multimap<int, string, ReverseSort<int>> mmap5(mmap1.cbegin(), mmap1.cend());
return 0;
}
3.マップおよびマルチマップクラスのメンバー関数
2.1要素挿入関数insert()
マップメンバー関数insert()を使用すると、新しいキーと値のペアをマップに挿入できます。以下は、4つの一般的な挿入方法を示しています。
#include <iostream>
#include <map>
#include <string>
using namespace std;
template <typename T>
// 显示内容
void DisplayContents(const T& Input)
{
for(auto iElement = Input.cbegin(); iElement != Input.cend(); ++iElement)
cout<<iElement->first<<" "<<iElement->second<<endl;
cout<<endl;
}
int main()
{
map<int, string, less<int>>map1;
// 插入方法1
map1.insert(map<int, string>::value_type(3, "Three"));
// 插入方法2
map1.insert(make_pair(1, "One"));
// 插入方法3
map1.insert(pair<int, string>(10, "Ten"));
// 插入方法4,采用类似于数组的语法进行插入。
map1[8] = "Eight";
cout<<"键 :值"<<endl;
DisplayContents(map1);
return 0;
}
クラスを4回挿入すると、以下に示すように、マップには4つのキーと値のペアがあります。
2.2要素関数find()を見つける
mapおよびmultimapは、メンバー関数find()を提供します。これは、指定されたkeyに基づいて値を検索できます。
以下に示すように、find()が返すのはイテレータです。
map1 <int, string>::const_iterator iFound = map1.find(Key);
find()を使用して要素を検索する場合、最初にイテレータをチェックしてfind()が成功したことを確認し、次にそれを使用して見つかった値にアクセスします。
if(iFound != map1.end())
{
cout<<"键 :值"<<endl;
cout<<iFound->first<<" "<<iFound->second<<endl;
}
else
cout<<"Key不存在于map中!"<<endl;
以下は、メンバー関数find()を使用してmap1のキーに対応する値を見つける実際的な例を示しています。
#include <iostream>
#include <map>
#include <string>
using namespace std;
template <typename T>
// 显示内容
void DisplayContents(const T& Input)
{
for(auto iElement = Input.cbegin(); iElement != Input.cend(); ++iElement)
cout<<iElement->first<<" "<<iElement->second<<endl;
cout<<endl;
}
int main()
{
map<int, string, less<int>>map1;
// 插入元素
map1.insert(make_pair(8, "Eight"));
map1.insert(make_pair(1, "One"));
map1.insert(make_pair(6, "Six"));
map1.insert(make_pair(10, "Ten"));
// 显示键值
cout<<"键 :值"<<endl;
DisplayContents(map1);
// 根据键查找其对应的值
auto iFound = map1.find(8);
if(iFound != map1.end())
cout<<iFound->first<<"对应的值是:"<<iFound->second<<endl;
else
cout<<"Key不存在于map1中!"<<endl;
return 0;
}
結果は次のとおりです。
マルチマップを使用している場合、コンテナには同じキーを持つ複数のキーと値のペアが含まれる可能性があるため、指定されたキーに対応するすべての値を見つける必要があります。したがって、最初にcount()を使用して、指定されたキーに対応する値の数を決定し、次にイテレーターをインクリメントして、対応するすべての値にアクセスします。
次のマップには3つの同一のKey-Valueペアがあります。この行では、Key-Valueペアを見つけて順番に出力する必要があります。
#include <iostream>
#include <map>
#include <string>
using namespace std;
template <typename T>
// 显示内容
void DisplayContents(const T& Input)
{
for(auto iElement = Input.cbegin(); iElement != Input.cend(); ++iElement)
cout<<iElement->first<<" "<<iElement->second<<endl;
cout<<endl;
}
int main()
{
multimap<int, string, less<int>>map1;
// 插入元素
map1.insert(make_pair(8, "Eight"));
map1.insert(make_pair(1, "One"));
map1.insert(make_pair(8, "Eight"));
map1.insert(make_pair(8, "Eight"));
// 显示键值
cout<<"键 :值"<<endl;
DisplayContents(map1);
// 根据键查找其所有对应的值
auto iFound = map1.find(8);
if(iFound != map1.end())
{
// 统计multimap中键8出现的次数
size_t num = map1.count(8);
for(size_t nCounter=0; nCounter<num; ++nCounter)
{
cout<<"Key: "<<iFound->first<<", Val["<<nCounter<<"]="<<iFound->second<<endl;
++iFound;
}
}
else
cout<<"Element Not Found In The Map!";
return 0;
}
演算結果:
2.3要素削除関数erase()
mapおよびmultimapは、コンテナー内の要素を削除するために使用されるメンバー関数erase()を提供します。主な方法は3つあります。
- キーをパラメーターとして使用すると、指定したキーを含むすべてのキーと値のペアが削除されます
map1.erase(key);
- イテレーターをパラメーターとして、イテレーターが指すパラメーターを削除します。
map1.erase(iFound);
- イテレータを使用して境界を指定し、指定された範囲内のすべての要素を削除します
map1.erase(iLowerBound, iUpperBound);
以下は、上記の3つの方法の使用の実際的なデモンストレーションです。
#include <iostream>
#include <map>
#include <string>
using namespace std;
template <typename T>
// 显示内容
void DisplayContents(const T& Input)
{
for(auto iElement = Input.cbegin(); iElement != Input.cend(); ++iElement)
cout<<iElement->first<<" "<<iElement->second<<endl;
cout<<endl;
}
int main()
{
multimap<int, string, less<int>>map1;
// 插入元素
map1.insert(make_pair(8, "Eight"));
map1.insert(make_pair(1, "One"));
map1.insert(make_pair(6, "Six"));
map1.insert(make_pair(10, "Ten"));
map1.insert(make_pair(15, "Fifteen"));
// 显示键值
cout<<"键 :值"<<endl;
DisplayContents(map1);
// 删除键1对应的键值对
map1.erase(1);
cout<<"删除键1对应的键值对后:"<<endl;
DisplayContents(map1);
// 删除迭代器指向的元素
auto iFound = map1.find(8);
map1.erase(iFound);
cout<<"删除键8对应的键值对后:"<<endl;
DisplayContents(map1);
// 删除指定范围内(6至10)的元素
map1.erase(map1.lower_bound(6), map1.lower_bound(15));
cout<<"删除指定范围内(从6开始,到15之前)的元素:"<<endl;
DisplayContents(map1);
return 0;
}
演算結果:
本日の共有は終了しました。勉強に役立ててください。
最後に、幸運にもCSDNオリジナルブログコンテストTop50に選ばれました。私に投票していただければ、私の作品への最大の励みになります。投票後も公式抽選に参加できます!
投票チャネル:CSDNオリジナルブログコンテスト