문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

testmap.insert(1, testvector);

You probably meant to do

testmap[1] = testvector;

instead.

Since you are using C++11 (as evidenced by your use of >> :)), you can also use emplace.

testmap.emplace(1, testvector);

try

testmap.insert({1, testvector});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top