c++ - 尝试按值对 map 进行排序但出现错误

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

我有一个带有 map 的程序,我试图按值对它们进行排序,但出现错误。谁能告诉我我做错了什么。错误在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)) ===|

最佳答案

对于初学者有错别字

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

其次,由于使用了指令,对于不合格的名称查找,您有两个名称相同的实体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();
}

关于c++ - 尝试按值对 map 进行排序但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62379343/

相关文章:

c++ - C++中的 vector 运算

c++ - 我是否需要在我的 for 循环中使用小于 (<) 运算符来进行自动矢量化?

c++ - 总是使用指针而不是引用是否可以接受,以便在需要时轻松转换为智能指针?

c++ - C++中函数指针的模板typedef

C++ typedef继承二义性问题

c++ - Gcc:强制编译器默认使用 unsigned char

c++ - 如何使用 typdef 名称 C++ typedef 模板类

c++ - 上下文敏感性与歧义

c# - 带有 Action<T> 参数重载的模糊方法调用

c++ - 消除对采用 std::functions 的函数的调用的歧义