gpt4 book ai didi

尝试插入 map 时出现 C++ 编译问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:32:27 26 4
gpt4 key购买 nike

我遇到的 C++ 编译器错误是:

line 27: Error: Could not find a match for 
std::multimap<std::string, std::vector<std::string>,
std::less<std::string>,
std::allocator<std::pair<const std::string,
std::vector<std::string>>>>
::insert(std::pair<std::string, std::vector<std::string>>)
needed in main().
1 Error(s) detected.

下面是我的程序:

#include<iostream>
#include<sstream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

typedef multimap<string, vector<string> > mos_map;
typedef multimap<string, vector<string> >::iterator mos_map_it;

int main()
{

mos_map mos;
mos_map_it it;

vector<string> v1;

v1.push_back("a");
v1.push_back("b");
v1.push_back("c");
v1.push_back("mo1");

std::string a(*(v1.end()-1));

mos.insert(std::pair< std::string, vector< std::string > >(a,v1));
//Is the above not the right way to to insert an element into the map?
return 0;
}

当我尝试使用字符串作为键插入一个 vector 作为值时,上面的代码抛出一个编译错误。我在 solaris 上工作。

最佳答案

在 map 中插入元素的正确方法是:

mos[a] = v1

(由于 map 已经重载了 operator[])

关于尝试插入 map 时出现 C++ 编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17361645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com