【STL】map容器

1、map容

  • map中所有元素都是pair组成
  • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
  • 所有元素都会根据元素的键值自动 排序

map/multimap属于关联式容器,底层结构由二叉树实现。
优点:可以根据key值快速找到value值。
map容器不允许容器中有重复的key值,而multimap允许重复Key。

2、map构造和赋值

  • mapmp;默认构造
  • map(const map &mp);拷贝构造
void printMap(map<int, char>& m) {
	for (map<int, char>::iterator it = m.begin(); it != m.end(); it++) {
		cout <<"key=" << it->first << " value =" <<it->second<< endl;
	}
}
void test02() {
	map<int,char>m;
	m.insert(pair<int,char>(1, 'a'));
	m.insert(pair<int,char>(3, 'c'));
	m.insert(pair<int,char>(2, 'b'));
	printMap(m); //会按照key自动排序,由小到大
	map<int, char>m1(m);
	map<int, char>m2;
	m2 = m1;
	printMap(m2);
}

3、map大小和交换

  • size()
  • empty()
  • swap()
map<int,char>m;
	m.insert(pair<int,char>(1, 'a'));
	m.insert(pair<int,char>(3, 'c'));
	m.insert(pair<int,char>(2, 'b'));
	printMap(m); //会按照key自动排序,由小到大
	if (!m.empty()) {
		cout << m.size() << endl;
		cout << sizeof(m) << endl;
	}
	map<int, char>m1;
	m1.insert(pair<int, char>(2, '3'));
	m1.insert(pair<int, char>(4, 'c'));
	m1.insert(pair<int, char>(6, 'q'));
	m.swap(m1);
	printMap(m1);

4、map插入和删除

  • insert();
  • clear();
  • erase(pos);
  • erase(beg,end);
  • erase(key);
	map<int,char>m;
	m.insert(pair<int,char>(1, 'a'));
	m.insert(pair<int,char>(3, 'c'));
	m.insert(pair<int,char>(2, 'b'));  //四种插入方式
	m.insert(make_pair(4, 'd'));
	m.insert(map<int,char>::value_type(5,'e'));
	m[6]='f';
	cout << m[6] << endl;
	printMap(m); //会按照key自动排序,由小到大
	m.erase(m.begin());
	m.erase(2);
	m.erase(m.begin(), m.end());
	printMap(m);

5、map查找和统计

  • find(key);
  • count(key); 返回0或1
map<int, char>::iterator it = m.find(2);
cout << it->first<<it->second;
cout<<m.count(3);

6、map排序

map默认按照key值排序
1、内置数据类型排序

class CompareInt {
public:
	bool operator()(int a, int b) const{
		return a > b;
	}
};

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

void test03() {
	map<int, int, CompareInt>m;
	m.insert(pair<int,int>(2, 4));
	m.insert(pair<int,int>(5, 4));
	m.insert(pair<int, int>(1, 4));
	m.insert(pair<int, int>(3, 4));
	printMap(m);
}

2、自定义数据类型排序

class Person {
public:
	string name;
	int age;
	Person(string m_name, int m_age) {
		name = m_name;
		age = m_age;
	}
};
class Mycompare {
public:
	bool operator()(const Person& p1, const Person& p2) const {
		return p1.age > p2.age;
	}
	bool operator()(int a, int b) const {
		return a > b;
	}
};
void printMap(map<int, Person, Mycompare>& m) {
	for (map<int, Person, Mycompare>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "key=" << it->first << " value:" << " name=" << it->second.name<<" age=" << it->second.age<<endl;
	}
}



void test02() {
	map<int, Person, Mycompare>m;
	Person p1("曹操", 1);
	Person p2("曹植", 5);
	Person p3("曹冲", 2);
	Person p4("曹丕", 6);
	Person p5("曹昂", 3);
	m.insert(pair<int, Person>(p1.age, p1));
	m.insert(pair<int, Person>(p2.age, p2));
	m.insert(pair<int, Person>(p3.age, p3));
	m.insert(pair<int, Person>(p4.age, p4));
	m.insert(pair<int, Person>(p5.age, p5));
	printMap(m);
}

你可能感兴趣的:(c++,c++,算法,数据结构)