// map 模板定义
template, class Allocator = std::allocator>> class map;
namespace pmr {
template< class Key, class T, class Compare = std::less >
using map = std::map>>
}
#include
#include
#include
#include
输出结果:
m1 :
m2 :
m3 :
m4 : 2 - 2 3 - 3 4 - 4
m5 : 2 - 2 3 - 3 4 - 4
m6 : 2 - 2 3 - 3 4 - 4
m7 : 2 - 2 3 - 3 4 - 4
m8 : 2 - 2 3 - 3 4 - 4
auto Print(const std::string &msg, const std::map &lst) {
std::cout << msg << " : ";
for (const auto &pa : lst) {
std::cout << pa.first << " - " << pa.second << "\t";
}
std::cout << "\n";
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
using map_int = std::map;
map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
Print("m1", m1);
std::cout << "m1.at(100) : " << m1.at(100) << std::endl;
std::cout << "m1.at(300) : " << m1.at(300) << std::endl;
std::cout << "m1.at(500) : " << m1.at(500) << std::endl;
std::cout << "m1[100] : " << m1[100] << std::endl;
std::cout << "m1[300] : " << m1[300] << std::endl;
std::cout << "m1[500] : " << m1[500] << std::endl;
return 0; // a.exec();
}
输出结果:
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1.at(100) : 10
m1.at(300) : 30
m1.at(500) : 50
m1[100] : 10
m1[300] : 30
m1[500] : 50
auto Print(const std::string &msg, const std::map &lst) {
std::cout << msg << " : ";
for (const auto &pa : lst) {
std::cout << pa.first << " - " << pa.second << "\t";
}
std::cout << "\n";
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
using map_int = std::map;
map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
Print("m1", m1);
map_int::iterator iter = m1.begin();
std::cout << "m1 : ";
for (; iter != m1.end(); ++iter) {
std::cout << iter->first << " - " << iter->second << "\t";
}
std::cout << "\n";
map_int::const_iterator citer = m1.begin(); //返回指向起始的迭代器
std::cout << "m1 : ";
for (; citer != m1.end(); ++citer) {
std::cout << citer->first << " - " << citer->second << "\t";
}
std::cout << "\n";
map_int::reverse_iterator rbiter = m1.rbegin(); //返回指向起始的逆向迭代器
std::cout << "m1 : ";
for (; rbiter != m1.rend(); ++rbiter) {
std::cout << rbiter->first << " - " << rbiter->second << "\t";
}
std::cout << "\n";
map_int::const_reverse_iterator crbiter = m1.crbegin(); //返回指向起始的迭代器
std::cout << "m1 : ";
for (; crbiter != m1.crend(); ++crbiter) {
std::cout << crbiter->first << " - " << crbiter->second << "\t";
}
std::cout << "\n";
return 0; // a.exec();
}
输出结果:
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1 : 500 - 50 400 - 40 300 - 30 200 - 20 100 - 10
m1 : 500 - 50 400 - 40 300 - 30 200 - 20 100 - 10
auto Print(const std::string &msg, const std::map &lst) {
std::cout << msg << " : ";
for (const auto &pa : lst) {
std::cout << pa.first << " - " << pa.second << "\t";
}
std::cout << "\n";
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
using map_int = std::map;
map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
Print("m1", m1);
//检查容器是否为空
std::cout << std::boolalpha << "m1.empty() : " << m1.empty() << std::endl;
std::cout << "m1.size() : " << m1.size() << std::endl; //返回容纳的元素数
std::cout << "m1.max_size() : " << m1.max_size()
<< std::endl; //返回可容纳的最大元素数,和平台有关
return 0; // a.exec();
}
输出结果:
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1.empty() : false
m1.size() : 5
m1.max_size() : 461168601842738790
auto Print(const std::string &msg, const std::map &lst) {
std::cout << msg << " : ";
for (const auto &pa : lst) {
std::cout << pa.first << " - " << pa.second << "\t";
}
std::cout << "\n";
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
using map_int = std::map;
map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
Print("m1", m1);
m1.clear(); // 清除内容
Print("m1", m1);
m1.insert(std::pair(1, 10)); //插入元素或结点
m1.insert({2, 20});
Print("m1", m1);
m1.emplace(3, 30); //原位构造元素
m1.emplace(4, 40);
Print("m1", m1);
m1.emplace_hint(m1.end(), 5, 50); //使用提示原位构造元素
m1.emplace_hint(m1.end(), 6, 60);
m1.emplace_hint(m1.end(), 7, 70);
Print("m1", m1);
m1.erase(m1.begin());
Print("m1", m1);
return 0; // a.exec();
}
输出结果:
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1 :
m1 : 1 - 10 2 - 20
m1 : 1 - 10 2 - 20 3 - 30 4 - 40
m1 : 1 - 10 2 - 20 3 - 30 4 - 40 5 - 50 6 - 60 7 - 70
m1 : 2 - 20 3 - 30 4 - 40 5 - 50 6 - 60 7 - 70
auto Print(const std::string &msg, const std::map &lst) {
std::cout << msg << " : ";
for (const auto &pa : lst) {
std::cout << pa.first << " - " << pa.second << "\t";
}
std::cout << "\n";
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
using map_int = std::map;
map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
Print("m1", m1);
std::cout << "m1.count() : " << m1.count(300)
<< std::endl; //返回匹配特定键的元素数量
auto it = m1.find(200); //寻找带有特定键的元素
if (it == m1.end()) {
std::cout << "m1.find(200) : m1.end()" << std::endl;
} else {
std::cout << "m1.find(200) : " << it->first << " - " << it->second
<< std::endl;
}
//返回容器中所有拥有给定关键的元素范围。范围以二个迭代器定义,一个指向首个不小于
// key 的元素,另一个指向首个大于 key 的元素。首个迭代器可以换用 lower_bound()
//获得,而第二迭代器可换用 upper_bound() 获得
std::pair pa =
m1.equal_range(100); //返回匹配特定键的元素范围
std::cout << pa.first->first << " - " << pa.first->second << std::endl;
std::cout << pa.second->first << " - " << pa.second->second << std::endl;
map_int::iterator lit =
m1.lower_bound(300); //返回指向首个不小于给定键的元素的迭代器
std::cout << "m1.lower_bound(300) : " << lit->first << " - " << lit->second
<< std::endl;
lit = m1.upper_bound(100); //返回指向首个大于给定键的元素的迭代器
std::cout << "m1.upper_bound(100) : " << lit->first << " - " << lit->second
<< std::endl;
return 0; // a.exec();
}
输出结果:
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1.count() : 1
m1.find(200) : 200 - 20
100 - 10
200 - 20
m1.lower_bound(300) : 300 - 30
m1.upper_bound(100) : 200 - 20
auto Print(const std::string &msg, const std::map &lst) {
std::cout << msg << " : ";
for (const auto &pa : lst) {
std::cout << pa.first << " - " << pa.second << "\t";
}
std::cout << "\n";
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
using map_int = std::map;
map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
map_int m2{{100, 1}, {200, 2}, {300, 3}, {400, 4}, {500, 5}};
Print("m1", m1);
Print("m2", m2);
std::cout.setf(std::ios::boolalpha);
std::cout << "m1 == m2 : " << (m1 == m2) << std::endl;
std::cout << "m1 != m2 : " << (m1 != m2) << std::endl;
std::cout << "m1 > m2 : " << (m1 > m2) << std::endl;
std::cout << "m1 >= m2 : " << (m1 >= m2) << std::endl;
std::cout << "m1 < m2 : " << (m1 < m2) << std::endl;
std::cout << "m1 <= m2 : " << (m1 <= m2) << std::endl;
std::cout.unsetf(std::ios::boolalpha);
// c++20 废弃以上操作符重载,提供三路运算符 operator <=> ()
std::swap(m1, m2);
Print("m1", m1);
Print("m2", m2);
return 0; // a.exec();
}
输出结果:
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m2 : 100 - 1 200 - 2 300 - 3 400 - 4 500 - 5
m1 == m2 : false
m1 != m2 : true
m1 > m2 : true
m1 >= m2 : true
m1 < m2 : false
m1 <= m2 : false
m1 : 100 - 1 200 - 2 300 - 3 400 - 4 500 - 5
m2 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
起始