<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[String zersplitten]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich hab folgendes Problem ich lese eine Datei ein die so aussieht<br />
10 ANTARCTICA 0.000 0.000<br />
12 ALGERIA 0.000 0.000<br />
16 AMERICAN SAMOA 0.000 0.000<br />
geht auch nur leider verschiebt sich alles wenn in dem ländernamen ein leerzeichen ist. hat wer ne Idee?<br />
Meine Funktion sieht so aus</p>
<pre><code>vector&lt;country&gt; read(string filename) {
	string line;
	country s;
	istringstream lin;
	vector&lt;country&gt; table;
	ifstream myfile(filename.c_str());
	if (myfile.is_open()) {
		myfile.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n'); // erste Zeile überspringen
		myfile.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
		myfile.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
		while (getline(myfile, line)) {

			lin.clear();
			lin.str(line);

			lin &gt;&gt; s.isonum &gt;&gt; s.countryname &gt;&gt; s.requirement &gt;&gt; s.consumptive;

			table.push_back(s);

		}
		myfile.close();
	}

	else{
		cerr &lt;&lt; &quot;Unable to open file: &quot; &lt;&lt; filename &lt;&lt; endl;
		exit (EXIT_FAILURE);
	}
	return table;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/323937/string-zersplitten</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 10:51:24 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/323937.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 23 Feb 2014 20:18:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to String zersplitten on Sun, 23 Feb 2014 20:18:50 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich hab folgendes Problem ich lese eine Datei ein die so aussieht<br />
10 ANTARCTICA 0.000 0.000<br />
12 ALGERIA 0.000 0.000<br />
16 AMERICAN SAMOA 0.000 0.000<br />
geht auch nur leider verschiebt sich alles wenn in dem ländernamen ein leerzeichen ist. hat wer ne Idee?<br />
Meine Funktion sieht so aus</p>
<pre><code>vector&lt;country&gt; read(string filename) {
	string line;
	country s;
	istringstream lin;
	vector&lt;country&gt; table;
	ifstream myfile(filename.c_str());
	if (myfile.is_open()) {
		myfile.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n'); // erste Zeile überspringen
		myfile.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
		myfile.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
		while (getline(myfile, line)) {

			lin.clear();
			lin.str(line);

			lin &gt;&gt; s.isonum &gt;&gt; s.countryname &gt;&gt; s.requirement &gt;&gt; s.consumptive;

			table.push_back(s);

		}
		myfile.close();
	}

	else{
		cerr &lt;&lt; &quot;Unable to open file: &quot; &lt;&lt; filename &lt;&lt; endl;
		exit (EXIT_FAILURE);
	}
	return table;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2384834</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384834</guid><dc:creator><![CDATA[Onubub]]></dc:creator><pubDate>Sun, 23 Feb 2014 20:18:50 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Sun, 23 Feb 2014 20:44:43 GMT]]></title><description><![CDATA[<pre><code>#include &lt;iostream&gt;

struct country
{
	unsigned A;
	std::string name;
        float B, C;
};

std::istream&amp; operator&gt;&gt;( std::istream&amp; is, country&amp; c )
{
	is &gt;&gt; c.A &gt;&gt; std::ws;

	c.name.clear();
	for( char ch; is.get(ch) &amp;&amp; (ch &gt;= 'A' &amp;&amp; ch &lt;= 'Z' || ch == ' ')  ; )
		c.name.push_back( ch );

	c.name.erase( c.name.find_last_not_of(' ') + 1 );

	return is &gt;&gt; c.B &gt;&gt; c.C;
}

#include &lt;vector&gt;
std::vector&lt;country&gt; parse( std::istream&amp; is )
{
	std::vector&lt;country&gt; rval;
	for( country tmp; is &gt;&gt; tmp; )
		rval.emplace_back( std::move(tmp) );

	return rval;
}

#include &lt;sstream&gt;

int main()
{
	std::istringstream stream{
R&quot;(10 ANTARCTICA 0.000 0.000
12 ALGERIA 0.000 0.000
16 AMERICAN SAMOA 0.000 0.000)&quot; };

	for( auto i : parse(stream) )
		std::cout &lt;&lt; i.name &lt;&lt; '\n';
}
</code></pre>
<p>Edit: Die Schleife lies sich natürlich stark vereinfachen (bzw. verkürzen).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384838</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384838</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sun, 23 Feb 2014 20:44:43 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Sun, 23 Feb 2014 21:38:52 GMT]]></title><description><![CDATA[<p>Hallo Onubub,</p>
<p>ich habe einen ähnlichen Weg gewählt wie Arcoth, nur mit dem Unterschied, dass ich alles als Zeichen eines Landes akzeptiere außer eben Zeichen die in einem nummerischen Ausdruck vorkommen.</p>
<p>Das Hauptprogramm ist noch strait forward:</p>
<pre><code>#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

struct Entry
{
    int nr_;
    std::string land_;
    double x_,y_;
};

std::istream&amp; operator&gt;&gt;( std::istream&amp; in, Entry&amp; e );
std::ostream&amp; operator&lt;&lt;( std::ostream&amp; out, const Entry&amp; e );

int main()
{
    using namespace std;
    ifstream file(&quot;input.txt&quot;);
    if( !file.is_open() )
    {
        cerr &lt;&lt; &quot;Error open file&quot; &lt;&lt; endl;
        return -2;
    }
    for( Entry e; file &gt;&gt; e; )
        cout &lt;&lt; e &lt;&lt; endl;
    if( file.eof() )
        cout &lt;&lt; &quot;Ok&quot; &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>Die Einlese- und Ausgaberoutine für einen Eintrag (Entry):</p>
<pre><code>template&lt; typename E, typename Traits, typename Pred &gt;
std::basic_istream&lt; E, Traits &gt;&amp; get_text( std::basic_istream&lt; E, Traits &gt;&amp; in, std::basic_string&lt; E, Traits &gt;&amp; txt, Pred end );

std::istream&amp; operator&gt;&gt;( std::istream&amp; in, Entry&amp; e )
{
    const std::ctype&lt; char &gt;&amp; ctype = std::use_facet&lt; std::ctype&lt; char &gt; &gt;( in.getloc() );
    return 
        get_text( in &gt;&gt; e.nr_, // Nummer lesen
        e.land_, [&amp;ctype]( char c )-&gt;bool { return ctype.is( std::ctype_base::digit, c ) || c == '.' || c == '-' || c == '+'; } ) // Land bis Zahlenzeichen
        &gt;&gt; e.x_ &gt;&gt; e.y_;
}

std::ostream&amp; operator&lt;&lt;( std::ostream&amp; out, const Entry&amp; e )
{
    return out &lt;&lt; e.nr_ &lt;&lt; &quot;) [&quot; &lt;&lt; e.land_ &lt;&lt; &quot;] &quot; &lt;&lt; e.x_ &lt;&lt; &quot;;&quot; &lt;&lt; e.y_;
}
</code></pre>
<p>Die Hauptarbeit steckt jetzt in der Funktion ' <code>get_text</code> ', die einen Text liest, bis eine Bedingung erfüllt ist, die über das mitgegebene Prädikat ' <code>pred</code> ' vorgegeben ist.</p>
<pre><code>template&lt; typename E, typename Traits, typename Pred &gt;
std::basic_istream&lt; E, Traits &gt;&amp; get_text( std::basic_istream&lt; E, Traits &gt;&amp; in, std::basic_string&lt; E, Traits &gt;&amp; txt, Pred end )
{
    std::basic_istream&lt; E, Traits &gt;::sentry ok( in );
    if( ok )
    {
        std::ios_base::iostate state = std::ios_base::goodbit;
        try
        {
            std::basic_string&lt; E, Traits &gt; tx;
            std::basic_string&lt; E, Traits &gt;::size_type trim_pos = 0;
            const std::ctype&lt; E &gt;&amp; ctype = std::use_facet&lt; std::ctype&lt; E &gt; &gt;( in.getloc() );
            for( Traits::int_type m = in.rdbuf()-&gt;sgetc(); ; m = in.rdbuf()-&gt;snextc() )
            {
                if( Traits::eq_int_type( m, Traits::eof() ) )
                {
                    state |= std::ios_base::eofbit;
                    break;
                }
                const E c = Traits::to_char_type( m );
                if( end( c ) )
                    break;
                tx.append( 1, c );
                if( !ctype.is( std::ctype_base::space, c ) )
                    trim_pos = tx.length();
            }
            tx.erase( trim_pos );
            txt = std::move( tx );
        }
        catch( ... )
        {
            state |= std::ios_base::badbit;
            if( in.exceptions() &amp; std::ios_base::badbit )
                throw;
        }
        in.setstate( state );
    }
    return in;
}
</code></pre>
<p>Ausgabe bei dem von Dir angegebenen Beispiel ist:</p>
<pre><code>10) [ANTARCTICA] 0;0
12) [ALGERIA] 0;0
16) [AMERICAN SAMOA] 0;0
Ok
</code></pre>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384852</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384852</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Sun, 23 Feb 2014 21:38:52 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 11:48:45 GMT]]></title><description><![CDATA[<p>Danke euch beiden ich teste gerade Arcoth version doch move kann er nicht finden?<br />
Ich nutze MinGW.<br />
error: 'class std::vector&lt;country&gt;' has no member named 'emplace_back'<br />
error: 'move' was not declared in this scope<br />
In function 'std::vector&lt;country&gt; read(std::string)':<br />
'auto' will change meaning in C++0x; please remove it [-Wc++0x-compat]<br />
'i' does not name a type</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384959</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384959</guid><dc:creator><![CDATA[Onubub]]></dc:creator><pubDate>Mon, 24 Feb 2014 11:48:45 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 11:48:48 GMT]]></title><description><![CDATA[<p>Wenn der halbwegs aktuell ist musst du noch den C++11 Modus einschalten. Der Parameter ist -std=c++0x</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384960</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384960</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Mon, 24 Feb 2014 11:48:48 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 12:04:15 GMT]]></title><description><![CDATA[<p>Ja danke, hat geklappt.</p>
<p>Ich hab nur das Problem jetzt das er wenn er 12 ALGERIA 2317065.750 5709.162 sowas einließt das nur 317065.750 die eingelesen wird</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384964</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384964</guid><dc:creator><![CDATA[Onubub]]></dc:creator><pubDate>Mon, 24 Feb 2014 12:04:15 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 12:40:24 GMT]]></title><description><![CDATA[<p>Onubub schrieb:</p>
<blockquote>
<p>Ja danke, hat geklappt.</p>
<p>Ich hab nur das Problem jetzt das er wenn er 12 ALGERIA 2317065.750 5709.162 sowas einließt das nur 317065.750 die eingelesen wird</p>
</blockquote>
<p>.. dann probiere doch mal meine Variante.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384970</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384970</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Mon, 24 Feb 2014 12:40:24 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 12:55:56 GMT]]></title><description><![CDATA[<blockquote>
<p>.. dann probiere doch mal meine Variante.</p>
</blockquote>
<p>Deine Variante ist etwas overengineered... aber das weißt du doch?</p>
<p>Der Fehler ist schnell gefunden, das erste Zeichen das nicht in den Namen passt wird mitextrahiert.</p>
<p>Der überladene Operator muss angepasst werden:</p>
<pre><code>std::istream&amp; operator&gt;&gt;( std::istream&amp; is, country&amp; c )
{
	is &gt;&gt; c.A &gt;&gt; std::ws;

	c.name.clear();
	// Edit: Für die Schleife muss ich mich entschuldigen.
	for( char ch = is.peek(); is &amp;&amp; (ch &gt;= 'A' &amp;&amp; ch &lt;= 'Z' || ch == ' ')  ; ch = is.peek() )
	{
		c.name.push_back( ch );
		is.ignore();
	}

	c.name.erase( c.name.find_last_not_of(' ') + 1 );

	return is &gt;&gt; c.B &gt;&gt; c.C;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2384972</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384972</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Mon, 24 Feb 2014 12:55:56 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 12:51:56 GMT]]></title><description><![CDATA[<p>die bekomme ich gar nicht zum laufen da bekomm ich 100 fehler</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384973</guid><dc:creator><![CDATA[Onubub]]></dc:creator><pubDate>Mon, 24 Feb 2014 12:51:56 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 12:59:49 GMT]]></title><description><![CDATA[<p>Onubub schrieb:</p>
<blockquote>
<p>die bekomme ich gar nicht zum laufen da bekomm ich 100 fehler</p>
</blockquote>
<p>Das ist äußerst merkwürdig, denn diese Funktion ist mit allen Standards kompatibel. Was für Fehler bekommst du denn?</p>
<p>Edit: Er meinte natürlich Werners Variante...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384976</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384976</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Mon, 24 Feb 2014 12:59:49 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 13:15:59 GMT]]></title><description><![CDATA[<p>Ja genau.</p>
<p>Deine Variante läuft jetzt einwandfrei! Danke.</p>
<p>Ich hab jetzt noch ne andere Datei da kommen statt Leerzeichen , Leerzeichen und dann tabs und dann leerzeichen. gibts da ne möglichkeit das das Programm beides kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384980</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384980</guid><dc:creator><![CDATA[Onubub]]></dc:creator><pubDate>Mon, 24 Feb 2014 13:15:59 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 13:17:50 GMT]]></title><description><![CDATA[<p>Onubub schrieb:</p>
<blockquote>
<p>die bekomme ich gar nicht zum laufen da bekomm ich 100 fehler</p>
</blockquote>
<p>Es fehlen ein paar typename</p>
<pre><code>template&lt; typename E, typename Traits, typename Pred &gt;
std::basic_istream&lt; E, Traits &gt;&amp; get_text( std::basic_istream&lt; E, Traits &gt;&amp; in, std::basic_string&lt; E, Traits &gt;&amp; txt, Pred end )
{
    typename std::basic_istream&lt; E, Traits &gt;::sentry ok( in );
    if( ok )
    {
        std::ios_base::iostate state = std::ios_base::goodbit;
        try
        {
            std::basic_string&lt; E, Traits &gt; tx;
            typename std::basic_string&lt; E, Traits &gt;::size_type trim_pos = 0;
            const std::ctype&lt; E &gt;&amp; ctype = std::use_facet&lt; std::ctype&lt; E &gt; &gt;( in.getloc() );
            for( typename Traits::int_type m = in.rdbuf()-&gt;sgetc(); ; m = in.rdbuf()-&gt;snextc() )
            {
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2384981</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384981</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 24 Feb 2014 13:17:50 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 14:09:01 GMT]]></title><description><![CDATA[<p>Onubub schrieb:</p>
<blockquote>
<p>Ja genau.</p>
<p>Deine Variante läuft jetzt einwandfrei! Danke.</p>
<p>Ich hab jetzt noch ne andere Datei da kommen statt Leerzeichen , Leerzeichen und dann tabs und dann leerzeichen. gibts da ne möglichkeit das das Programm beides kann?</p>
</blockquote>
<p>Meine overingenierte Variante kann das sofort, wenn Du die <code>typename</code> 's noch einfügst, so wie camper es beschrieben hat.</p>
<p>camper schrieb:</p>
<blockquote>
<p>Onubub schrieb:</p>
<blockquote>
<p>die bekomme ich gar nicht zum laufen da bekomm ich 100 fehler</p>
</blockquote>
<p>Es fehlen ein paar typename</p>
</blockquote>
<p>.. das ist das Kreuz mit dem Compiler vom Visual Studio, der ist da großzügig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384990</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384990</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Mon, 24 Feb 2014 14:09:01 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 14:55:51 GMT]]></title><description><![CDATA[<p>Auf die Erfinder und Standardisierer der C++ Streams warten ein paar ganz besondere Qualen in der Hölle.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2385008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2385008</guid><dc:creator><![CDATA[qweasdyxc]]></dc:creator><pubDate>Mon, 24 Feb 2014 14:55:51 GMT</pubDate></item><item><title><![CDATA[Reply to String zersplitten on Mon, 24 Feb 2014 17:14:16 GMT]]></title><description><![CDATA[<p>Moment mal! Das kann meine Variante auch, man muss es nur so anpassen:</p>
<pre><code>for( char ch = is.peek(); is &amp;&amp; ch != '+' &amp;&amp; ch != '-' &amp;&amp; ch != '.' &amp;&amp; !std::isdigit(ch) ; ch = is.peek() )
	{
		c.name.push_back( ch );
		is.ignore();
	}
</code></pre>
<p>Edit: Das war die falsche.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2385050</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2385050</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Mon, 24 Feb 2014 17:14:16 GMT</pubDate></item></channel></rss>