【问题标题】:error: ‘class std::map’ has no member named ‘first’ [closed]错误:‘class std::map’没有名为‘first’的成员[关闭]
【发布时间】:2020-10-17 08:53:00
【问题描述】:

我试图使用 map 来计算数组中所有元素的频率。这是我正在尝试的代码-

void count(int arr[] , int n){
    map<int ,int> d;
    for(int i=0;i<n ;i++){
        d[arr[i]]++;
    }
    for(auto i : d){
       cout<<d.first<<" "<<d.second<<"\n";
    }
}

基本上我使用 map 来存储数组元素的频率,我在 C++14 上尝试过,但它给出了以下错误- 错误:“class std::map”没有名为“first”的成员

我认为地图中内置了第一个和第二个成员,那么为什么它会给出错误? 如果代码中有任何更正,请告诉我

【问题讨论】:

  • unorderd_map在哪里

标签: c++ dictionary unordered-map


【解决方案1】:

std::map 没有名为first 的成员函数。您的意思是 std::pairvalue_typestd::map 并提供对键值对的访问?

【讨论】:

    【解决方案2】:

    firstsecond 字段位于地图的元素中(类型为 pair),而不是地图本身。

        for(auto i : d){
           cout<<i.first<<" "<<i.second<<"\n";
        }
    

    【讨论】:

      猜你喜欢
      • 2015-11-14
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 2013-11-16
      • 2021-03-04
      相关资源
      最近更新 更多