unordered_map::insert vs operator[]=函数给出错误

时间:2021-11-09 18:51:47

I am inserting records to an unordered map, which results in an error, below is the program:

我将记录插入到无序映射中,导致错误,下面是程序:

#include<tr1/unordered_map>
using namespace std;

int main(int argc, char *argv[])
{
    std::tr1::unordered_map<int, int> u1;
    int n;
    cout << "Enter the no. of times" << endl;
    cin >> n;
    for (int i = 0; i<n; i++)
    {
        int no_items;
        cout << "Enter no of items" << endl;
        cin >> no_items;
        for (int j = 0; j<no_items; j++)
        {
            int key, val;
            cout << "key=";
            cin >> key;
            cout << endl << "val=";
            cin >> val;
            u1.insert(std::make_pair<int, int>(key, val)); //Compiler error
                                                           //u1[key]=val; //This line is working instead of insert.
        }
    }
    return 0;
}

u1.insert(std::make_pair<int,int>(key,val)); gives error

u1.insert(std::make_pair < int,int >(关键,val));给出了误差

  • cannot convert 'key' (type 'int') to type 'int&&'
  • 不能将'key'(键入'int')转换为'int&& &'
  • no matching function for call to 'make_pair(int&, int&)'
  • 对“make_pair(int&, int&)”调用没有匹配函数

Wondering how operator [] for inserting records into unordered_map is working and insert() function not.

想知道在unordered_map中插入记录的操作符[]是如何工作的,插入()函数不是。

3 个解决方案

#1


3  

The 2 arguments for std::make_pair are forwarding references T&&. Just let the compiler do the type deduction instead:

std::make_pair的两个参数是转发引用T&& &。让编译器来做类型演绎:

u1.insert(std::make_pair(key,val));

#2


1  

u1.insert(std::make_pair<int,int>(key,val));

works if g++ is invoked without -std=c++11

如果不使用-std=c+ 11调用g++ +,则有效

but for c++11, we need to give as...

但是对于c++11,我们需要给出as…

u1.insert(std::make_pair(key,val));

Because earlier versions of make_pair is

因为make_pair的早期版本是

template <class T1,class T2>
  pair<T1,T2> make_pair (T1 x, T2 y)
  {
    return ( pair<T1,T2>(x,y) );
  }

but from c++11, the signature is

但是从c++11,签名是。

template <class T1, class T2>
pair<V1,V2> make_pair (T1&& x, T2&& y);

#3


1  

I would even recommend doing emplace - to save on creating a temporary. Check out this:

我甚至建议做放置——以节省创建临时文件的时间。看看这个:

u1.emplace(key, val);

#1


3  

The 2 arguments for std::make_pair are forwarding references T&&. Just let the compiler do the type deduction instead:

std::make_pair的两个参数是转发引用T&& &。让编译器来做类型演绎:

u1.insert(std::make_pair(key,val));

#2


1  

u1.insert(std::make_pair<int,int>(key,val));

works if g++ is invoked without -std=c++11

如果不使用-std=c+ 11调用g++ +,则有效

but for c++11, we need to give as...

但是对于c++11,我们需要给出as…

u1.insert(std::make_pair(key,val));

Because earlier versions of make_pair is

因为make_pair的早期版本是

template <class T1,class T2>
  pair<T1,T2> make_pair (T1 x, T2 y)
  {
    return ( pair<T1,T2>(x,y) );
  }

but from c++11, the signature is

但是从c++11,签名是。

template <class T1, class T2>
pair<V1,V2> make_pair (T1&& x, T2&& y);

#3


1  

I would even recommend doing emplace - to save on creating a temporary. Check out this:

我甚至建议做放置——以节省创建临时文件的时间。看看这个:

u1.emplace(key, val);