<legend id='cPdAP'><style id='cPdAP'><dir id='cPdAP'><q id='cPdAP'></q></dir></style></legend>

      <i id='cPdAP'><tr id='cPdAP'><dt id='cPdAP'><q id='cPdAP'><span id='cPdAP'><b id='cPdAP'><form id='cPdAP'><ins id='cPdAP'></ins><ul id='cPdAP'></ul><sub id='cPdAP'></sub></form><legend id='cPdAP'></legend><bdo id='cPdAP'><pre id='cPdAP'><center id='cPdAP'></center></pre></bdo></b><th id='cPdAP'></th></span></q></dt></tr></i><div id='cPdAP'><tfoot id='cPdAP'></tfoot><dl id='cPdAP'><fieldset id='cPdAP'></fieldset></dl></div>
      • <bdo id='cPdAP'></bdo><ul id='cPdAP'></ul>

      <small id='cPdAP'></small><noframes id='cPdAP'>

      1. <tfoot id='cPdAP'></tfoot>

        C++ 映射访问丢弃限定符 (const)

        时间:2024-05-12
          <tbody id='S3KJ0'></tbody>

          <i id='S3KJ0'><tr id='S3KJ0'><dt id='S3KJ0'><q id='S3KJ0'><span id='S3KJ0'><b id='S3KJ0'><form id='S3KJ0'><ins id='S3KJ0'></ins><ul id='S3KJ0'></ul><sub id='S3KJ0'></sub></form><legend id='S3KJ0'></legend><bdo id='S3KJ0'><pre id='S3KJ0'><center id='S3KJ0'></center></pre></bdo></b><th id='S3KJ0'></th></span></q></dt></tr></i><div id='S3KJ0'><tfoot id='S3KJ0'></tfoot><dl id='S3KJ0'><fieldset id='S3KJ0'></fieldset></dl></div>
                  <bdo id='S3KJ0'></bdo><ul id='S3KJ0'></ul>
                • <tfoot id='S3KJ0'></tfoot>

                • <legend id='S3KJ0'><style id='S3KJ0'><dir id='S3KJ0'><q id='S3KJ0'></q></dir></style></legend>

                  <small id='S3KJ0'></small><noframes id='S3KJ0'>

                  本文介绍了C++ 映射访问丢弃限定符 (const)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  以下代码表示将映射作为 const 传递到 operator[] 方法会丢弃限定符:

                  The following code says that passing the map as const into the operator[] method discards qualifiers:

                  #include <iostream>
                  #include <map>
                  #include <string>
                  
                  using namespace std;
                  
                  class MapWrapper {
                  public:
                      const int &get_value(const int &key) const {
                          return _map[key];
                      }
                  
                  private:
                      map<int, int> _map;
                  };
                  
                  int main() {
                      MapWrapper mw;
                      cout << mw.get_value(42) << endl;
                      return 0;
                  }
                  

                  这是因为地图访问时可能发生的分配吗?不能将具有映射访问的函数声明为 const 吗?

                  Is this because of the possible allocation that occurs on the map access? Can no functions with map accesses be declared const?

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

                  推荐答案

                  std::mapoperator [] 没有声明为 const,并且不能因为它的行为:

                  std::map's operator [] is not declared as const, and cannot be due to its behavior:

                  T&operator[] (const Key& key)

                  T& operator[] (const Key& key)

                  返回对映射到与键等效的键的值的引用,如果这样的键不存在,则执行插入.

                  Returns a reference to the value that is mapped to a key equivalent to key, performing insertion if such key does not already exist.

                  因此,您的函数不能声明为const,也不能使用地图的operator[].

                  As a result, your function cannot be declared const, and use the map's operator[].

                  std::mapfind() 函数允许您在不修改映射的情况下查找键.

                  std::map's find() function allows you to look up a key without modifying the map.

                  find() 返回一个iteratorconst_iteratorstd::pair 包含键(.first)和值(.second).

                  find() returns an iterator, or const_iterator to an std::pair containing both the key (.first) and the value (.second).

                  在 C++11 中,你也可以使用 at() 用于 std::map.如果元素不存在,该函数会抛出一个 std::out_of_range 异常,这与 operator [] 不同.

                  In C++11, you could also use at() for std::map. If element doesn't exist the function throws a std::out_of_range exception, in contrast to operator [].

                  这篇关于C++ 映射访问丢弃限定符 (const)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 emplace_back 比 push_back 快? 下一篇:STL 向量:移动向量的所有元素

                  相关文章

                  <i id='V2DWN'><tr id='V2DWN'><dt id='V2DWN'><q id='V2DWN'><span id='V2DWN'><b id='V2DWN'><form id='V2DWN'><ins id='V2DWN'></ins><ul id='V2DWN'></ul><sub id='V2DWN'></sub></form><legend id='V2DWN'></legend><bdo id='V2DWN'><pre id='V2DWN'><center id='V2DWN'></center></pre></bdo></b><th id='V2DWN'></th></span></q></dt></tr></i><div id='V2DWN'><tfoot id='V2DWN'></tfoot><dl id='V2DWN'><fieldset id='V2DWN'></fieldset></dl></div>

                    <small id='V2DWN'></small><noframes id='V2DWN'>

                      <bdo id='V2DWN'></bdo><ul id='V2DWN'></ul>

                  1. <tfoot id='V2DWN'></tfoot>

                  2. <legend id='V2DWN'><style id='V2DWN'><dir id='V2DWN'><q id='V2DWN'></q></dir></style></legend>