【问题标题】:Trying to sort map by values but get errors尝试按值对地图进行排序但出现错误
【发布时间】:2020-10-04 08:10:57
【问题描述】:

我有这个带有地图的程序,我正在尝试按值对它们进行排序,但出现错误。谁能告诉我我做错了什么。错误在 28、29、30 行。谢谢

#include <iostream>
#include <cstdlib>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<string,int> pair;
void CovidMapa()
{
    typedef map <string,int> covid1304;
    covid1304 cd;
    cd["Kumanovo"] = 44;
    cd["Skopje"] = 28;
    cd["Prilep"] = 11;
    cd["Bitola"] = 6;
    cd["Tetovo"] = 5;
    cd["Veles"] = 4;
    cd["Debar"] = 2;
    cd["Gostivar"] = 2;
    cd["Stip"] = 1;
    cd["Kavadarci"] = 1;
    covid1304::iterator ir;
    for(ir = cd.begin(); ir != cd.end(); ++ir)
    {
        cout<<"Grad: "<<ir->first<<" Zaboleni: "<<ir->second<<endl;
    }
    vector <pair> vec;
    copy (cd.begin(); cd.end(); back_inserter<vector<pair> >(vec));
    sort (vec.begin(), vec.end(), [](const pair& l, pair& r)
          {
              if(l.second != r.second)
              return l.second<r.second;
              return l.first<r.first;
          });
    for (auto const &pair: vec)
    {
        cout<<"Grad: "<<pair.first<<" Zaboleni: "<<pair.second;
    }
}

int main()
{
    CovidMapa();
}

||=== Build: Debug in NaprednoCovid (compiler: GNU GCC Compiler) ===|

C:\Users\Kent\Desktop\NaprednoCovid\main.cpp||In function 'void CovidMapa()':|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|28|error: template argument 1 is invalid|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|28|error: template argument 2 is invalid|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|28|error: invalid type in declaration before ';' token|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|29|error: expected ')' before ';' token|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|29|error: template argument 1 is invalid|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|29|error: template argument 2 is invalid|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|29|error: no matching function for call to 'back_inserter(int&)'|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|29|note: candidate is:|
c:\program files (x86)\codeblocks1\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h|479|note: template<class _Container> std::back_insert_iterator<_Container> std::back_inserter(_Container&)|
c:\program files (x86)\codeblocks1\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h|479|note:   template argument deduction/substitution failed:|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|29|error: template argument 1 is invalid|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|29|error: expected ';' before ')' token|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|30|error: request for member 'begin' in 'vec', which is of non-class type 'int'|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|30|error: request for member 'end' in 'vec', which is of non-class type 'int'|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|30|error: reference to 'pair' is ambiguous|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|8|note: candidates are: typedef struct std::pair<std::basic_string<char>, int> pair|
c:\program files (x86)\codeblocks1\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_pair.h|96|note:                 template<class _T1, class _T2> struct std::pair|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|30|error: 'pair' does not name a type|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|35|error: ISO C++ forbids declaration of 'parameter' with no type [-fpermissive]|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|35|error: expected '{' before ';' token|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp||In function 'void CovidMapa()':|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|35|warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|35|error: expected ')' before ';' token|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|36|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|36|error: ISO C++ forbids declaration of 'pair' with no type [-fpermissive]|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|36|error: range-based 'for' loops are not allowed in C++98 mode|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|38|error: request for member 'first' in 'pair', which is of non-class type 'const int'|
C:\Users\Kent\Desktop\NaprednoCovid\main.cpp|38|error: request for member 'second' in 'pair', which is of non-class type 'const int'|
||=== Build failed: 20 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

【问题讨论】:

  • typedef pairpair 已经在全局命名空间中,因为 using namespace std; 不要这样做 using namespace std;
  • 那么有没有另一种方法不需要删除 using namespace std; ?
  • 您可以将 pair 重命名为其他名称 - 但您真的应该忘记将 using namespace std; 放入全局命名空间。这是不好的做法。
  • 你还有一个问题。你有 ; 而不是 ,std::copy
  • 我想通了,但现在我得到错误 |36|error: template argument for 'template void std::sort(_RAIter, _RAIter, _Compare)' uses local type 'CovidMapa()::__lambda0'|

标签: c++ typedef ambiguity name-lookup unqualified-name


【解决方案1】:

对于初学者来说,有错别字

copy (cd.begin(); cd.end(); back_inserter<vector<pair> >(vec));
               ^^^       ^^^                    

其次,由于 using 指令的非限定名称查找,您有两个具有相同名称的实体 pair。所以编译器会报错。

举个例子

typedef pair<string,int> Pair;

//...

vector <Pair> vec;
copy (cd.begin(), cd.end(), back_inserter(vec));
sort (vec.begin(), vec.end(), [](const Pair& l, const Pair& r)
      {
          if(l.second != r.second)
          return l.second<r.second;
          return l.first<r.first;
      });

另一种方法是保留 typedef 声明,但随后使用限定名称 ::pair 而不是非限定名称 pair,例如

vector <::pair> vec;

这是显示更新的程序。

#include <iostream>
#include <cstdlib>
#include <map>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
typedef pair<string,int> pair;

void CovidMapa()
{
    typedef map <string,int> covid1304;
    covid1304 cd;
    cd["Kumanovo"] = 44;
    cd["Skopje"] = 28;
    cd["Prilep"] = 11;
    cd["Bitola"] = 6;
    cd["Tetovo"] = 5;
    cd["Veles"] = 4;
    cd["Debar"] = 2;
    cd["Gostivar"] = 2;
    cd["Stip"] = 1;
    cd["Kavadarci"] = 1;
    covid1304::iterator ir;
    for(ir = cd.begin(); ir != cd.end(); ++ir)
    {
        cout<<"Grad: "<<ir->first<<" Zaboleni: "<<ir->second<<endl;
    }
    vector <::pair> vec;
    copy (cd.begin(), cd.end(), back_inserter(vec));
    sort (vec.begin(), vec.end(), [](const ::pair& l, const ::pair& r)
          {
              if(l.second != r.second)
              return l.second<r.second;
              return l.first<r.first;
          });

    cout << '\n';          
    for (auto const &pair: vec)
    {
        cout<<"Grad: "<<pair.first<<" Zaboleni: "<<pair.second << endl;
    }
}

int main()
{
    CovidMapa();
}

【讨论】:

  • |36|错误:'template 的模板参数 void std::sort(_RAIter, _RAIter, _Compare)' 使用本地类型 'CovidMapa()::__lambda0'| |36|错误:试图实例化 'template void std::sort(_RAIter, _RAIter, _Compare)'|收到这些错误
  • @avatarguy 从我的回答中复制并粘贴 std::sort 的调用。
  • 如你所说更改了所有内容,检查了所有内容,但仍然出现错误
  • @avatarguy 请尝试复制并粘贴我显示的代码。
  • |37|error: no matching function for call to 'sort(std::vector<:pair>, int> >::iterator, std::向量<:pair>, int> >::iterator, CovidMapa()::__lambda0)'|我复制并粘贴了您显示的代码,我得到了这个
【解决方案2】:

typedef pairpair 已经在全局命名空间中,因为 using namespace std; 不要这样做 using namespace std;

std::copy 中也有; 而不是,,但根本不需要使用std::copy。只需直接从迭代器创建vector

vector <mypair> vec(cd.begin(), cd.end());

我还建议在比较 lambda 中使用 std::tie

return std::tie(l.second, l.first) < std::tie(r.second, r.first);

完整代码:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <tuple>

using namespace std; // don't do this
typedef pair<string,int> mypair; // rename your pair to not collide with std::pair
void CovidMapa()
{
    typedef map <string,int> covid1304;
    covid1304 cd;
    cd["Kumanovo"] = 44;
    cd["Skopje"] = 28;
    cd["Prilep"] = 11;
    cd["Bitola"] = 6;
    cd["Tetovo"] = 5;
    cd["Veles"] = 4;
    cd["Debar"] = 2;
    cd["Gostivar"] = 2;
    cd["Stip"] = 1;
    cd["Kavadarci"] = 1;
    covid1304::iterator ir;
    for(ir = cd.begin(); ir != cd.end(); ++ir)
    {
        cout<<"Grad: "<<ir->first<<" Zaboleni: "<<ir->second<<endl;
    }

    // construct vec directly from the iterators:
    vector <mypair> vec(cd.begin(), cd.end());

    sort (vec.begin(), vec.end(), [](const mypair& l, mypair& r)
          {
              // use std::tie to make the lambda easier to understand and maintain
              return std::tie(l.second, l.first) < std::tie(r.second, r.first);
          });

    for (auto const &pair: vec)
    {
        cout<<"Grad: "<<pair.first<<" Zaboleni: "<<pair.second << '\n';
    }
}

int main()
{
    CovidMapa();
}

另一个不需要复制所有std::strings 和ints 的选项可以是将迭代器存储在vector 中。由于迭代器是轻量级的,这种方法可能会快一点。

    vector <covid1304::iterator> vec;
    vec.reserve(cd.size());
    for(auto it=cd.begin(); it != cd.end(); ++it) vec.push_back(it);

    sort (vec.begin(), vec.end(), [](const auto& l, const auto& r)
          {
              // use std::tie to make the lambda easier to understand and maintain
              return std::tie(l->second, l->first) < std::tie(r->second, r->first);
          });

    for (auto const &pair: vec)
    {
        cout<<"Grad: "<<pair->first<<" Zaboleni: "<<pair->second << '\n';
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2012-04-25
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    相关资源
    最近更新 更多