Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ string/stream

Started by Dario Oliveri Jun 11, 2013 at 10:52 PM 7 replies 1.6k views
Original Post
Dario Oliveri
Dario Oliveri

Hi I was wandering the C++ STL's reference and I think I'm missing something

Actually I have a InputStream (ifstream) and I want to read exactly N characters from the stream, but I don't want to put them into a char[N] buffer, but I want to put them inside a std::string (eventually by first calling "str.resize(N)")

Wich is the fastest way for doing that?

actually I'm doing


for( int i=0;i<N;i++){
    char c;
    file->read(&c,sizeof(char));
    str.push_back(c);
}

but it is really really slow :(

Peace and love, now I understand really what it means! Guardian Angels exist! Thanks!
Brother Bob
Brother Bob

Resize the string and read all characters at once.


str.resize(N);
file.read(&str[0], N);
Dario Oliveri
Dario Oliveri

oh great! never thought was so simple! thanks!:)

Peace and love, now I understand really what it means! Guardian Angels exist! Thanks!
swiftcoder
swiftcoder

Resize the string and read all characters at once.

str.resize(N);
file.read(&str[0], N);


Is this behaviour guaranteed by the standard? I seem to recall std::string not requiring contiguous storage in all implementations.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
King Mir
King Mir

Resize the string and read all characters at once.


str.resize(N);
file.read(&str[0], N);
Is this behaviour guaranteed by the standard? I seem to recall std::string not requiring contiguous storage in all implementations.

std::string is implicitly continuous by virtue of c_str() being specified as O(1), IIRC.

swiftcoder
swiftcoder




std::string is implicitly continuous by virtue of c_str() being specified as O(1), IIRC.

I'm particularly thinking of small string optimisation.

I'm assuming that the call to resize(N) saves us here, because it will set the string up use either the small buffer or the dynamically-allocated buffer, but not both?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
King Mir
King Mir

std::string is implicitly continuous by virtue of c_str() being specified as O(1), IIRC.

I'm particularly thinking of small string optimisation.

I'm assuming that the call to resize(N) saves us here, because it will set the string up use either the small buffer or the dynamically-allocated buffer, but not both?

Yes it does. Small sting optimisation uses a continuous buffer. It's just part of the object instead of seperately allocated.

Brother Bob
Brother Bob

Resize the string and read all characters at once.


str.resize(N);
file.read(&str[0], N);
Is this behaviour guaranteed by the standard? I seem to recall std::string not requiring contiguous storage in all implementations.

Can't speak for earlier versions, but the C++11 draft says the following (section 21.4.1.5):



The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size().

And, just to be complete, operator[] is described in terms of begin() and an offset.

Bregma
Bregma




Is this behaviour guaranteed by the standard? I seem to recall std::string not requiring contiguous storage in all implementations.

The contiguous property of std::string and std::vector was made an explicit requirement in C++03. That was one of the bigger changes from C++98.

Stephen M. Webb
Professional Free Software Developer

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.