久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 std::map 中使用基于范圍的 for() 循環?

How to use range-based for() loop with std::map?(如何在 std::map 中使用基于范圍的 for() 循環?)
本文介紹了如何在 std::map 中使用基于范圍的 for() 循環?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

C++11 基于范圍的 for() 循環的常見示例總是像這樣簡單:

The common example for C++11 range-based for() loops is always something simple like this:

std::vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7 };
for ( auto xyz : numbers )
{
     std::cout << xyz << std::endl;
}

在這種情況下,xyz 是一個 int.但是,當我們擁有地圖之類的東西時會發生什么?這個例子中變量的類型是什么:

In which case xyz is an int. But, what happens when we have something like a map? What is the type of the variable in this example:

std::map< foo, bar > testing = { /*...blah...*/ };
for ( auto abc : testing )
{
    std::cout << abc << std::endl;         // ? should this give a foo? a bar?
    std::cout << abc->first << std::endl;  // ? or is abc an iterator?
}

當被遍歷的容器很簡單時,看起來基于范圍的 for() 循環會給我們每個項目,而不是迭代器.這很好……如果是迭代器,我們總是要做的第一件事就是取消引用它.

When the container being traversed is something simple, it looks like range-based for() loops will give us each item, not an iterator. Which is nice...if it was iterator, first thing we'd always have to do is to dereference it anyway.

但我對地圖和多地圖等內容的期望感到困惑.

But I'm confused as to what to expect when it comes to things like maps and multimaps.

(我仍然使用 g++ 4.4,而基于范圍的循環使用 g++ 4.6+,所以我還沒有機會嘗試它.)

(I'm still on g++ 4.4, while range-based loops are in g++ 4.6+, so I haven't had the chance to try it yet.)

推薦答案

容器的每個元素都是一個 map::value_type,也就是一個 typedefcode> 用于 std::pair.因此,在 C++17 或更高版本中,您可以編寫

Each element of the container is a map<K, V>::value_type, which is a typedef for std::pair<const K, V>. Consequently, in C++17 or higher, you can write

for (auto& [key, value]: myMap) {
    std::cout << key << " has value " << value << std::endl;
}

或作為

for (const auto& [key, value]: myMap) {
    std::cout << key << " has value " << value << std::endl;
}

如果您不打算修改這些值.

if you don't plan on modifying the values.

在 C++11 和 C++14 中,您可以使用增強的 for 循環來單獨提取每一對,然后手動提取鍵和值:

In C++11 and C++14, you can use enhanced for loops to extract out each pair on its own, then manually extract the keys and values:

for (const auto& kv : myMap) {
    std::cout << kv.first << " has value " << kv.second << std::endl;
}

如果您想要值的只讀視圖,您也可以考慮標記 kv 變量 const.

You could also consider marking the kv variable const if you want a read-only view of the values.

這篇關于如何在 std::map 中使用基于范圍的 for() 循環?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 日韩精品一区二区三区第95 | 天天av网| 色综合久久久 | av一区二区三区 | 日韩欧美在线观看一区 | 成人做爰www免费看 午夜精品久久久久久久久久久久 | 国产精品久久久久9999鸭 | 天天色天天色 | 久久成人国产精品 | 日韩欧美一区在线 | 国产乱肥老妇国产一区二 | 91久久| 伊人网综合| 丝袜美腿一区二区三区 | 狠狠久久| 91久久综合亚洲鲁鲁五月天 | 久久久久国产一区二区三区四区 | 男人的天堂亚洲 | 国产国拍亚洲精品av | 日韩免费一区 | 99pao成人国产永久免费视频 | 天天舔天天 | 美女一级a毛片免费观看97 | 欧美在线日韩 | 国产精品久久久久久久7电影 | 日韩精品在线播放 | 日韩欧美在线不卡 | 国产精品久久久久一区二区三区 | 一区二区三区免费 | av网站观看| 欧美一级久久 | 狠狠干av | 成人区一区二区三区 | 精品www | 国产一区 | 91视频网址 | 91精品国产一区 | 久久99精品久久久久久 | 亚洲视频一区在线 | 国产精品视频一二三 | 日韩精品久久一区二区三区 |