map迭代器输出问题
int main()
{
std::map<string,string> wordbook;
std::string file="E:/wordbook/wordbook.txt";
std::map<string,string>::iterator ter=wordbook.begin();
std::ifstream infile(file.c_str());
if(!infile)
{
std::cout<<"unable to open infile!"<<std::endl;
return -1;
}
string s;
string t;
while(!infile.eof())
{
infile>>s;
infile>>t;
//std::cout<<s<<" "<<t<<std::endl;
wordbook.insert(make_pair(s,t));
}
//std::cout<<wordbook["cent"];
while(!(ter==wordbook.end()))
{
std::cout<<wordbook.begin()->second<<std::endl;
++ter;
}
return 0;
}
////输不出map容器中的元素,为什么?
[解决办法]
- C/C++ code
int main(){ std::map<string,string> wordbook; std::string file="E:/wordbook/wordbook.txt"; // std::map<string,string>::iterator ter=wordbook.begin(); 这句剪切下来 std::ifstream infile(file.c_str()); if(!infile) { std::cout<<"unable to open infile!"<<std::endl; return -1; } string s; string t; while(!infile.eof()) { infile>>s; infile>>t;// std::cout<<s<<" "<<t<<std::endl; wordbook.insert(make_pair(s,t)); }//std::cout<<wordbook["cent"]; std::map<string,string>::iterator ter=wordbook.begin();//剪完贴在这里 while(!(ter==wordbook.end())) { std::cout<<wordbook.begin()->second<<std::endl; ++ter; } return 0;}
[解决办法]
std::map<string,string>::iterator ter=wordbook.begin();//剪完贴在这里
为什么位置放在那里就正常?。。不一样局部变量么?
[解决办法]
问题1
请google“insert 迭代器失效”……
问题2
请不要使用vc6
[解决办法]