欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

12STL-map/multimap-創(chuàng)新互聯(lián)

重新系統(tǒng)學(xué)習(xí)c++語言,并將學(xué)習(xí)過程中的知識(shí)在這里抄錄、總結(jié)、沉淀。同時(shí)希望對(duì)刷到的朋友有所幫助,一起加油哦!

成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)、網(wǎng)站制作與策劃設(shè)計(jì),濉溪網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:濉溪等地區(qū)。濉溪做網(wǎng)站價(jià)格咨詢:18980820575

每一次學(xué)習(xí)都是為了追求智慧!

寫在前面,本篇章主要介紹STL中常用容器map/multimap。

1.1 map基本概念

簡(jiǎn)介:

  • map中所有元素都為鍵值對(duì),即pair;
  • pair中第一個(gè)元素為key(鍵),起到索引作用,第二個(gè)元素為value(值);
  • map中所有元素都會(huì)跟進(jìn)元素的key進(jìn)行自動(dòng)排序。默認(rèn)升序。

本質(zhì):

? map/multimap 都屬于關(guān)聯(lián)式容器,底層結(jié)構(gòu)是用二叉樹實(shí)現(xiàn)的。

優(yōu)點(diǎn):

? 可以根據(jù)key值,快速找到value值。

map和multimap區(qū)別:

  • map不允許有重復(fù)key的元素;
  • multimap允許有重復(fù)key的元素。

1.2 map構(gòu)造和賦值

函數(shù)原型:

構(gòu)造:

  • mapmp;//map默認(rèn)構(gòu)造函數(shù):
  • map(const map &mp);? ? ? ? ??//拷貝構(gòu)造函數(shù)

賦值:

  • map& operator=(const map &mp);//重載等號(hào)操作符

示例:

#include#include#includeusing namespace std;

void printMap(const map& m) {
	for (map::const_iterator it = m.begin(); it != m.end(); it++) {
		cout<< "key = "<< (*it).first
			<< " value = "<< it->second
			<< endl;
	}
	cout<< endl;
}
void test() {
	// 創(chuàng)建map,insert插入數(shù)據(jù),默認(rèn)按key值升序排序
	// 不運(yùn)行插入重復(fù)key值
	mapm;
	m.insert(pair(1, 10));
	m.insert(pair(3, 30));
	m.insert(pair(2, 20));
	m.insert(pair(4, 40));
	m.insert(pair(4, 50));
	printMap(m);

	// 拷貝構(gòu)造
	mapm2(m);
	printMap(m2);

	// 賦值
	mapm3;
	m3 = m2;
	printMap(m3);
}

int main() {
	test();

	system("pause");
	return 0;
}

1.3 map大小和交換

函數(shù)原型:

  • size();? ? ? ? ? ?//返回容器中元素的數(shù)目
  • empty(); //判斷容器是否為空
  • swap(st);????????//交換兩個(gè)集合容器

示例:

#include#include#includeusing namespace std;

void printMap(const map& m) {
	for (map::const_iterator it = m.begin(); it != m.end(); it++) {
		cout<< "key = "<< (*it).first
			<< " value = "<< it->second
			<< endl;
	}
	cout<< endl;
}

void test() {
	mapm;
	m.insert(pair(1, 10));
	m.insert(pair(3, 30));
	m.insert(pair(2, 20));
	m.insert(pair(4, 40));
	printMap(m);

	if (m.empty()) {
		cout<< " map is empty"<< endl;
	}
	else {
		cout<< "map is not empty"<< endl;
		cout<< "map size: "<< m.size()<< endl;
	}
}

// 交換
void test2() {
	mapm;
	m.insert(pair(1, 10));
	m.insert(pair(3, 30));
	m.insert(pair(2, 20));
	m.insert(pair(4, 40));
	cout<< "map size: "<< m.size()<< endl;
	printMap(m);

	mapm2;
	m2.insert(pair(4, 40));
	m2.insert(pair(5, 50));
	m2.insert(pair(6, 60));
	cout<< "map size: "<< m2.size()<< endl;
	printMap(m2);

	m.swap(m2);

	cout<< "change:"<< endl;
	cout<< "map size: "<< m.size()<< endl;
	printMap(m);
	cout<< "map size: "<< m2.size()<< endl;
	printMap(m2);
}
int main() {
	test2();

	system("pause");
	return 0;
}

1.4 map插入和刪除

函數(shù)原型:

  • insert(elem); ????????????????//在容器中插入元素。要插入對(duì)組
  • clear(); ????????????????????????//清除所有元素
  • erase(pos); ????????????????//刪除pos迭代器所指的元素,返回下一個(gè)元素的迭代器。
  • erase(beg, end); ????????//刪除區(qū)間[beg,end)的所有元素 ,返回下一個(gè)元素的迭代器。
  • erase(key); ????????????????//刪除容器中值為key的元素。

示例:

#include#include#includeusing namespace std;

//insert(elem); //在容器中插入元素。
//clear(); //清除所有元素
//erase(pos); //刪除pos迭代器所指的元素,返回下一個(gè)元素的迭代器。
//erase(beg, end); //刪除區(qū)間[beg,end)的所有元素 ,返回下一個(gè)元素的迭代器。
//erase(key); //刪除容器中值為key的元素。
void printMap(const map& m) {
	for (auto item : m) {
		cout<< " key = "<< item.first
			<< " value = "<< item.second
			<< " "<< endl;
	}
	cout<< endl;
}

void test() {
	// map容器插入數(shù)據(jù)
	mapm;
	
	//第一種
	m.insert(pair(1, 10));

	//第二種
	m.insert(make_pair(2, 20));

	//第三種
	m.insert(map::value_type(3, 30));

	//第四種
	m[4] = 40;

	// 不建議用[]來插入數(shù)據(jù)
	// [] 主要用來通過key來訪問到value
	//cout<< m[4]<< endl;

	printMap(m);

	//刪除
	m.erase(m.begin());
	printMap(m);

	m.erase(3);//按照key來刪除
	m.erase(10);//刪除沒有key的元素,不會(huì)報(bào)錯(cuò)
	printMap(m);

	//m.erase(m.begin(), m.end());// 利用迭代器來清空
	m.clear();// 清空容器
	printMap(m);
}

int main() {
	test();

	system("pause");
	return 0;
}

1.5 map查找和統(tǒng)計(jì)

函數(shù)原型:

  • find(key);? 查找key是否存在。若存在,返回該鍵的元素的迭代器;若不存在,返回m.end();
  • count(key);? 統(tǒng)計(jì)key的元素個(gè)數(shù)。map只會(huì)有0或1,multimap有0和n。

示例:

#include#include#includeusing namespace std;

void test() {
	mapm;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(3, 40));

	map::iterator it = m.find(3);
	if (it !=m.end()) {
		cout<< " find! key = "<< (*it).first
			<< " value = "<< (*it).second
			<< endl;
	}
	else {
		cout<< "not find"<< endl;
	}

	// 統(tǒng)計(jì)
	// 由于map只能插入不可重復(fù)key值的元素,所以統(tǒng)計(jì)結(jié)果只能是 0 或 1
	// multimap 可以統(tǒng)計(jì)出 0 或n
	int n = m.count(3);
	cout<< "find key =3 times:"<< n<< endl;

	n = m.count(5);
	cout<< "find key =5 times:"<< n<< endl;
}

int main() {
	test();

	system("pause");
	return 0;
}

輸出:?

find! key = 3 value = 30
find key =3 times:1
find key =5 times:0


1.6 map容器排序

問題:

map容器默認(rèn)排序是按照key從大到小排序,那如何做到改變排序規(guī)則?

方法:

利用仿函數(shù),改變排序規(guī)則。

示例:

#include#include#includeusing namespace std;
class MyCompare {
public:
	bool operator()(int v1,int v2)const {
		return v1 >v2;
	}
};

void printMap(const map& m) {
	for (map::const_iterator it = m.begin(); it != m.end(); it++) {
		cout<< "key = "<< (*it).first
			<< " value = "<< it->second
			<< endl;
	}
	cout<< endl;
}

void test() {
	//map 默認(rèn)排序規(guī)則 從小到大
	// 利用仿函數(shù)改變排序規(guī)則 從大到小
	mapm;
	m.insert(make_pair(3, 30));
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(4, 40));
	printMap(m);
}

int main() {
	test();

	system("pause");
	return 0;
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

本文題目:12STL-map/multimap-創(chuàng)新互聯(lián)
文章源于:http://chinadenli.net/article28/dhjjcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)建站、用戶體驗(yàn)、微信小程序、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)