static istringstream [英] static istringstream

查看:72
本文介绍了static istringstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我的任务是将数字从字符串转换为int变量。我用

istringstream来执行它。

所以我编写了简单的测试函数。但它并没有像我预期的那样工作

因为在程序运行时val没有被更改。可以

你能告诉我原因吗?


#include< iostream>

#include< sstream>

#include< iomanip>

#include< stdint.h>


int main()

{

static std :: string str;

static std :: istringstream istr;

static unsigned val;


while(true)

{

std :: cout<< 输入num: << std :: endl;

std :: cin> str;


istr.str(str);


istr> val;

std :: cout<< 数字: << val<< std :: endl;

}


返回0;

}

解决方案

6月7日,5:36 * pm,sergey.lukosh ... @ gmail.com写道:


我的任务是将数字从字符串转换为int变量。我使用了

istringstream来执行它。

所以我编写了简单的测试函数。但它并没有像我预期的那样工作

因为在程序运行时val没有被更改。可以

你能告诉我原因吗?


#include< iostream>

#include< sstream>

#include< iomanip>

#include< stdint.h>


int main()

{

* * * static std :: string str;

* * * static std :: istringstream istr;

* * * static unsigned val;


* * while(true)

* * {

* * * * std :: cout< ;< 输入num: << std :: endl;

* * * * std :: cin> str;


* * * * istr.str(str);


* * * * istr> val;

* * * * std :: cout<< 数字: << val<< std :: endl;

* *}


* *返回0;

}



您需要清除stringstream对象持有的缓冲区。做一个

调用istr.str("");


另外,为什么不使用boost :: lexical_cast<这只是来自boost的

标头库?为您节省错误处理程序

,否则您需要自己使用它来支付

失败的情况。


< blockquote> 6月7日,7:51 * pm,Abhishek Padmanabh< abhishek.padman ... @ gmail.com>

写道:


6月7日,5:36 * pm,sergey.lukosh ... @ gmail.com写道:


我的任务是将数字从字符串转换为int变量。我使用了

istringstream来执行它。

所以我编写了简单的测试函数。但它并没有像我预期的那样工作

因为在程序运行时val没有被更改。可以

你能告诉我为什么?


#include< iostream>

#include< sstream>

#include < iomanip>

#include< stdint.h>


int main()

{

* * * static std :: string str ;

* * * static std :: istringstream istr;

* * * static unsigned val;


* * while(true)

* * {

* * * * std :: cout<< 输入num: << std :: endl;

* * * * std :: cin> str;


* * * * istr.str(str);


* * * * istr> val;

* * * * std :: cout<< 数字: << val<< std :: endl;

* *}


* * return 0;

}



您需要清除stringstream对象持有的缓冲区。做一个

调用istr.str("");



对不起,更正:似乎为

stringstream设置了eof()位。因此它进一步无效。您需要清除

标志以进行下一次迭代。为此,您需要调用

clear()成员。通过输入验证:


std :: cout<< std :: endl<< istr.eof()<< " " << istr.fail()<< "

<< istr.bad()<< std :: endl;


你需要检查那些标志,eof()可能没问题,但是如果设置了剩余的

2位那么可能意味着IO

操作出错。


或者,为什么不使用boost :: lexical_cast<这是来自boost的只有一个

标头库?为您节省错误处理程序

,否则您需要自己使用它来b / b
失败。


6月7日晚上8:19,Abhishek Padmanabh< abhishek.padman ... @ gmail.com>

写道:


6月7日晚上7:51,Abhishek Padmanabh< abhishek.padman ... @ gmail.com>

写道:


6月7日下午5:36,sergey.lukosh ... @ gmail.com写道:


我的任务是将数字从字符串转换为int变量。我用

istringstream来执行它。

所以我编写了简单的测试函数。但它并没有像我预期的那样工作

因为在程序运行时val没有被更改。可以

你能告诉我为什么?


#include< iostream>

#include< sstream>

#include< iomanip>

#include< stdint.h>


int main()

{

static std :: string str;

static std :: istringstream istr;

static unsigned val;


while(true)

{

std :: cout<< 输入num: << std :: endl;

std :: cin> str;


istr.str(str);


istr> val;

std :: cout< ;< 数字: << val<< std :: endl;

}


return 0;

}


您需要清除stringstream对象持有的缓冲区。做一个

调用istr.str("");



对不起,更正:似乎为

stringstream设置了eof()位。因此它进一步无效。您需要清除

标志以进行下一次迭代。为此,您需要调用

clear()成员。通过输入验证:


std :: cout<< std :: endl<< istr.eof()<< " " << istr.fail()<< "

<< istr.bad()<< std :: endl;


你需要检查那些标志,eof()可能没问题,但是如果设置了剩余的

2位那么可能意味着IO

操作出错。


或者,为什么不使用boost :: lexical_cast<这是来自boost的只有一个

标头库?为您节省错误处理程序

,否则您需要自己使用它来b / b
失败。



非常感谢!现在我调用istr.clear()并且它正常工作。

这里是我的固定代码。


PS我从未使用过boost lib。

int main()

{

static std :: string str;

static std :: istringstream istr;

静态无符号val;


while(true)

{

std :: cout< < 输入num: << std :: endl;

std :: cin> str;


istr.clear();

istr.str (str);


istr> val;

std :: cout<< 数字: << val<< std :: endl;

}


返回0;


- 隐藏引用的文字 -

- 显示引用的文字 -

}


Hello everyone!

My task is in converting numbers from string to int variables. I used
istringstream to perform it.
So I wrote simple test function. But it doesn''t work as I expected
because val is not being changed while the program is running. Could
you please tell me why ??

#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>

int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;

while( true )
{
std::cout << "Enter num: " << std::endl;
std::cin >str;

istr.str(str);

istr >val;
std::cout << "Number: " << val << std::endl;
}

return 0;
}

解决方案

On Jun 7, 5:36*pm, sergey.lukosh...@gmail.com wrote:

My task is in converting numbers from string to int variables. I used
istringstream to perform it.
So *I wrote simple test function. But it doesn''t work as I expected
because val is not being changed while the program is running. Could
you please tell me why ??

#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>

int main()
{
* * *static std::string str;
* * *static std::istringstream istr;
* * *static unsigned val;

* * while( true )
* * {
* * * * std::cout << "Enter num: " << std::endl;
* * * * std::cin >str;

* * * * istr.str(str);

* * * * istr >val;
* * * * std::cout << "Number: " << val << std::endl;
* * }

* * return 0;
}

You need to clear the buffer held by the stringstream object. Make a
call istr.str("");

Alternatively, why don''t you use boost::lexical_cast<which is just a
header only library from boost? Saves you from error handling routines
as well which otherwise you need to employ yourself for cases where it
fails.


On Jun 7, 7:51*pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:

On Jun 7, 5:36*pm, sergey.lukosh...@gmail.com wrote:

My task is in converting numbers from string to int variables. I used
istringstream to perform it.
So *I wrote simple test function. But it doesn''t work as I expected
because val is not being changed while the program is running. Could
you please tell me why ??

#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>

int main()
{
* * *static std::string str;
* * *static std::istringstream istr;
* * *static unsigned val;

* * while( true )
* * {
* * * * std::cout << "Enter num: " << std::endl;
* * * * std::cin >str;

* * * * istr.str(str);

* * * * istr >val;
* * * * std::cout << "Number: " << val << std::endl;
* * }

* * return 0;
}


You need to clear the buffer held by the stringstream object. Make a
call istr.str("");

Sorry, correction : it seems eof() bit is being set for the
stringstream. And hence it further doesn''t work. You need to clear
that flag for the next iteration. For that, you need to call the
clear() member. Verified that by putting in:

std::cout << std::endl << istr.eof() << " " << istr.fail() << " "
<< istr.bad() << std::endl;

You need to check those flags, eof() may be ok but if the rest of the
2 bits are set then that would probably mean an error with the IO
operation.

Alternatively, why don''t you use boost::lexical_cast<which is just a
header only library from boost? Saves you from error handling routines
as well which otherwise you need to employ yourself for cases where it
fails.


On Jun 7, 8:19 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:

On Jun 7, 7:51 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:

On Jun 7, 5:36 pm, sergey.lukosh...@gmail.com wrote:

My task is in converting numbers from string to int variables. I used
istringstream to perform it.
So I wrote simple test function. But it doesn''t work as I expected
because val is not being changed while the program is running. Could
you please tell me why ??

#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>

int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;

while( true )
{
std::cout << "Enter num: " << std::endl;
std::cin >str;

istr.str(str);

istr >val;
std::cout << "Number: " << val << std::endl;
}

return 0;
}

You need to clear the buffer held by the stringstream object. Make a
call istr.str("");


Sorry, correction : it seems eof() bit is being set for the
stringstream. And hence it further doesn''t work. You need to clear
that flag for the next iteration. For that, you need to call the
clear() member. Verified that by putting in:

std::cout << std::endl << istr.eof() << " " << istr.fail() << " "
<< istr.bad() << std::endl;

You need to check those flags, eof() may be ok but if the rest of the
2 bits are set then that would probably mean an error with the IO
operation.

Alternatively, why don''t you use boost::lexical_cast<which is just a
header only library from boost? Saves you from error handling routines
as well which otherwise you need to employ yourself for cases where it
fails.


Thanks a lot ! Now I call istr.clear() and it works properly.
Here my fixed code.

P.S I''ve never used boost lib.
int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;

while( true )
{
std::cout << "Enter num: " << std::endl;
std::cin >str;

istr.clear();
istr.str(str);

istr >val;
std::cout << "Number: " << val << std::endl;
}

return 0;

- Hide quoted text -
- Show quoted text -
}


这篇关于static istringstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆