gexicide
gexicide

Reputation: 40098

Retrieve a char from a std::stringstream

Is there a function for getting the char at position n from a stringstream without copying to a string first and without removing the char from the buffer? In the documentation I only see a get function to retrieve the next char, but no function to access a char at a specific position.

E.g.:

std::stringstream s;
s << "abcdefg";
char c = s.getCharAt(3); // I am looking for this method
// c should be 'd' now

Upvotes: 2

Views: 2278

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153955

You can s.seekg(n, std::ios_base::cur) and read the character at the given position. You can also specify the position using std::ios_base::beg or std::ios_base::end to position relative to the begin or the end of the stream rather than the current position.

Upvotes: 4

Related Questions