STL------map容器的基本操作

#include<iostream>
#include<map>

using namespace std;

//map容器:
/*
Map的特性是,所有元素都會根據元素的鍵值自動排序。
Map所有的元素都是pair,同時擁有實值和鍵值,
pair的第一元素被視爲鍵值,第二元素被視爲實值,map不允許兩個元素有相同的鍵值。
*/

/*
 map構造函數
map<T1, T2> mapTT;//map默認構造函數:
map(const map &mp);//拷貝構造函數
*/
/*
map賦值操作
map& operator=(const map &mp);//重載等號操作符
swap(mp);//交換兩個集合容器
*/
void test01()
{
	map<int, int> m;
	//插入:
	//插入方式4種:
	//第一種:
	m.insert(pair < int, int>(1, 10));

	//第二種:(推薦)
	m.insert(make_pair(2, 20));
	
	//第三種:
	m.insert(map<int, int>::value_type(3, 40));

	//第四種:
	m[4] = 40;
	//對於第4種插入方式,會有不安全的隱患;
	//可以通過這種方式去訪問value;

	//打印:
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "key=" << it->first << "    value=" << it->second << endl;;
	}
	cout << endl;
}

/*
map大小操作
size();//返回容器中元素的數目
empty();//判斷容器是否爲空
map刪除操作
clear();//刪除所有元素
erase(pos);//刪除pos迭代器所指的元素,返回下一個元素的迭代器。
erase(beg,end);//刪除區間[beg,end)的所有元素 ,返回下一個元素的迭代器。
erase(keyElem);//刪除容器中key爲keyElem的對組。
map查找操作
find(key);//查找鍵key是否存在,若存在,返回該鍵的元素的迭代器;/若不存在,返回map.end();
count(keyElem);//返回容器中key爲keyElem的對組個數。對map來說,要麼是0,要麼是1。對multimap來說,值可能大於1。
lower_bound(keyElem);//返回第一個key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一個key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key與keyElem相等的上下限的兩個迭代器。
*/
void test02()
{
	map<int, int> m;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));

	m.erase(1);

	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "key=" << it->first << "    value=" << it->second << endl;;
	}
	cout << endl;

	map<int, int>::iterator pos = m.find(1);
	if (pos != m.end()) {
		cout << "找到了,value是:" << pos->second << endl;
	}
	else {
		cout << "未找到" << endl;
	}
	pos = m.find(2);
	if (pos != m.end()) {
		cout << "找到了,value是:" << pos->second << endl;
	}
	else {
		cout << "未找到" << endl;
	}

	//count(keyElem);//返回容器中key爲keyElem的對組個數。對map來說,要麼是0,要麼是1。對multimap來說,值可能大於1。
	int num = m.count(2);
	cout << "num= " << num << endl;

	/*
	lower_bound(keyElem);//返回第一個key>=keyElem元素的迭代器。
	upper_bound(keyElem);//返回第一個key>keyElem元素的迭代器。
	equal_range(keyElem);//返回容器中key與keyElem相等的上下限的兩個迭代器。
	*/

	map<int, int >::iterator it_1 = m.lower_bound(3);
	if (it_1 != m.end()) {
		cout << "符合要求是值是: " << it_1->second << endl;
	}
	map<int, int>::iterator it_2 = m.upper_bound(3);
	if (it_2 != m.end()) {
		cout << "符合要求是值是: " << it_2->second << endl;
	}
	pair<map<int,int>::iterator, map<int,int>::iterator> it_3 = m.equal_range(3);
	if (it_3.second!= m.end()) {
		cout << "下限是:" << it_3.first->second;//first是下限;
		cout << "     上限是:" << it_3.second->second;//second是上限;
		cout << endl;
	}
}

//指定排序規則:
//仿函數:
class myCompare
{
public:
	bool operator()(const int & val1, const int & val2)
	{
		return val1 > val2;
	}
};

void test03()
{
	map<int, int,myCompare> m;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));

	for (map<int, int, myCompare>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "key=" << it->first << "    value=" << it->second << endl;;
	}
	cout << endl;
}


int main()
{
	test01();
	test02();
	test03();

	system("pause");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章