简体   繁体   English

从字符串中提取数字

[英]Extract numbers from string

how do i get and store two numbers from a string using istringstream 我如何使用istringstream从字符串中获取和存储两个数字

For eg. 例如。

sum from 2 to 3 总和从2到3

i want to read and store 2 and 3 not necessarily we know the position of integers occuring 我想读取和存储2和3不一定我们知道整数的位置

You can read input using std::cin , for example. 例如,您可以使用std::cin读取输入。 If you know your next token is a string, give it a string: 如果您知道下一个令牌是字符串,请给它一个字符串:

std::string str;
std::cin >> str;

Or an int: 或int:

int num;
std::cin >> num;

You should check for eof and error states as usual. 您应该照常检查eof和错误状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM