【问题标题】:Find on list of map在地图列表中查找
【发布时间】:2015-03-17 22:28:49
【问题描述】:

您好,我正在做一个项目,我是 C++ 初学者

我有一张地图列表

list<map<int,double>> voisin;

我想知道是否可以检查该元素是否存在于我的地图中。 这是一个 C# 代码,你能告诉我如何用 C++ 来做吗?

var res = voisin[a].Find(Map<int, double> x)

谢谢

【问题讨论】:

    标签: c++ hashmap


    【解决方案1】:

    看看http://www.cplusplus.com/reference/map/map/find/

    这应该可以解决问题。

    // map::find
    #include <iostream>
    #include <map>
    
    int main ()
    {
      std::map<char,int> mymap;
      std::map<char,int>::iterator it;
    
      mymap['a']=50;
      mymap['b']=100;
      mymap['c']=150;
      mymap['d']=200;
    
      it=mymap.find('b');
      mymap.erase (it);
      mymap.erase (mymap.find('d'));
    
      // print content:
      std::cout << "elements in mymap:" << '\n';
      std::cout << "a => " << mymap.find('a')->second << '\n';
      std::cout << "c => " << mymap.find('c')->second << '\n';
    
      return 0;
    }
    

    输出: mymap 中的元素: 一个 => 50 c => 150

    【讨论】:

      【解决方案2】:

      使用std::map::find:

      std::map<int,double>::iterator it = m.find(e);
      

      其中mmapem 中的键类型相同。

      find 如果没有找到指定的键,则返回一个迭代器到map::end

      注意std::list in C++ is not the same as List in C#std::list 是一个链表。如果您想要一个与 C# List 等效的容器,请使用 std::vector

      【讨论】:

        猜你喜欢
        • 2018-06-21
        • 1970-01-01
        • 2022-12-05
        • 2018-02-27
        • 1970-01-01
        • 2021-12-17
        • 1970-01-01
        • 2012-04-23
        • 2012-09-26
        相关资源
        最近更新 更多