stringstream的使用_@pegasus.rutgers.edu

The stringstream is a class that is useful for extracting data from or writing formatted data to strings.

A very common question, for example is this: ``How do I convert a string to a number ? ''. Of course, there's no way to do so in general, since not all strings look like numbers. But it's certainly not unusual that we'd want to extract a number from a string. Note that the g++ compiler ships without the sstream header. You can download it from here Here's how one does it:
stringstream1.cc

FAQ: How Do I Convert String To Number ?


#include <iostream>
#include <sstream>
using namespace std;
int main()
{
	int a;
	string s = "456";
	istringstream sin(s);
	sin >> a;
	cout << a << endl;
	return 0;
}

The istringstream class is an input stream attached to a string. The constructor copies the string s into its private buffer, and then we may extract data from it using regular stream semantics, in this case we use sin>>a.

FAQ: How Do I Convert A Number To A String ?

or how do I do ``sprintf'' in C++ ?

You may have guessed -- the answer is to use an ostringstream. This data type behaves in a similar way to the istringstream. The main difference is that there is an extra method, str(). This method returns the string that lies in the ostringstream object. There's also a verision of str() that takes a string argument -- this initialises the streams underlying string buffer to that argument. This is commonly used to clear a stream for reuse -- one can call mystream.str("");

stringstream4.cc

#include <sstream>
#include <iostream>
int main()
{
    std::ostringstream strout;
    int x = 42;
    strout << "The answer to the question is " << 42 << std::endl;
    cout << strout.str() << endl;
    strout.str("");
    strout << 53.2;
    cout << strout.str() << endl;
    return 0;
}

Using Stringstreams To Parse Input

A problem that often comes up is this: suppose you have written the following code:

stringstream2.cc

#include <iostream>
	
using namespace std;
int main()
{
    int x;
    do
        cout << "Enter a positive integer (0 to quit)" << endl;
    while ( cin >> x && x != 0 );
    return 0;
}

 

What happens if the user enters

2 3
Or worse, if they enter:
5.0
The problem is that the extraction operator does not expect each item extracted to be on a seperate line. So if you do expect this, you need to be explicit about it in your code. The way to do this is use getline() to read a line of input into a string and then use that string to create an istringstream from which we can extract data. After we've extracted the data we need, we can check for trailing garbage. Here's an example:
stringstream3.cc

#include <sstream>
#include <iostream>

using std::cin;
using std::cout;
using std::end;

int main()
{
    int x;
    char ch;
    std::string myString;
    while (getline ( cin, myString ))
    {
        std::istringstream strin(myString);
        strin >> x;
        if (!strin)
            cout << "Bad input" << endl;
        else if ( strin >> ch )
            cout << "Bad input" << endl;
        else
            cout << "You entered: " << x << endl;
    }
    return 0;
}


Some notes:
  • The istringstream object is declared within the loop, so its scope is the block of the loop. So a new istringstream object is created and the old one is destroyed for each iteration of the loop.
  • The test (!strin) checks to see if the stream is in an error state.
  • The attempt to extract a character is a test to see if there's anything left in the stream after we extract an integer. Note that this ignores whitespace (which is the desired effect in this case)
  • If there are no problems, then the extraction was succesful.

strstream considered harmful

There exists a deprecated class similar to stringstream that is called strstream. Do not confuse these two classes. They are not the same. strstream has a very error prone interface because of the way it handles memory. One problem is that it does not append trailing nulls when str() is called. So you must append a trailing null, or std::ends. Another important thing to remember is that if you use ostrstream with a dynamic buffer, like this:

std::ostrstream strout;
strout << "The answer is ... " << 42 << std::endl << std::ends;
strout.str();
Then calling str has the peculiar side effect that the caller is responsible for managing the memory allocated by strout's buffer, which is pretty silly since the caller does not know how the memory was allocated (it may be allocated using malloc() or new). So to make the stupid thing take its memory back, one makes the following call:
strout.freeze(0);

 

It's worth mentioning that there is another, safer way to use ostrstream and that is to use it with a static buffer. If you do this, you don't need to deal with this freeze() nonsense. To do this, call the constructor that takes a character array as an argument.

char a[100];
std::ostrstream strout(a,100);
strout << "the answer is" << 42 << std::endl << std::ends;
std::cout << a << std::endl;
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值