Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting into map with types <int, vector<int>>

I have this code:

map< int , vector< int>> testmap;
vector<int> testvector;
testvector.push_back(10);
testmap.insert(1, testvector);

This code is giving me an error, telling me that there is no overloaded function to match the argument list.

Can anyone tell me why this is happening? I'm trying to insert a vector into a map but this method doesn't seem to work.

like image 785
Hatted Rooster Avatar asked May 15 '26 16:05

Hatted Rooster


2 Answers

There is no overload of std::map::insert matching the arguments you are passing. This would work:

auto p = testmap.insert(std::make_pair(1, testvector));

std::cout << std::boolalpha;
std::cout << "Did insert succeed? " << p.second << std::endl;

This will succeed if there is no element in the map with key 1.

like image 198
juanchopanza Avatar answered May 17 '26 06:05

juanchopanza


testmap.insert(1, testvector);

You probably meant to do

testmap[1] = testvector;

instead.

like image 25
πάντα ῥεῖ Avatar answered May 17 '26 06:05

πάντα ῥεῖ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!