根據http://www.cplusplus.com/reference/vector/vector/operator=/我明白我們可以使用'='運算符來分配向量。但運行下面的代碼,我得到一個錯誤。出了什麼問題?C++:向量賦值錯誤
代碼:
void checkFunction(map<string, vector<string> > foo){
vector<string> bar;
bar = foo.find("ABC");
}
錯誤:
error: no match for ‘operator=’ in ‘bar = foo.std::map<_Key, _Tp, _Compare, _Alloc>::find [with _Key = std::basic_string<char>, _Tp = std::vector<std::basic_string<char> >, _Compare = std::less<std::basic_string<char> >, _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::vector<std::basic_string<char> > > >, std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::vector<std::basic_string<char> > > >, key_type = std::basic_string<char>](((const std::map<std::basic_string<char>, std::vector<std::basic_string<char> > >::key_type&)(& std::basic_string<char>(((const char*)"PAR"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))))))’
std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >]
std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::vector<_Tp, _Alloc>&&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >, std::vector<_Tp, _Alloc> = std::vector<std::basic_string<char> >]
std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::initializer_list<_CharT>) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >, std::vector<_Tp, _Alloc> = std::vector<std::basic_string<char> >]
* vectors *你可以指定! –
@LotharKrause:是的,但爲什麼會出現這種錯誤呢? – Enigman
因爲'map :: find'給你一個迭代器,它被取消引用,根據http://www.cplusplus.com/reference/map/map/find/ –