代码之家  ›  专栏  ›  技术社区  ›  Shadow

C++在映射中搜索元组错误:无法将“int”左值绑定到“int&&”

  •  0
  • Shadow  · 技术社区  · 8 年前

    我总是遇到这个错误。

    #include <bits/stdc++.h>
    using namespace std;
    #define mt make_tuple<int,int>
    
    int main(){
        map<tuple<int,int>,int> l;
        l[mt(5,4)] = 3;
        cout << l.count(mt(9,8));
    }
    

    1、我应该更改什么以接受文件中的值?
    2、错误在哪里?

    int main(){
        map<tuple<int,int>,int> l;
        l[mt(5,4)] = 3;
        int a,b;
        cin >> a >> b;
        cout << l.count(mt(a,b));
    }
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Vittorio Romeo    8 年前

    的全部要点 make_tuple 就是让它为你推断元组的类型。如果显式调用它,请指定 <int, int> ,则会阻止正确进行扣减。

    就让我来吧 make\u元组 做好它的工作,不要仅仅因为想保存一些击键就定义宏——你会后悔的。

    int main(){
        std::map<std::tuple<int, int>, int> l;
        l[std::make_tuple(5,4)] = 3;
        int a,b;
        cin >> a >> b;
        cout << l.count(std::make_tuple(a,b));
    }
    

    live example on wandbox.org