我在c ++中有一个双端队列,我想从1到17回到它的数字.我写了下面的代码:string Result;
string Result;
ostringstream convert;
for(int i=1; i< 18; i++){
convert >> i;
Result = convert.str();
temp.push_back(Result);
}
cout <<"temp at_"<< temp.at(16) << endl;
Run Code Online (Sandbox Code Playgroud)
问题是temp.at(16)打印有,cout: 1234567891011121314151617并且not 17如何才能每次只添加当前的i?
编辑:以上代码有效:
string Result;
ostringstream convert;
for(int i=1; i< 18; i++){
convert.str(std::string());
convert << i;
Result = convert.str();
temp.push_back(Result);
}
cout <<"temp at_"<< temp.at(16) << endl;
Run Code Online (Sandbox Code Playgroud)
看起来你没有清理你的converter,
1234567891011121314151617
Run Code Online (Sandbox Code Playgroud)
是你迭代的所有整数
假设,转换是:
std::stringstream convert;
Run Code Online (Sandbox Code Playgroud)
您可以在每次使用前清除它:
convert.str(std::string());
convert << i;
Run Code Online (Sandbox Code Playgroud)