简体   繁体   English

将string转换为int并使用stringstream获取C ++中消耗的字符数

[英]Convert string to int and get the number of characters consumed in C++ with stringstream

I am new to C++ (coming from a C# background) and am trying to learn how to convert a string to an int. 我是C ++的新手(来自C#背景),正在尝试学习如何将字符串转换为int。

I got it working by using a stringstream and outputting it into a double , like so: 我通过使用stringstream并将其输出为double来使其工作,如下所示:

const char* inputIndex = "5+2";
double number = 0;
stringstream ss(inputIndex);
ss >> number;
// number = 5

This works great. 这很好。 The problem I'm having is that the strings I'm parsing start with a number, but may have other, not digit characters after the digits (eg "5+2", "9-(3+2)", etc). 我遇到的问题是,我解析的字符串以数字开头 ,但是在数字之后可能还有其他而非数字字符(例如“ 5 + 2”,“ 9-(3 + 2)”等) 。 The stringstream parses the digits at the beginning and stops when it encounters a non-digit, like I need it to. stringstream解析数字开头,当它遇到一个非数字,就像我需要它停止。

The problem comes when I want to know how many characters were used to parse into the number. 当我想知道使用多少个字符解析成数字时,问题就来了。 For example, if I parse 25+2 , I want to know that two characters were used to parse 25 , so that I can advance the string pointer. 例如,如果我解析25+2 ,我想知道使用了两个字符来解析25 ,以便我可以前进字符串指针。

So far, I got it working by clearing the stringstream , inputting the parsed number back into it, and reading the length of the resulting string: 到目前为止,我通过清除stringstream ,将解析后的数字输入其中,并读取结果字符串的长度来使其工作:

ss.str("");
ss << number;

inputIndex += ss.str().length();

While this does work, it seems really hacky to me (though that might just be because I'm coming from something like C#), and I have a feeling that might cause a memory leak because the str() creates a copy of the string. 虽然这样做确实可行,但对我来说似乎真的很hack(尽管可能只是因为我来自C#之类的东西),并且我有一种可能会导致内存泄漏的感觉,因为str()创建了字符串的副本。

Is there any other way to do this, or should I stick with what I have? 还有其他方法可以做到吗,还是我应该坚持自己拥有的东西?

Thanks. 谢谢。

You can use std::stringstream::tellg() to find out the current get position in the input stream. 您可以使用std::stringstream::tellg()找出输入流中的当前获取位置。 Store this value in a variable before you extract from the stream. 从流中提取之前,将此值存储在变量中。 Then get the position again after you extract from the stream. 从流中提取后,再获取位置。 The difference between these two values is the number of characters extracted. 这两个值之间的差异是提取的字符数。

double x = 3435;
std::stringstream ss;
ss << x;

double y;
std::streampos pos = ss.tellg();
ss >> y;
std::cout << (ss.tellg() - pos) << " characters extracted" << std::endl;

The solution above using tellg() will fail on modern compilers (such as gcc-4.6). 上面使用tellg()的解决方案在现代编译器(例如gcc-4.6)上将失败。

The reason for this is that tellg() really shows the position of the cursor, which is now out of scope. 原因是tellg()确实显示了光标的位置,该光标现在不在范围内。 See eg " file stream tellg/tellp and gcc-4.6 is this a bug? " 参见例如“ 文件流tellg / tellp和gcc-4.6,这是一个错误吗?

Therefore you need to also test for eof() (meaning the entire input was consumed). 因此,您还需要测试eof()(这意味着整个输入已被消耗)。

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

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