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

如何使用基于范圍的 for 循環(huán)修改地圖中的值?

How can I modify values in a map using range based for loop?(如何使用基于范圍的 for 循環(huán)修改地圖中的值?)
本文介紹了如何使用基于范圍的 for 循環(huán)修改地圖中的值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個基于范圍的 for 循環(huán)來迭代 foobar 中的元素,如下所示:

#include #include int main(){std::mapfoob??ar({{1,1}, {2,2}, {3,3}});for(自動 p : foobar){++p.second;std::cout <<{"<<p.first <<", " <<p.second <<"}";}std::cout <<std::endl;for(自動 q : foobar){std::cout <<{"<<q.第一個<<", " <<q.second<<"}";}std::cout <<std::endl;}

此代碼產(chǎn)生以下輸出:

{1, 2} {2, 3} {3, 4}{1, 1} {2, 2} {3, 3}

第一行被修改并打印在 for 循環(huán)中,第二行應(yīng)該打印相同的修改值.為什么輸出不匹配?對 std::map 的更改是否僅在循環(huán)范圍內(nèi)有效?有沒有辦法不僅可以訪問而且可以修改這些值?

可以在 cpp.sh 上找到 此代碼的運行版本.

為了清楚起見,此處給出的示例經(jīng)過修改以匹配接受的答案.

解決方案

你可以把 auto 變成 auto& 如果你想改變/修改容器,例如:

#include #include int main(){std::mapfoob??ar({{1,1}, {2,2}, {3,3}});for(auto& p: foobar) {++p.second;std::cout <<'{' <<p.first <<", " <<p.second <<"}";}std::cout <<std::endl;}

編譯和輸出

<前>{1, 2} {2, 3} {3, 4}

現(xiàn)場示例

I have a range based for loop to iterate over elements in foobar as follows:

#include <map>
#include <iostream>

int main()
{
  std::map<int, int> foobar({{1,1}, {2,2}, {3,3}});

  for(auto p : foobar) 
  {
    ++p.second;
    std::cout << "{" << p.first << ", " << p.second << "} ";
  }
  std::cout << std::endl;

  for(auto q : foobar) 
  {
    std::cout << "{" << q.first << ", " << q.second << "} ";
  } 
  std::cout << std::endl;
}

This code produces the following output:

{1, 2} {2, 3} {3, 4}
{1, 1} {2, 2} {3, 3}

The first line is modified and printed inside a for loop and the second line supposedly prints the same modified values. Why don't the outputs match? Are changes to std::map only effective in the scope of the loop? Is there a way I can not only access but modify these values?

A running version of this code can be found on cpp.sh.

EDIT: The example given here was modified to match the accepted answer for clarity.

解決方案

You can turn auto into auto& if you want to mutate/modify the container, for instance:

#include <map>
#include <iostream>

int main()
{
  std::map<int, int> foobar({{1,1}, {2,2}, {3,3}});
  for(auto& p : foobar) {
    ++p.second;
    std::cout << '{' << p.first << ", " << p.second << "} ";
  }
  std::cout << std::endl;
}

compiles ands outputs

{1, 2} {2, 3} {3, 4} 

live example

這篇關(guān)于如何使用基于范圍的 for 循環(huán)修改地圖中的值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
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 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 亚洲一区中文 | 欧美一级片在线播放 | 日韩在线观看中文字幕 | 黄色片视频在线观看 | 欧美激情网站 | 黄色网址免费看 | 久久精品视频网 | 精品一区在线播放 | 精品少妇 | 丁香六月激情 | 亚洲精品911 | 成人av一区二区三区在线观看 | 国产一区二区三区免费 | 国产午夜免费 | 亚洲第一伊人 | 高清视频一区二区 | 日本少妇一区二区 | 涩涩视频免费看 | 黄色福利网站 | 国产中文字幕在线播放 | 亚洲黄色av | 在线观看欧美日韩视频 | 五月天婷婷激情 | 午夜视频福利 | 久久久久黄色 | 欧美精品影院 | 欧美日韩视频一区二区 | 国产精品911 | 亚洲综合成人网 | 精品一区二区三区av | 午夜拍拍| 亚洲午夜18毛片在线看 | 一区二区三区精品 | 日韩视频二区 | 国产精品一级二级 | 综合色婷婷一区二区亚洲欧美国产 | 男女啪啪网站 | 精品久久久久久久久久久久久 | 中文字幕一区二区三区乱码 | 欧美 日韩 国产 成人 在线 | 一区在线观看 |