<?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[Probleme mit std::list]]></title><description><![CDATA[<p>Hi,</p>
<p>Meine erste frage wäre, wieso kommt er nie in den loop rein(linie 49.)? Ich verstehe einfach nicht was ich falsch mache. Meine zweite frage wäre, ich brauche zugriff auf die Player daten über jeden member. Das soll heißen, dass ich manchmal die Player Daten über die PlayerId/PlayerName usw.. kriegen muss, meine frage ist, gibt es eine leichtere methode als für jeden member eine fkt. zu schreiben? ( FindPlayerById, FindPlayerByName, FindPlayerByXCoord, usw.. ). Ja ich weiß, dass man es schön in einer Klasse haben könnte, aber der code dient nur als bsp.</p>
<pre><code class="language-cpp">#include &lt;list&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;

struct PlayerData
{
	int PlayerId;
	std::string PlayerName;
	float PlayerXCoord;
	float PlayerYCoord;

	PlayerData()
		: PlayerId(0)
		, PlayerName(&quot;&quot;)
		, PlayerXCoord(0.00)
		, PlayerYCoord(0.00)
	{

	}

};

typedef std::list&lt; PlayerData &gt; PlayerList;
typedef PlayerList::iterator PlayerListItr;
typedef PlayerList::const_iterator PlayerListConstItr;

void pauseApp()
{
    std::cin.clear();
    std::cin.ignore(std::cin.rdbuf()-&gt;in_avail());
    std::cin.get();
}

bool FindPlayerById( const int PlayerId, PlayerData &amp; tempObject )
{
	PlayerList players;
	PlayerListConstItr Itr;
	PlayerListConstItr ItrEnd;

	Itr = players.begin();
	ItrEnd = players.end();

	for(; Itr != ItrEnd; ++Itr)
	{
		std::cout &lt;&lt; &quot;found player:&quot; &lt;&lt; Itr-&gt;PlayerName; // kommt nie in den loop, wieso?
		if( Itr-&gt;PlayerId == PlayerId )
		{
			tempObject = *Itr;
			return true;
		}

	}

	return false;

}

bool FindPlayerByName( const std::string PlayerName, PlayerData &amp; tempObject )
{
	PlayerList players;
	PlayerListConstItr Itr;
	PlayerListConstItr ItrEnd;

	Itr = players.begin();
	ItrEnd = players.end();

	for(; Itr != ItrEnd; ++Itr)
	{
		if( Itr-&gt;PlayerName == PlayerName )
		{
			tempObject = *Itr;
			return true;
		}

	}

	return false;
}

void FillList()
{
	PlayerData NewPlayer;
	PlayerList Players;

    // fill the list
	NewPlayer.PlayerId     = 123;
	NewPlayer.PlayerName   = &quot;Test&quot;;
	NewPlayer.PlayerXCoord = 0.00;
	NewPlayer.PlayerYCoord = 0.00;

	Players.push_back(NewPlayer);
}

int main()
{

    FillList();
	PlayerData NewPlayer;
	// find the player by id
	bool isPlayerFoundById = FindPlayerById( 123, NewPlayer);

	// print the data
    if ( isPlayerFoundById ) 
	{
	    std::cout &lt;&lt; NewPlayer.PlayerId
		          &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerName
			      &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerXCoord
			      &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerYCoord;
	}

	else 
	{
		std::cout &lt;&lt; &quot;PlayerId: 123 does not exist&quot;;
	}

    // find the player by name
	bool isPlayerFoundByName = FindPlayerByName( &quot;test&quot;, NewPlayer);

	// print the data
	if( isPlayerFoundByName ) 
	{ 
		std::cout &lt;&lt; NewPlayer.PlayerId
		          &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerName
			      &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerXCoord
			      &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerYCoord;

	}
	else 
	{
		// some msg..
	}

	pauseApp();

}
</code></pre>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/245416/probleme-mit-std-list</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 18:28:12 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/245416.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 14 Jul 2009 00:33:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Probleme mit std::list on Tue, 14 Jul 2009 00:33:22 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>Meine erste frage wäre, wieso kommt er nie in den loop rein(linie 49.)? Ich verstehe einfach nicht was ich falsch mache. Meine zweite frage wäre, ich brauche zugriff auf die Player daten über jeden member. Das soll heißen, dass ich manchmal die Player Daten über die PlayerId/PlayerName usw.. kriegen muss, meine frage ist, gibt es eine leichtere methode als für jeden member eine fkt. zu schreiben? ( FindPlayerById, FindPlayerByName, FindPlayerByXCoord, usw.. ). Ja ich weiß, dass man es schön in einer Klasse haben könnte, aber der code dient nur als bsp.</p>
<pre><code class="language-cpp">#include &lt;list&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;

struct PlayerData
{
	int PlayerId;
	std::string PlayerName;
	float PlayerXCoord;
	float PlayerYCoord;

	PlayerData()
		: PlayerId(0)
		, PlayerName(&quot;&quot;)
		, PlayerXCoord(0.00)
		, PlayerYCoord(0.00)
	{

	}

};

typedef std::list&lt; PlayerData &gt; PlayerList;
typedef PlayerList::iterator PlayerListItr;
typedef PlayerList::const_iterator PlayerListConstItr;

void pauseApp()
{
    std::cin.clear();
    std::cin.ignore(std::cin.rdbuf()-&gt;in_avail());
    std::cin.get();
}

bool FindPlayerById( const int PlayerId, PlayerData &amp; tempObject )
{
	PlayerList players;
	PlayerListConstItr Itr;
	PlayerListConstItr ItrEnd;

	Itr = players.begin();
	ItrEnd = players.end();

	for(; Itr != ItrEnd; ++Itr)
	{
		std::cout &lt;&lt; &quot;found player:&quot; &lt;&lt; Itr-&gt;PlayerName; // kommt nie in den loop, wieso?
		if( Itr-&gt;PlayerId == PlayerId )
		{
			tempObject = *Itr;
			return true;
		}

	}

	return false;

}

bool FindPlayerByName( const std::string PlayerName, PlayerData &amp; tempObject )
{
	PlayerList players;
	PlayerListConstItr Itr;
	PlayerListConstItr ItrEnd;

	Itr = players.begin();
	ItrEnd = players.end();

	for(; Itr != ItrEnd; ++Itr)
	{
		if( Itr-&gt;PlayerName == PlayerName )
		{
			tempObject = *Itr;
			return true;
		}

	}

	return false;
}

void FillList()
{
	PlayerData NewPlayer;
	PlayerList Players;

    // fill the list
	NewPlayer.PlayerId     = 123;
	NewPlayer.PlayerName   = &quot;Test&quot;;
	NewPlayer.PlayerXCoord = 0.00;
	NewPlayer.PlayerYCoord = 0.00;

	Players.push_back(NewPlayer);
}

int main()
{

    FillList();
	PlayerData NewPlayer;
	// find the player by id
	bool isPlayerFoundById = FindPlayerById( 123, NewPlayer);

	// print the data
    if ( isPlayerFoundById ) 
	{
	    std::cout &lt;&lt; NewPlayer.PlayerId
		          &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerName
			      &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerXCoord
			      &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerYCoord;
	}

	else 
	{
		std::cout &lt;&lt; &quot;PlayerId: 123 does not exist&quot;;
	}

    // find the player by name
	bool isPlayerFoundByName = FindPlayerByName( &quot;test&quot;, NewPlayer);

	// print the data
	if( isPlayerFoundByName ) 
	{ 
		std::cout &lt;&lt; NewPlayer.PlayerId
		          &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerName
			      &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerXCoord
			      &lt;&lt; &quot;\n&quot;
			      &lt;&lt; NewPlayer.PlayerYCoord;

	}
	else 
	{
		// some msg..
	}

	pauseApp();

}
</code></pre>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1742464</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1742464</guid><dc:creator><![CDATA[Neues Thema]]></dc:creator><pubDate>Tue, 14 Jul 2009 00:33:22 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit std::list on Tue, 14 Jul 2009 01:08:11 GMT]]></title><description><![CDATA[<p>Zu deiner ersten Frage:</p>
<p>In FindPlayerById() legst du eine neue (und somit leere) Liste an:</p>
<p>PlayerList players;</p>
<p>Also kein Wunder, eine leere Liste kannst du nicht durchiterieren.<br />
Lerne erstmal mehr über die STL, dann können wir weitersehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1742466</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1742466</guid><dc:creator><![CDATA[Check0r]]></dc:creator><pubDate>Tue, 14 Jul 2009 01:08:11 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit std::list on Tue, 14 Jul 2009 01:09:20 GMT]]></title><description><![CDATA[<p>Da ich erst mal von oben nach unten angefangen habe, hier erst mal dein struct, wie ich es machen würde:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
struct PlayerData 
{ 
    int PlayerId; 
    std::string PlayerName; 
    float PlayerXCoord; 
    float PlayerYCoord; 

    PlayerData() 
    :    PlayerId(0), PlayerXCoord(0.f), PlayerYCoord(0.f)
    {}
};
</code></pre>
<p>hätte ich zwar im ctor mit standard-parametern gemacht (und den dann noch explicit gemacht), aber ansonsten würde das so zumindest sehr viel besser sein als zu vor ^^<br />
zur erklärung 0.00 ist nen double - du hast aber floats - solltest du also 2 warnings bekommen... 0.f == 0.0f -&gt; 0 als float<br />
der default-ctor von std::string macht genau das, was du willst: es wird ein leerer string erstellt...</p>
<p>zu deiner eigentlichen Frage:</p>
<pre><code class="language-cpp">bool FindPlayerById(int PlayerId, PlayerData &amp;tempObject)
{ 
    std::list &lt;PlayerData&gt; players; //ist also leer...
	for(std::list&lt;PlayerData&gt;::const_iterator i(players.begin()), e(players.end()); i != e; ++i) //wir gehen hier also eine leere liste durch...
    { 
        std::cout &lt;&lt; &quot;found player:&quot; &lt;&lt; Itr-&gt;PlayerName; //wenns nichts zum durchgehen gibt, wie oft werden wir dann hier sein...? 
        if( Itr-&gt;PlayerId == PlayerId ) 
        { 
            tempObject = *Itr; 
            return true; 
        } 
    } 
    return false; 
}
</code></pre>
<p>nen (sehr?) &quot;kluger&quot; compiler macht so was draus:</p>
<pre><code class="language-cpp">bool FindPlayerById(int PlayerId, PlayerData &amp;)
{ 
    return false; 
}
</code></pre>
<p>dein quellcode ist aber auch an und für sich... naja - nicht gerade sehr gut zu lesen, finde ich ^^<br />
ab und an sind leerzeilen ja gut und bringen übersicht - aber deshalb nur jede zweite zeile zu beschreiben sollte nicht sinn der sache sein...</p>
<p>und jetzt, wo du weist, was dort falsch ist, guck dir mal das hier an:</p>
<pre><code class="language-cpp">void FillList()
{
    PlayerData NewPlayer;
    PlayerList Players;

    // fill the list
    NewPlayer.PlayerId     = 123;
    NewPlayer.PlayerName   = &quot;Test&quot;;
    NewPlayer.PlayerXCoord = 0.00;
    NewPlayer.PlayerYCoord = 0.00;

    Players.push_back(NewPlayer);
}
</code></pre>
<p>für so etwas gibts übrigens construktoren:</p>
<pre><code class="language-cpp">PlayerData NewPlayer;
    NewPlayer.PlayerId     = 123;
    NewPlayer.PlayerName   = &quot;Test&quot;;
    NewPlayer.PlayerXCoord = 0.00;
    NewPlayer.PlayerYCoord = 0.00;
</code></pre>
<p>also versuch es mal noch mal - tipp: den container kannst du auch in der main anlegen und per (const) reference an die funktionen übergeben...</p>
<p>mal gucken - ich bin ja ma gespannt auf deine Lösung ^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1742467</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1742467</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Tue, 14 Jul 2009 01:09:20 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit std::list on Tue, 14 Jul 2009 01:47:29 GMT]]></title><description><![CDATA[<p>Check0r schrieb:</p>
<blockquote>
<p>Zu deiner ersten Frage:</p>
<p>In FindPlayerById() legst du eine neue (und somit leere) Liste an:</p>
<p>PlayerList players;</p>
<p>Also kein Wunder, eine leere Liste kannst du nicht durchiterieren.<br />
Lerne erstmal mehr über die STL, dann können wir weitersehen.</p>
</blockquote>
<p>Hast recht.. Keine ahnung wie ich so ein fehler machen konnte.. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":/"
      alt="😕"
    /></p>
<p>ty</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/16305">@unskilled</a></p>
<p>auch danke, habs atm so gelöst..</p>
<p>class.hpp</p>
<pre><code class="language-cpp">#include &lt;list&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;

struct PlayerData
{
	int PlayerId;
	std::string PlayerName;
	float PlayerXCoord;
	float PlayerYCoord;

	explicit PlayerData(int playerId, std::string playerName, float playerXcoord, float playerYcoord )
		: PlayerId(playerId)
		, PlayerName(playerName)
		, PlayerXCoord(playerXcoord)
		, PlayerYCoord(playerYcoord)
	{}

	explicit PlayerData()
        : PlayerId(0)
		, PlayerXCoord(0.f)
		, PlayerYCoord(0.f)
	{}

};

class MyPlayerClass
{
      public:
	  bool FindPlayerById(const int PlayerId, PlayerData &amp; tempObject);
	  void FillList();

      private:
      typedef std::list&lt; PlayerData &gt; PlayerList;
      typedef PlayerList::iterator PlayerListItr;
      typedef PlayerList::const_iterator PlayerListConstItr;
      PlayerList Players_;

};
</code></pre>
<p>class.cpp</p>
<pre><code class="language-cpp">#include &quot;class.hpp&quot;

bool MyPlayerClass::FindPlayerById( const int PlayerId, PlayerData &amp; tempObject )
{
    PlayerListConstItr Itr;
	PlayerListConstItr ItrEnd;

    Itr = Players_.begin();
    ItrEnd = Players_.end();

	for(; Itr != ItrEnd; ++Itr)
	{

		if( Itr-&gt;PlayerId == PlayerId )
		{
			tempObject = *Itr;
			return true;
		}

	}

	return false;

}

void MyPlayerClass::FillList()
{
	PlayerData NewPlayer(123, &quot;Test123&quot;, 1.0, 1.0);
	Players_.push_back(NewPlayer);

}
</code></pre>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &quot;class.hpp&quot;

void pauseApp()
{
    std::cin.clear();
    std::cin.ignore(std::cin.rdbuf()-&gt;in_avail());
    std::cin.get();
}

int main()
{
    MyPlayerClass NewClass;
    NewClass.FillList();

	PlayerData tempPlayer;
	NewClass.FindPlayerById(123, tempPlayer);
	std::cout &lt;&lt; tempPlayer.PlayerId;

	pauseApp();

}
}
</code></pre>
<p>Ich habe &quot;explicit&quot; benutzt, aber wo ist der unterschied? Ich merke keinen.. Und ich bräuchte hilfe bei meiner 2ten frage, ist so etwas überhaupt möglich zu basteln? ^^</p>
<p>btw:</p>
<pre><code class="language-cpp">PlayerId(0), PlayerXCoord(0.f), PlayerYCoord(0.f)
</code></pre>
<p>find ich hässlicher als</p>
<pre><code class="language-cpp">: PlayerId(0)
        , PlayerXCoord(0.f)
        , PlayerYCoord(0.f)
</code></pre>
<p>Aber das kommt ja auf die Person an, und ich mag es halt so. ^^ Trotzdem danke für deine hilfe.</p>
<p>Grüße</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1742470</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1742470</guid><dc:creator><![CDATA[Neues Thema]]></dc:creator><pubDate>Tue, 14 Jul 2009 01:47:29 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit std::list on Tue, 14 Jul 2009 07:01:36 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">struct PlayerData 
{ 
    int PlayerId; 
    std::string PlayerName; 
    float PlayerXCoord; 
    float PlayerYCoord; 

    explicit PlayerData(int playerId=0, float playerXcoord=0.f, float playerYcoord=0.f, std::string playerName=std::string) 
        : PlayerId(playerId) 
        , PlayerName(playerName) 
        , PlayerXCoord(playerXcoord) 
        , PlayerYCoord(playerYcoord) 
    {} 
};
</code></pre>
<p>jz macht das explicit auch Sinn...</p>
<p>bb</p>
<p>zu explicit - obwohl man da auch bei google ne ganze menge finden sollte...:</p>
<pre><code class="language-cpp">struct foo
{
  foo(int i=0, int j=0) {}
};

struct bar
{
  explicit bar(int i=0, int j=0) {}
};

foo GetFoo()
{
 return 1;
}
foo GetFoo2()
{
 return foo(1);
}

bar GetBar()
{
 return 1;
}
bar GetBar2()
{
 return bar(1);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1742499</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1742499</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Tue, 14 Jul 2009 07:01:36 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit std::list on Tue, 14 Jul 2009 18:31:17 GMT]]></title><description><![CDATA[<p>Ach! Macht sinn, danke!</p>
<p>Kann mir jemand meine 2te frage beantworten?</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1743006</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1743006</guid><dc:creator><![CDATA[Neues Thema]]></dc:creator><pubDate>Tue, 14 Jul 2009 18:31:17 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit std::list on Tue, 14 Jul 2009 20:41:07 GMT]]></title><description><![CDATA[<p>hättest sie ja noch mal schreiben können ^^</p>
<p>naja:</p>
<blockquote>
<p>Das soll heißen, dass ich manchmal die Player Daten über die PlayerId/PlayerName usw.. kriegen muss, meine frage ist, gibt es eine leichtere methode als für jeden member eine fkt. zu schreiben? ( FindPlayerById, FindPlayerByName, FindPlayerByXCoord, usw.. ). Ja ich weiß, dass man es schön in einer Klasse haben könnte, aber der code dient nur als bsp.</p>
</blockquote>
<p>Du kannst std::find_if verwenden und dann musst du nur noch die compare-funktoren schreiben...<br />
Irgendwer (wahrscheinlich Dravere oder vll auch Nexus) hatte mal so etwas komplett mit templates gezeigt - da war es zwar zum Sortieren aber das sollte hier auch klappen...</p>
<p>Jz hab ichs doch noch aus den Bookmarks rausgekramt:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-236521" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-236521</a><br />
es war Nexus ^^</p>
<p>Wenn du dann noch fragen dazu hast, dann zeig aber auch, dass du dich mit dem Code auseinander gesetzt hast - dann wird dir das auch jmd erklären, falls du iwas nich verstehen solltest...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1743080</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1743080</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Tue, 14 Jul 2009 20:41:07 GMT</pubDate></item></channel></rss>