C++ std::map几种遍历方式(正序倒序)
目录
- 1、map 的定义方式
- 2、正序遍历 map
- 2.1 使用 for 循环
- 2.2 使用 while 循环
- 3、倒序遍历 map
- 3.1 使用 for 循环
- 3.2 使用 while 循环
- 4、使用 std::greater 属性,直接定义倒序存储的 map
- 4.1 使用 for 循环
- 4.2 使用 while 循环
1、map 的定义方式
//默认定义格式(默认按key升序存储): key,js value,其中key可以是任意类型 std::map<std::uint32_t, std::string> myMap; //key 值为 std::uint32_t 类型 std::map<std::string, std::string> myMap; //key 值为 std::string 类型 //指定数据按key升序存储 std::map<std::uint32_t, std::string, std::greater<std::uint32_t> > myMap; //指定数据按key升序存储 std::map<std::uint32_t, std::string, std::less<std::uint32_t> > myMap;
2、正序遍历 map
注意:正序使用的是 std::map<std::uint32_t, std::string>::iterator, 倒序使用的是:std::map<std::uint32_t, std::string>::reverse_iterator。
2.1 使用 for 循环
#include <IOStream>
#include <map>
int main()
{
std::map<std::uint32_t, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}};
// 使用迭代器倒序遍历map
std::map<std::uint32_t, std::string>::iterator iter;
for (iter = myMap.begin(); iter != myMap.end(); ++iter)
{
std::cout << iter->first << " => " << iter->second << '\n';
}
return 0;
}
2.2 使用 while 循环
#include <iostream>
#include <map>
int main()
{
std::map<std::uint32_t, std::string> myMap = {{1, "one"}, {2, "two"}, {3, android"three"}};
std::map<std::uint32_t, std::string>::iterator iter = myMap.begin();
// 使用迭代器倒序遍历map
while (iter != myMap.end())
{
std::cout << iter->first << " => " << iter->second << '\n';
++it;
}
return 0;
}
3、倒序遍历 map
注意:正序使用的是 std::map<std::uint32_t, std::string>::iterator, 倒序使用的是:std::map<std::uint32_t, std::string>::reverse_iterator。
3.1 使用 for 循环
#include <iostream>
#include <map>
int main()
{
std::map<std::uint32_t, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}};
// 使用迭代器倒序遍历map
std::map<std::uint32_t, std::string>::reverse_iterator iter;
for (iter = myMap.rbegin(); iter != myMap.rend(); ++iter)
{
std::cout << iter->first << " => " << iter->second << '\n';
}
return 0;
}
3.2 使用 whilepython 循环
#include <iostream>
#include <map>
int main()
{
std::map<std::uint32_t, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}};
std::map<std::uint32_t, std::string>::reverse_iterator iter = myMap.rbegin();
// 使用迭代器倒序遍历map
while (iter != myMap.rend())
{
std::cout << iter->first << " => " << iter->second << '\n';
++it;
}
return 0;
}
4、使用 std::greater 属性,直接定义倒序存储的 map
注意:正序使用的是 std::map<std::uint32_t, std::string>::iterator, 倒序使用的是:编程客栈std::map<std::uint32_t, std::string>::reverse_iterator。
4.1 使用 for 循环
#include <iostream>
#include <map>
int main()
{
std::map<std::uint32_t, std::string, std::greater<std::uint32_t> > myMap = {{1, "one"}, {2, "two"}, {3, "three"}};
// 使用迭代器倒序遍历map
std::map<std::uint32_t, std::string>::iterator iter;
for (iter = myMap.begin(); iter != myMap.end(); ++iter)
{
std::cout << iter->first << " => " << iter->second << '\n';
}
return 0;
}
4.2 www.devze.com使用 while 循环
#include <iostream>
#include <map>
int main()
{
std::map<std::uint32_t, std::string, std::greater<std::uint32_t> > myMap = {{1, "one"}, {2, "two"}, {3, "three"}};
std::map<std::uint32_t, std::string>::iterator iter = myMap.begin();
// 使用迭代器倒序遍历map
while (iter != myMap.end())
{
std::cout << iter->first << " => " << iter->second << '\n';
++it;
}
return 0;
}
到此这篇关于C++ std::map几种遍历方式(正序倒序)的文章就介绍到这了,更多相关C++ std::map遍历内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
加载中,请稍侯......
精彩评论