您现在的位置是:首页 > 唯美句子

C++ std::map几种遍历方式(正序、倒序)

作者:利杜鹃时间:2024-04-28 17:35:40分类:唯美句子

简介  文章浏览阅读1.3k次,点赞8次,收藏8次。C++ std::map几种遍历方式(正序、倒序)1、map 的定义方式2、正序遍历 map2.1 使用 for 循环2.2 使用 while 循环3、倒序遍历 map3.1 使用 for 循环3.2 使用 whil

点击全文阅读

C++ std::map几种遍历方式(正序、倒序)

文章目录

C++ std::map几种遍历方式(正序、倒序)1、map 的定义方式2、正序遍历 map2.1 使用 for 循环2.2 使用 while 循环 3、倒序遍历 map3.1 使用 for 循环3.2 使用 while 循环 4、使用 std::greater 属性,直接定义倒序存储的 map4.1 使用 for 循环4.2 使用 while 循环

1、map 的定义方式

//默认定义格式(默认按key升序存储): key, 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, "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 使用 while 循环

#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 使用 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;}

点击全文阅读

郑重声明:

本站所有活动均为互联网所得,如有侵权请联系本站删除处理

我来说两句

fetch1("select plface from {$dbtbpre}enewspl_set limit 1"); $facer=explode("||",$r[plface]); $count=count($facer); $plface=''; $plfacereply=''; for($i=1;$i<$count-1;$i++) { $face=explode("##",$facer[$i]); $img = $public_r[newsurl].'e/data/face/'.$face[1]; $plface.='
  • '; $plfacereply.='
  • '; } $userpiclink = ''; $username = getcvar('mlusername'); $userid=getcvar('mluserid'); $rnd = getcvar('mlrnd'); if($username&&$userid&&$rnd){ $user_r = sys_ShowMemberInfo($userid,'ui.userpic'); $userpic=$user_r[userpic]?$user_r[userpic]:$public_r[newsurl].'e/extend/lgyPl/assets/nouserpic.gif'; $userpiclink = ''; $userlink=''.$username.''; } ?>
    连接失败,请检查您的网络!
    热门评论
    0人参与,0条评论
    正在载入评论列表...