Sign in to follow this  

std::stringstream C++ SIZE

This topic is 1829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

 

Share this post


Link to post
Share on other sites
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

[quote]

I suspect using std::stringstream is not big enough

[/quote]

Care to explain what did you really mean?

 

[quote]

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.

Share this post


Link to post
Share on other sites
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...
Edited by Cornstalks

Share this post


Link to post
Share on other sites
@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).

Share this post


Link to post
Share on other sites
Sign in to follow this