C++代码中map的find函数问题
#include<iostream>#include<string.h>#include<map>structtest{std::map<std::string,int>...
#include <iostream>
#include <string.h>
#include <map>
struct test{
std::map<std::string, int> mapx;
test(){
for (int i=0; i<10; i++){
mapx.insert(std::make_pair("xxx", i));
}
}
};
int main(){
std::map<int, std::string>::iterator itr;
test x;
itr=x.mapx.find(std::string("xxx"));
std::cout<<itr->second<<std::endl;
return 0;
}
为什么会报错? 报错信息如下
test.cpp:15: error: no match for 'operator=' in 'itr = ((std::map<std::string, int, std::less<std::string>, std::allocator<std::pair<const std::string, int> > >*)(&x))->std::map<_Key, _Tp, _Compare, _Alloc>::find [with _Key = std::string, _Tp = int, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, int> >](((const std::string&)((const std::string*)(&string(((const char*)"xxx"), ((const std::allocator<char>&)((const std::allocator<char>*)(&allocator<char>()))))))))'
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/stl_tree.h:152: note: candidates are: std::_Rb_tree_iterator<std::pair<const int, std::string> >& std::_Rb_tree_iterator<std::pair<const int, std::string> >::operator=(const std::_Rb_tree_iterator<std::pair<const int, std::string> >&) 展开
#include <string.h>
#include <map>
struct test{
std::map<std::string, int> mapx;
test(){
for (int i=0; i<10; i++){
mapx.insert(std::make_pair("xxx", i));
}
}
};
int main(){
std::map<int, std::string>::iterator itr;
test x;
itr=x.mapx.find(std::string("xxx"));
std::cout<<itr->second<<std::endl;
return 0;
}
为什么会报错? 报错信息如下
test.cpp:15: error: no match for 'operator=' in 'itr = ((std::map<std::string, int, std::less<std::string>, std::allocator<std::pair<const std::string, int> > >*)(&x))->std::map<_Key, _Tp, _Compare, _Alloc>::find [with _Key = std::string, _Tp = int, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, int> >](((const std::string&)((const std::string*)(&string(((const char*)"xxx"), ((const std::allocator<char>&)((const std::allocator<char>*)(&allocator<char>()))))))))'
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/stl_tree.h:152: note: candidates are: std::_Rb_tree_iterator<std::pair<const int, std::string> >& std::_Rb_tree_iterator<std::pair<const int, std::string> >::operator=(const std::_Rb_tree_iterator<std::pair<const int, std::string> >&) 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询