std::stringstream C++ SIZE

Started by
5 comments, last by Cornstalks 11 years, 3 months ago

Hi

I suspect using std::stringstream is not big enough. Could that be? In fact I need to store around 20'000 characters. How to set std::stringstream size?

Many thanks

Advertisement

The string stream has no size limitations.

Hi

I suspect using std::stringstream is not big enough. Could that be? In fact I need to store around 20'000 characters. How to set std::stringstream size?

Many thanks

I suspect using std::stringstream is not big enough

[/quote]

Care to explain what did you really mean?

How to set std::stringstream size?

[/quote]

You are not allowed to manually set size as far as i know, but you can manipulate objects stored in it so you control its size indirectly.

But what you can do is reserve memory for 20000 characters, to easy process of reallocation for space each time you add character.


Michaels-MacBook-Pro:~ mjbshaw$ cat t.cpp
#include <iostream>
#include <sstream>
 
int main()
{
    std::stringstream ss;
    for (int i = 0; i < 20000; ++i)
        ss << i << '\n';
 
    std::string str = ss.str();
 
    std::cout << "There are " << str.size() << " characters in ss and str\n";
    std::cout << "The contents of ss and str are:\n" << str;
}
Michaels-MacBook-Pro:~ mjbshaw$ g++ t.cpp && ./a.out 
There are 108890 characters in ss and str
The contents of ss and str are:
0
1
2
3
4
[...] (I'm not going to post all 20,000 numbers)
Seems to work just fine for me... I got 108,890 characters in ss and str...
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

@Cornstalks: how many characters in str total though? Also how much would be the sizeof occupied. would it be depending on systems configuration.

I raised the loop and hehe, it did go up to 438888890 for i < 50000000. I gueess its is has no size limitations. :)

@Cornstalks: how many characters in str total though? Also how much would be the sizeof occupied. would it be depending on systems configuration.

Crap. I'm sorry, I tapped my mousepad and didn't realize my mouse was right over the "vote down" button and accidentally gave this post a -1. I've tried to counter-act that accident by voting up the post below it. Sorry!

Now to respond to your question: There is no set size limit for std::string (or any container, for that matter). The limit is your system. If std::string needs more memory, it will request more memory from the operating system. If the operating system has it, it will give it more. If it doesn't, then your program will fail (well, technically a std::bad_alloc exception will be thrown).

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement