C++ 无法从初始化列表转换为 std::pair

C++ Cannot convert from Initializer List to std::pair

我有一个名为 TestFunction 的函数,我已针对这个问题对其进行了简化...但实际上,我收到一个错误 <function-style-cast> cannot convert from 'initializer list' to std::pair<int, int>。这是我的简化函数:

#include <iostream>
#include <map>

void MyClass::TestFunction(cli::array<int>^ ids){

    std::multimap<int, int> mymap;
    int count = ids->Length;

    for (int i = 0; i < count; ++i) {
        //fill in the multimap with the appropriate data key/values
        mymap.insert(std::make_pair((int)ids[i], (int)i));
    }
}

如您所见,这是一个非常基本的函数(经过简化),但是当我尝试将数据插入多映射时出现错误。有谁知道为什么?

我要么使用

mymap.insert(std::make_pair((int)ids[i], (int)i));

mymap.emplace((int)ids[i], (int)i);

我正在根据@CoryKramer 的回答进行构建。看来,如果我创建一个 int 类型的临时变量,然后将其传递给 multimap.insert() 函数……错误就修复了。这是新功能:

#include <iostream> 
#include <map>

void MyClass::TestFunction(cli::array<int>^ ids){

    std::multimap<int, int> mymap;
    int count = ids->Length;

    for (int i = 0; i < count; ++i) {
        //fill in the multimap with the appropriate data key/values
        int ff = (int)ids[i];
        mymap.insert(std::make_pair(ff, (int)i));
    }
} 

出于好奇...有人知道为什么会这样吗?