本人简介:男
年龄:18
✍每日一句:【道固远,笃行可至;事虽巨,坚为必成】
map m;
默认构造map(const map &m);
拷贝构造#include
#include
#include
using namespace std;
void printMap(const map<int, int> m) {
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
cout << "key = " << (*it).first << " value = " << it->second << endl; //
} cout << endl;
}
//构造、赋值
void text01() {
//默认构造
map<int, int> m;
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 30));
m.insert(pair<int, int>(1, 40));
m.insert(pair<int, int>(1, 50));
printMap(m); //自动按照key值由小到大排序
//拷贝构造
map<int, int> m2(m);
printMap(m2);
//赋值操作
map<int, int> m3;
m3 = m2;
printMap(m3);
}
int main() {
text01();
return 0;
}
size();
返回容器元素数目empty();
判空swap(st);
交换两个集合容器#include
#include
#include
using namespace std;
void printMap(const map<int, int> m) {
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
cout << "key = " << (*it).first << " value = " << it->second << endl; //
} cout << endl;
//cout << "------------------------------------------------------" << endl;
}
//大小、交换
void text02() {
map<int, int> m1;
m1.insert(pair<int, int>(1, 10));
m1.insert(pair<int, int>(2, 15));
m1.insert(pair<int, int>(3, 25));
m1.insert(pair<int, int>(4, 30));
cout << "m1容器大小为: " << m1.size() << endl;
if (m1.empty()) cout << "m容器为空" << endl;
else cout << "m1容器不为空" << endl;
map<int, int> m2;
m2.insert(pair<int, int>(5, 40));
m2.insert(pair<int, int>(4, 50));
m2.insert(pair<int, int>(8, 60));
m2.insert(pair<int, int>(6, 70));
cout << "交换前: " << endl;
cout << "m1为: " << endl; printMap(m1); cout << "m2为: " << endl; printMap(m2);
m1.swap(m2);
cout << "交换后: " << endl;
cout << "m1为: " << endl; printMap(m1); cout << "m2为: " << endl; printMap(m2);
}
int main(){
text02();
return 0;
}
insert(elem);
插入元素clear();
清空erase(pos);
删除迭代器所指的元素,并返回下一个迭代器erase(beg, end);
删除区间[beg, end)的所有元素,返回下一个元素的迭代器erase(key);
删除容器中值为key的元素#include
#include
#include
using namespace std;
void printMap(const map<int, int> m) {
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
cout << "key = " << (*it).first << " value = " << it->second << endl; //
} cout << endl;
//cout << "------------------------------------------------------" << endl;
}
void text03() {
map<int, int> m;
//插入
//第一种pair()
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
//第二种make_pair
m.insert(make_pair(5, 50));
//第三种::value_type
m.insert(map<int, int>::value_type(6, 60));
//第四种[]
m[7] = 70;
printMap(m);
//删除
m.erase(3); //删除为key元素的值
printMap(m);
m.erase(++ m.begin() , m.end());
printMap(m);
m.clear();
printMap(m);
}
int main(){
text03();
return 0;
}
find(key);
查找key是否存在
count(key);
统计key的个数#include
#include
#include
using namespace std;
void printMap(const map<int, int> m) {
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
cout << "key = " << (*it).first << " value = " << it->second << endl; //
} cout << endl;
//cout << "------------------------------------------------------" << endl;
}
void text04() {
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.insert(make_pair(4, 40));
m.insert(make_pair(4, 40));
//查找
map<int, int>::iterator pos = m.find(3);
if (pos != m.end()) cout << "查找到了" << endl;
else cout << "未查找到" << endl;
//统计
int cnt = m.count(4);
cout <<"key为4的个数为:" << cnt << endl; //不允许插入重复key
}
#include
#include
#include
using namespace std;
class cmp {
public:
bool operator()(int x, int y)const{
return x > y;
}//从大到小排
};
void text05() {
map<int, int, cmp> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(4, 40));
m.insert(make_pair(3, 30));
m.insert(make_pair(2, 20));
for (map<int, int, cmp>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key = " << (*it).first << " value = " << it->second << endl;
}
}
如果感觉这篇文章对你有帮助的话,不妨三连支持下,十分感谢(✪ω✪)。
printf("点个赞吧*^*");
cout << "收藏一下叭o_o";
System.out.println("评论一下吧^_^");
print("关注一下叭0-0")