首页 > 解决方案 > 如何在地图 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地图。

标签: c++multimap

解决方案


你的声明hello是错误的。在这里,您不能将三种类型相互关联。

查看文档,std::multimap有 4 个模板参数:

  • Key
  • T(又名值类型)
  • Compare-operator()将被调用以比较地图元素的类型。必须是仿函数类型(函数指针、lambda、具有重载的类operator())。默认为std::less<Key>
  • Allocator- 对我们来说并不有趣。

当你std::multimap这样声明时:

multimap<string,vector<int>,int> hello;

你告诉编译器它应该按ints 比较键。这没有任何意义 - 在 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求和作为例如向量的最后一个元素,但这使得向量的目的非常不清楚(那里的一些值代表成绩,其中一个是成绩的总和?)


推荐阅读