<tfoot id='GkRwh'></tfoot>
  • <small id='GkRwh'></small><noframes id='GkRwh'>

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

      • <bdo id='GkRwh'></bdo><ul id='GkRwh'></ul>
      1. <legend id='GkRwh'><style id='GkRwh'><dir id='GkRwh'><q id='GkRwh'></q></dir></style></legend>
      2. STL MAP 应该使用 find() 或 [n] 标识符来查找地图中的元素吗?

        STL MAP should use find() or [n] identifier to find element in map?(STL MAP 应该使用 find() 或 [n] 标识符来查找地图中的元素吗?)
      3. <small id='wNzM8'></small><noframes id='wNzM8'>

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

          <tbody id='wNzM8'></tbody>

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

                  本文介绍了STL MAP 应该使用 find() 或 [n] 标识符来查找地图中的元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我很困惑哪个更有效?

                  既然可以直接访问map,为什么还要用find?

                  As we can access map directly, why do we need to use find?

                  我只需要知道哪种方式更有效.

                  I just need to know which way is more efficient.

                  #include <iostream>
                  #include <map>
                  using namespace std;
                  
                  int main ()
                  {
                    map<char,int> mymap;
                    map<char,int>::iterator it;
                  
                    mymap['a']=50;
                    mymap['b']=100;
                    mymap['c']=150;
                    mymap['d']=200;
                  
                    //one way
                  
                    it=mymap.find('b');
                    cout << (*it).second <<endl;
                  
                    //another way
                        cout << mymap['b'] <<endl;
                  
                    return 0;
                  }
                  

                  提前致谢!:)

                  推荐答案

                  使用 find 意味着您不会无意中创建地图中的新元素,如果键不存在,而且——更重要的是——这意味着你可以使用 find 来查找元素,如果你只有一个对地图的常量引用.

                  Using find means that you don't inadvertently create a new element in the map if the key doesn't exist, and -- more importantly -- this means that you can use find to look up an element if all you have is a constant reference to the map.

                  这当然意味着你应该检查find的返回值.通常是这样的:

                  That of course means that you should check the return value of find. Typically it goes like this:

                  void somewhere(const std::map<K, T> & mymap, K const & key)
                  {
                      auto it = mymap.find(key);
                      if (it == mymap.end()) { /* not found! */ }
                      else                   { do_something_with(it->second); }
                  }
                  

                  这篇关于STL MAP 应该使用 find() 或 [n] 标识符来查找地图中的元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                  <i id='xoO4i'><tr id='xoO4i'><dt id='xoO4i'><q id='xoO4i'><span id='xoO4i'><b id='xoO4i'><form id='xoO4i'><ins id='xoO4i'></ins><ul id='xoO4i'></ul><sub id='xoO4i'></sub></form><legend id='xoO4i'></legend><bdo id='xoO4i'><pre id='xoO4i'><center id='xoO4i'></center></pre></bdo></b><th id='xoO4i'></th></span></q></dt></tr></i><div id='xoO4i'><tfoot id='xoO4i'></tfoot><dl id='xoO4i'><fieldset id='xoO4i'></fieldset></dl></div>
                    <bdo id='xoO4i'></bdo><ul id='xoO4i'></ul>

                    <legend id='xoO4i'><style id='xoO4i'><dir id='xoO4i'><q id='xoO4i'></q></dir></style></legend>
                      <tfoot id='xoO4i'></tfoot>
                          <tbody id='xoO4i'></tbody>

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