c++ - 如何在地图 C++ 中向矢量添加值
问题描述
我有multimap<string,vector<int>,int> hello
。我正在尝试将insert
值设置为hello
,但我有一个错误说No matching member function for call to 'insert'
我认为矢量部分有问题,但我无法弄清楚它到底是什么。
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
using namespace std;
void addSpace(string &gendata, string &target){
int size = static_cast<int>(gendata.length());
for (int i = 0; i < size; i++){
target += gendata[i];
// check i for (i < gendata.length() - 1) !!!
if (i < gendata.length() - 1 && isdigit(gendata[i]) && isalpha(gendata[i + 1])){
target += ' ';
}
}
}
std::vector<int> make_vector(int a, int b, int c, int d) {
std::vector<int> result;
result.push_back(a);
result.push_back(b);
result.push_back(c);
result.push_back(d);
return result;
}
int main(int argc, const char * argv[]) {
multimap<string,pair<string,int>> student;
multimap<string,vector<int>,int> hello;
multimap<string,pair<string,int>>::iterator itr;
string s, gendata, target, word, name, subject;
int mark=0, i=0;
target = "Aurora Physics 55 Lily Biology 81 Dylan Physics 62 Bella Physics 58 Jaxon Physics 85 Noah Chemistry 98 Jaxon Mathematics 54 Michael Chemistry 98 Charlotte Mathematics 92 Penelope Mathematics 95 Ellie Physics 93";
istringstream iss(target);
int count = 0;
while(iss >> word){
if(count == 0){
name = word;
count++;
}
else if (count == 1){
subject = word;
count++;
}else if (count == 2){
mark = stoi(word);
student.insert({name,{subject,mark}});
count = 0;
}
}
string rememberName;
int bio=0, phys=0, chem=0, maths=0, sum=0;
for (itr = student.begin() ; itr != student.end() ; itr++) {
bio=0; phys=0; chem=0; maths=0;
rememberName = itr->first;
while (rememberName == itr->first) {
if (itr->second.first == "Biology") {
bio = itr->second.second;
}else if(itr->second.first == "Physics"){
phys = itr->second.second;
}else if(itr->second.first == "Chemistry"){
chem = itr->second.second;
}else{
maths = itr->second.second;
}
itr++;
}
itr--;
sum = bio+phys+chem+maths;
// this is where I have a problem
hello.insert({rememberName,{make_vector(bio,phys,chem,maths)},sum});
}
}
我希望它将矢量插入hello
地图。
解决方案
你的声明hello
是错误的。在这里,您不能将三种类型相互关联。
查看文档,std::multimap
有 4 个模板参数:
Key
T
(又名值类型)Compare
-operator()
将被调用以比较地图元素的类型。必须是仿函数类型(函数指针、lambda、具有重载的类operator()
)。默认为std::less<Key>
Allocator
- 对我们来说并不有趣。
当你std::multimap
这样声明时:
multimap<string,vector<int>,int> hello;
你告诉编译器它应该按int
s 比较键。这没有任何意义 - 在 map 中插入或查找元素时,编译器将不得不尝试调用int(std::string, std::string)
并期望类型的结果,bool
这是不可能的。
为什么要在int
此处存储附加信息?您可以随时从vector
地图中计算它。恕我直言,最好的解决方案是不要重复自己并使用std:multimap<std::string, std::vector<int>> hello
如果你真的想存储sum
以及vector
,你必须以某种方式将它们混合成一种类型。您可以使用std::pair<std::string, std::pair<std::vector<int>, int>>
,但这会非常嵌套。您也可以push_back
求和作为例如向量的最后一个元素,但这使得向量的目的非常不清楚(那里的一些值代表成绩,其中一个是成绩的总和?)
推荐阅读
- python - numpy 矩阵的差异给出了荒谬的结果
- css - 是否可以在 Sass 变量的定义中使用 CSS 自定义属性,如果可以,如何使用?
- java - 在二维有序数组中查找行元素的位置
- google-apps-script - 使用 google Apps Script 创建新联系人并自动与我的手机联系人同步
- android - 在android studio中创建新项目时出错
- ios - 如何为重用准备约束?
- r - 更改函数 `lubridate::yday` 的开始日期
- linux - 创建一个宏将存储在rax中的数字打印到控制台,宏也应该有错误处理
- hibernate - 如何使用 Spring Data Jpa 动态更改模式名称?
- r - 在 R 中使用凌乱的调查数据进行分组并扩大范围