首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

map迭代器输出有关问题

2012-02-10 
map迭代器输出问题int main(){std::mapstring,string wordbookstd::string fileE:/wordbook/wordbook

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
[解决办法]
探讨
std::map<string,string>::iterator ter=wordbook.begin();//剪完贴在这里


为什么位置放在那里就正常?。。不一样局部变量么?

热点排行