久久久久久久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 轉換單一顏色)
主站蜘蛛池模板: 9191在线观看| 国产成人网 | 国产欧美一区二区三区在线看 | 亚洲日韩视频 | 国产精品永久久久久久久www | 伊大人久久| 亚洲一二三区免费 | 91免费观看| 一级黄a| 免费成人高清 | 国产精品一区在线 | 最新国产精品精品视频 | 免费在线观看黄网站 | 欧美亚洲成人网 | hdfreexxxx中国妞| 中文字幕第一页在线 | 精品无码三级在线观看视频 | 人人性人人性碰国产 | 九九亚洲 | 国产成人午夜精品影院游乐网 | 国产精品永久免费视频 | 九九九国产 | av毛片 | 99热成人在线 | 国产精品久久久久一区二区三区 | 伊人热久久 | 自拍偷拍av| 国产精品久久久久一区二区三区 | 日本精品久久久久 | 久久精品国产免费高清 | 亚洲性视频 | 成人综合一区二区 | aaa综合国产| 韩日精品一区 | 粉嫩在线| 日本成人一区二区 | 午夜免费视频 | 日韩电影一区 | 精品国产伦一区二区三区观看方式 | 亚洲国产成人精品久久 | 福利视频一区二区 |