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呢? : 多谢高手指点
|
|