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

常量映射元素訪問

Const map element access(常量映射元素訪問)
本文介紹了常量映射元素訪問的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我嘗試使用 operator[] 訪問 const map 中的元素,但此方法失敗.我也嘗試使用 at() 來做同樣的事情.這次成功了.但是,我找不到任何關于使用 at() 訪問 const map 中的元素的參考.at()map新增的函數嗎?我在哪里可以找到有關此的更多信息?非常感謝!

I tried to use the operator[] access the element in a const map, but this method failed. I also tried to use at() to do the same thing. It worked this time. However, I could not find any reference about using at() to access element in a const map. Is at() a newly added function in map? Where can I find more info about this? Thank you very much!

示例如下:

#include <iostream>
#include <map>

using namespace std;

int main()
{
        map<int, char> A;
        A[1] = 'b';
        A[3] = 'c';

        const map<int, char> B = A;

        cout << B.at(3) << endl; // it works
        cout << B[3] << endl;  // it does not work

}

使用B[3]",編譯時返回如下錯誤:

For using "B[3]", it returned the following errors during compiling:

t01.cpp:14: 錯誤:傳遞‘conststd::map>>’ 作為‘_Tp&’的‘this’參數std::map<_Key, _Tp, _Compare,_Alloc>::operator[](const _Key&) [with _Key = int, _Tp = char, _Compare = std::less, _Alloc =std::allocator>]' 丟棄限定符

t01.cpp:14: error: passing ‘const std::map<int, char, std::less, std::allocator<std::pair<const int, char> > >’ as ‘this’ argument of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = char, _Compare = std::less, _Alloc = std::allocator<std::pair<const int, char> >]’ discards qualifiers

使用的編譯器是g++ 4.2.1

The compiler used is g++ 4.2.1

推薦答案

at() 是 C++11 中 std::map 的新方法.

如果具有給定鍵的元素不存在,則不會像 operator[] 那樣插入新的默認構造元素,而是拋出 std::out_of_range 異常.(這類似于 at() 對于 dequevector 的行為.)

Rather than insert a new default constructed element as operator[] does if an element with the given key does not exist, it throws a std::out_of_range exception. (This is similar to the behaviour of at() for deque and vector.)

由于這種行為,at()const 重載是有意義的,不像 operator[] 總是有改變地圖的潛力.

Because of this behaviour it makes sense for there to be a const overload of at(), unlike operator[] which always has the potential to change the map.

這篇關于常量映射元素訪問的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 轉換單一顏色)
主站蜘蛛池模板: 日韩中文在线视频 | 精品99爱视频在线观看 | 中文字幕一区在线观看视频 | 国产精品久久久久无码av | 请别相信他免费喜剧电影在线观看 | 亚洲网视频 | 手机看片1 | 欧美一区二区三区在线播放 | 91精品国产综合久久精品 | 日韩欧美网 | 欧美在线a | 97色在线观看免费视频 | 狠狠操在线 | 日韩视频免费 | 日本成人三级电影 | 香蕉婷婷 | 欧美激情在线播放 | 日韩a视频 | 亚洲一区二区三区久久久 | 欧美国产日本一区 | 国产小视频在线观看 | 一区二区三区精品视频 | 美女黄视频网站 | 91福利网 | 欧美中文字幕一区二区三区 | 99久久久国产精品 | 国产精品视频一区二区三区, | 亚洲一区在线日韩在线深爱 | 国产精品国产三级国产aⅴ中文 | 日本亚洲精品成人欧美一区 | 欧美成人自拍 | 国产视频不卡一区 | 久久噜噜噜精品国产亚洲综合 | 亚洲欧美一区二区三区情侣bbw | 女人牲交视频一级毛片 | 色吊丝2 | 日韩午夜电影在线观看 | 国产成人a亚洲精品 | 高清不卡毛片 | 成人免费视频网址 | 日韩高清中文字幕 |