I have multimap<string,vector<int>,int> hello
. I'm trying to insert
a value to hello
, but I have an error saying No matching member function for call to 'insert'
I think there's a problem with vector part, but I can't figure out what it is exactly.
#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});
}
}
I'd like it to insert the vector into hello
map.
Your declaration of hello
is wrong. You cannot have three types in relation to each other here.
Looking at the documentation, std::multimap
has 4 template arguments:
Key
T
(a.k.a. value type)Compare
- the type on which operator()
will be invoked to compare elements of the map. Must be a functor type (function pointer, lambda, class with overloaded operator()
). Defaults to std::less<Key>
Allocator
- not interesting for us.When you declare std::multimap
like this:
multimap<string,vector<int>,int> hello;
you're telling compiler it should compare keys by int
s. This doesn't make any sense - when inserting or looking for elements in map, compiler will have to try to call int(std::string, std::string)
and expect a result of type bool
and this is not posssible.
Why do you want to store an additional int
here? You can calculate it anytime from the vector
you have in map. Best solution IMHO would be to not repeat yourself and use std:multimap<std::string, std::vector<int>> hello
If you really want to store sum
as well as vector
, you have to mash them into one type somehow. You could use std::pair<std::string, std::pair<std::vector<int>, int>>
, but this gets pretty deeply nested. You could also push_back
sum as the last element of the vector for exaple, but that makes the vector purpose pretty unclear (some values there represent grades and one of them is sum of the grades?)