You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++向multimap的vector元素插入值报错:无匹配insert成员函数

问题定位与修复方案

你的核心问题出在multimap的模板参数定义错误,以及对应的insert调用不匹配——和vector本身没有关系,下面一步步拆解问题并修复:

1. 错误根源:multimap模板参数误用

C++标准库中std::multimap的模板参数格式是:

std::multimap<KeyType, MappedType, ComparatorType = std::less<KeyType>>
  • 第一个参数:键的类型(你这里用string没问题)
  • 第二个参数:键对应的值的类型
  • 第三个参数:键的比较器类型(可选,默认用std::less做升序比较)

你写的multimap<string,vector<int>,int> hello;把第三个参数设成了int,这完全不符合要求——编译器会认为你要把int作为键的比较器,这显然是非法的,所以后续调用insert时找不到匹配的函数。

从你的代码逻辑看,你想让每个名字对应「四门成绩的vector」+「总分sum」,所以应该把**第二个参数(值的类型)**定义为std::pair<std::vector<int>, int>,也就是:

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

2. 修正insert调用

原来的insert写法hello.insert({rememberName,{make_vector(bio,phys,chem,maths)},sum});不符合新的multimap值类型,需要把vector和sum打包成一个pair,修正为:

hello.insert({rememberName, {make_vector(bio, phys, chem, maths), sum}});

3. 完整修正后的代码

#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];
        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的模板参数
    multimap<string, pair<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;
            }
            // 新增判断:避免迭代器越界访问end()
            if (next(itr) == student.end()) {
                break;
            }
            itr++;
        }
        sum = bio+phys+chem+maths;
        // 修正insert的参数格式
        hello.insert({rememberName, {make_vector(bio, phys, chem, maths), sum}});
    }

    // 可选:测试输出验证结果
    for (auto& entry : hello) {
        cout << "Name: " << entry.first << endl;
        cout << "Scores: ";
        for (int score : entry.second.first) {
            cout << score << " ";
        }
        cout << "\nTotal: " << entry.second.second << "\n\n";
    }
}

额外提示

在你的循环逻辑中,直接itr++后进入while判断可能会导致迭代器越界访问student.end(),我在代码里加了if (next(itr) == student.end()) break;的判断,避免出现未定义行为。

内容的提问来源于stack exchange,提问作者xqoa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:08:00