Reputation: 265
I have a:map<vector<int>, vector<int>> info
I have to do a search. I try:
Key[0]=1;
Key[1]=3;
Key[2]=1;
test=info.find(key);
Where Key
and test
are defined as follows:vector<int> Key (3,0)
and vector<int> test (2,0)
.
But this returns a compilation error: error: no match for 'operator=' in 'test =
. What is the reason for this?
Upvotes: 1
Views: 5233
Reputation: 15524
You get the error because std::vector
does not have an operator overload for iterator assignment.
std::vector<int>::find
returns an input iterator. std::vector<int>::operator=
takes another std::vector<int>
or a C++11 initializer-list.
You should try something like this instead.
// Some map.
std::map<std::vector<int>, std::vector<int>> info{ { { 1, 3, 1 }, { 5, 5, 5 } } };
auto itr = info.find({ 1, 3, 1 }); // Find element
if (itr != std::end(info)) { // Only if found
auto& v = itr->second; // Iterator returns std::pair (key, value)
for (auto i : v) { // Print result or do what you want.
std::cout << i << std::endl;
}
}
Upvotes: 0
Reputation: 103693
find
returns an iterator. First you need to check if the key was actually found, by testing it against info.end()
. Then, you need to assign from the value, which is stored in the second of the pair.
auto it = info.find(key);
// pre-c++11: std::map<vector<int>, vector<int> >::iterator it = info.find(key)
if (it != info.end())
{
test = it->second;
}
Upvotes: 4