简体   繁体   中英

Getting address of string in stringstream object

Is it possible to get the pointer to the string stored in string stream object . Basically i want to copy the string from that location into another buffer .. I found that i can get the length from below code

 myStringStreamObj.seekg(0, ios::end);
 lengthForStringInmyStringStreamObj = myStringStreamObj.tellg();

I know i can always do myStringStreamObj.str().c_str() . However my profiler tells me this code is taking time and i want to avoid it . hence i need some good alternative to get pointer to that string.

My profiler also tells me that another part of code where is do myStringStreamObj.str(std::string()) is slow too . Can some one guide me on this too .

Please , I cant avoid stringstream as its part of a big code which i cant change / dont have permission to change .

The answer is "no". The only documented API to obtain the formatted string contents is the str () method.

Of course, there's always a small possibility that whatever the compiler or platform you're using might have its own specific non-standard and/or non-documented methods for accessing the internals of a stringstream object; which might be faster. Because your question did not specify any particular compiler or implementation, I must conclude that you are looking for a portable, standards-compliant answer; so the answer in that case is a pretty much a "no".

I would actually be surprised if any particular compiler or platform, even some who might have a "reputation" for poisonings language standards <cough> , would offer any alternatives. I would expect that all implementations would prefer to keep the internal stringstream moving gears private, so that they can be tweaked and fiddled with, in future releases, without breaking binary ABI compatibility.

The only other possibility you might want to investigate is to obtain the contents of the stringstream using its iterators. This is, of course, an entirely different mechanism for pulling out what's in a stringstream ; and is not as straightforward as just calling one method that hands you the string, on a silver platter; so it's likely to involve a fairly significant rewrite of your code that works with the returned string. But it is possible that iterating over what's in the stringstream might turn out to be faster since, presumably, there will not be any need for the implementation to allocate a new std::string instance just for str ()'s benefit, and jamming everything inside it.

Whether or not iterating will be faster in your case depends on how your implementation's stringstream works, and how efficient the compiler is. The only way to find out is to go ahead and do it, then profile the results.

I can not provide a portable, standards compliant method.

Although you can't get the internal buffer you can provide your own.

According to the standard setting the internal buffer of a std::stringbuf object has implementation defined behaviour.

cplusplus.com: std::stringbuf::setbuf()

As it happens in my implementation of GCC 4.8.2 the behaviour is to use the external buffer you provide instead if its internal std::string .

So you can do this:

int main()
{
    std::ostringstream oss;

    char buf[1024]; // this is where the data ends up when you write to oss
    oss.rdbuf()->pubsetbuf(buf, sizeof(buf));

    oss << "Testing" << 1 << 2 << 3.2 << '\0';

    std::cout << buf; // see the data
}

But I strongly advise that you only do stuff like this as a (very) temporary measure while you sort out something more efficient that is portable according to the standard.

Having said all that I looked at how my implementation implements std::stringstream::str() and it basically returns its internal std::string so you get direct access and with optimization turned on the function calls should be completely optimized away. So this should be the preferred method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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