Stringstreams in C++ [] (Stringstreams in C++), Lektion, page 722665
https://www.purl.org/stefan_ram/pub/stringstream_c++ (permalink) is the canonical URI of this page.
Stefan Ram
C++-Kurs

Stringstreams in C++ 

Teilnehmerfrage Wie Text in Wörter zerlegen?

#include <iostream> /* ::std::cout */

#include <ostream> /* << */

#include <string> /* << */

#include <sstream>

int main()

{ ::std::stringstream source;

source << "alpha beta gamma";

::std::string word;

while( source >> word )::std::cout << word << '\n'; }

Main.java

#include <iostream>

#include <ostream>

#include <string>

#include <iterator>

#include <algorithm>

#include <vector>

#include <initializer_list>

#include <sstream>

int main()

{ ::std::string const source = "Haus Hof Haus Haus Garten";

::std::stringstream stream( source );

::std::vector<::std::string> words
( std::istream_iterator<std::string>{ stream },
std::istream_iterator<std::string>{} );

::std::sort( ::std::begin( words ), ::std::end( words ) );

::std::unique_copy

( ::std::begin( words ), ::std::end( words),

::std::ostream_iterator< ::std::string >( ::std::cout, "\n" ) );}

Garten

Haus

Hof

Beispiel

text mit Zeilen der Form

a=b

interpretieren

std::istringstream istringstream_object( text );

std::string line;

while( std::getline(istringstream_object, line).good() )

{ auto start_of_line_position = begin( line );

auto end_of_line_position = end( line );

auto equal_sign_position = find( start_of_line_position, end_of_line_position, '=' );

std::string const keyword( start_of_line_position, equal_sign_position );

auto value_position = start_of_line_position != end_of_line_position ? equal_sign_position + 1 : equal_sign_position;

std::string value(value_position, end_of_line_position); }


Abschnitt Einlesen mit getline

getline liest normalerweise eine Zeile ein.

#include <iostream>
#include <ostream>
#include <string>
int main()
{ ::std::string str;
getline( ::std::cin, str );
::std::cout << str << '\n'; }

::std::cout

1

1

Es kann aber auch ein andere Separator als weiteres drittes Argument von getline angegeben werden. Das folgende Programm addiert plusseparierte Zahlen jeder Zeile. (Allerdings ohne Behandlung von Eingabefehlern.)

#include <iostream>

#include <ostream>

#include <string>

#include <sstream>

int main()

{ for( ::std::string input; getline( ::std::cin, input ); )

{ ::std::stringstream source; source << input;

int sum = 0;

while( getline( source, input, '+' ))sum += ::std::stoi( input );

::std::cout << sum << '\n'; }}

2+3

5

1+22+333+4444

4800

Problemlösung fehlende to_string-Funktion

#include <iostream>

#include <ostream>

#include <sstream>

#include <initializer_list>

template< typename V >std::string to_string( V v )

{ ::std::stringstream s; s << v; return s.str(); }

int main()

{ auto s = to_string( 22 ); ::std::cout << s + "a" << '\n'; }

Automatisches Formatieren von Tabellen

main.cpp

#include <sstream>
#include <iomanip>
#include <vector>
#include <algorithm>

#include <iostream>
#include <ostream>

static ::std::string align_columns( ::std::string const & str )
{ /* Based on code posted to comp.lang.c++ by Ben Bacarisse in 2017*/
::std::vector< ::std::string::size_type >widths;
::std::istringstream is( str );
::std::string word;

{ unsigned col = 0;
while( is >> word )
{ ::std::clog << "read \"" << word << "\".\n";
if( col >= widths.size() )
widths.push_back( word.length() );
else widths[ col ]= ::std::max( widths[ col ], word.length() );
::std::clog << "peek is \"" <<( is.peek()== '\n' ? "\\n" : " " )<< "\".\n";
col = is.peek() == '\n' ? 0 : col + 1; }}

is.clear();
is.seekg( 0 );

std::ostringstream os;
os << ::std::left;

{ unsigned col = 0;
while( is >> word )
{ os <<
::std::setw( static_cast< int >( widths[ col ]))<<
word <<
static_cast< char >( is.peek() );
col = is.peek() == '\n' ? 0 : col + 1; }}

return os.str(); }

int main()
{ std::string source = R"(
Alpha Beta
Epsilon Eta
)";

::std::cout << align_columns( source ); }

transcript

Alpha Beta

Epsilon Eta

read "Alpha".

peek is "32".

read "Beta".

peek is "10".

read "Epsilon".

peek is "32".

read "Eta".

peek is "10".

main.cpp

#include <sstream>
#include <iomanip>
#include <vector>
#include <algorithm>

static ::std::string align_columns( ::std::string const & str )
{ /* Based on code by Ben Bacarisse posted to comp.lang.c++ in 2017 */
::std::vector< ::std::string::size_type >widths;
::std::istringstream is( str );
::std::string word;

{ unsigned col = 0;
while( is >> word )
{ if( col >= widths.size() )
widths.push_back( word.length() );
else widths[ col ]= ::std::max( widths[ col ], word.length() );
col = is.peek() == '\n' ? 0 : col + 1; }}

is.clear();
is.seekg( 0 );

std::ostringstream os;
os << ::std::left;

{ unsigned col = 0;
while( is >> word )
{ os <<
::std::setw( static_cast< int >( widths[ col ]))<<
word <<
static_cast< char >( is.peek() );
col = is.peek() == '\n' ? 0 : col + 1; }}

return os.str(); }

#include <iostream>

int main()
{ std::string source = R"(
Alpha Beta Gamma Delta Epsilon
Zeta Eta Theta Iota Kappa
Lambda My Ny Xi Omikron
Pi Rho Sigma Tau Ypsilon
Phi Chi Psi Omega
)";

::std::cout << align_columns( source ); }

transcript

Alpha Beta Gamma Delta Epsilon

Zeta Eta Theta Iota Kappa

Lambda My Ny Xi Omikron

Pi Rho Sigma Tau Ypsilon

Phi Chi Psi Omega

About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email-address of Stefan Ram.   |   A link to the start page of Stefan Ram appears at the top of this page behind the text "Stefan Ram".)  |   Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram722665 stefan_ram:722665 Stringstreams in C++ Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd722665, slrprddef722665, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram.
https://www.purl.org/stefan_ram/pub/stringstream_c++