由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ string to int Problem
相关主题
C++ Q13: InputC++ 初学者请教一个 iostream 的问题
在C里面怎么验证一个input数字是不是超过int的范围?how to read a sentence into a vector of string?
New C++ programmer, need to ask a I/O file read question读取数据求教
关于文件读取的C++ 问题?问个C++的String问题
C++如何输入的一个小问题问个关于cin的问题
C++ 屏幕输入问题如何快速读入文本形式的整数
请教一个C++的问题求助,这样从c++输入窗口读入一连串的单词或数字呢?
C++ string类输入数据的问题请教用c++读取large file怎么可以快一些?
相关话题的讨论汇总
话题: string话题: txt2话题: use话题: ss话题: getline
进入Programming版参与讨论
1 (共1页)
b***i
发帖数: 3043
1
suppose "is" is the istreamstring varaible, which contains the whole line:
txt1 txt2 , 1, 2, 0, 3 ,
what I did was, to use is>>string1; to read first string, because space is
the delination char, then I use getline(is, sring2, ","); to read the second
string, such as txt2. then, I use a while loop to read the numbers
while(getline(is, ss, ','))
{
cout< istringstream(ss)>>i;
cout< }
but string thing is, the last number, was 0 if I use ato
t****t
发帖数: 6806
2
你逗号后面还有别的东西(e.g. extra newline, extra space)被读了进来,但是并非数
字, 这时候:
atoi返回0
extractor (>>)失败,上次的结果不变, 所以是3
PS 来来回回看了几遍才总算看懂了你的问题, 下次能不能说中文

second

【在 b***i 的大作中提到】
: suppose "is" is the istreamstring varaible, which contains the whole line:
: txt1 txt2 , 1, 2, 0, 3 ,
: what I did was, to use is>>string1; to read first string, because space is
: the delination char, then I use getline(is, sring2, ","); to read the second
: string, such as txt2. then, I use a while loop to read the numbers
: while(getline(is, ss, ','))
: {
: cout<: istringstream(ss)>>i;
: cout<
b***i
发帖数: 3043
3
对,如果逗号后有空格,就会读入上一次的东西。可是有什么解决方案呢?
我就是不想用atoi,我想用C++的string, >>等实现,如何做到最后读入一个0呢?
多谢高手指点

【在 t****t 的大作中提到】
: 你逗号后面还有别的东西(e.g. extra newline, extra space)被读了进来,但是并非数
: 字, 这时候:
: atoi返回0
: extractor (>>)失败,上次的结果不变, 所以是3
: PS 来来回回看了几遍才总算看懂了你的问题, 下次能不能说中文
:
: second

r*******y
发帖数: 290
4
get rid of the commas first

【在 b***i 的大作中提到】
: 对,如果逗号后有空格,就会读入上一次的东西。可是有什么解决方案呢?
: 我就是不想用atoi,我想用C++的string, >>等实现,如何做到最后读入一个0呢?
: 多谢高手指点

1 (共1页)
进入Programming版参与讨论
相关主题
请教用c++读取large file怎么可以快一些?C++如何输入的一个小问题
Re: 一道count frequency of all words的面试题 (转载)C++ 屏幕输入问题
c++ string 一问请教一个C++的问题
c++之极弱问C++ string类输入数据的问题
C++ Q13: InputC++ 初学者请教一个 iostream 的问题
在C里面怎么验证一个input数字是不是超过int的范围?how to read a sentence into a vector of string?
New C++ programmer, need to ask a I/O file read question读取数据求教
关于文件读取的C++ 问题?问个C++的String问题
相关话题的讨论汇总
话题: string话题: txt2话题: use话题: ss话题: getline