<?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[g++ Probleme mit Containern und Iteratoren?!]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich habe ein Problem mit meinem Compiler (glaube ich).<br />
Ich bin erst vor einem Monat auf Linux (Ubuntu 8.10) umgestiegen und bin deswegen noch nicht all zu bewandert. Zum Programmieren benutze ich das Programm Geany. Als Compiler wird der GNU Compiler benutzt.</p>
<p>Es geht um folgenden C++ Code mit Verwendung von Templates und Containern:</p>
<pre><code class="language-cpp">/*-------------------+----------------------------+--------------------+
| Ohm-Hochschule     |                            | cont1.cpp          |
| Nuernberg          |    Programmieren in C++    |--------------------|
| Peter Jesorsky     |                            | 03.07.2007         |
+--------------------+----------------------------+-------------------*/
/*
Einsatz eines vector-, list- oder deque-Containers
Die Klasse vector kann gegen list oder deque ausgetauscht werden.
*/

#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

template &lt;class T&gt; 
ostream &amp; operator&lt;&lt;(ostream &amp;os, const vector&lt;T&gt; &amp;co)
{
   // über alle Elemente iterieren
   // Wenn der Container-Inhalt nicht verändert werden darf, muss statt
   // des iterator- ein const_iterator-Objekt eingesetzt werden.
   vector&lt;T&gt;::const_iterator iter;
   for (iter = co.begin(); iter != co.end(); iter++)
      os &lt;&lt; *iter &lt;&lt; ' ';
   os &lt;&lt; endl;
   return os;
}

int main(void)
{
   vector&lt;int&gt; cont;
   vector&lt;int&gt;::iterator it;

   // Elemente hinzufügen
   cont.push_back(1);
   cont.push_back(2);
   cont.push_back(4);
   cont.push_back(5);
   cout &lt;&lt; &quot;1: &quot; &lt;&lt; cont;         // 1 2 4 5

   // auf das erste und letzte Element zugreifen
   cout &lt;&lt; &quot;2: &quot; &lt;&lt; cont.front() &lt;&lt; ' ' &lt;&lt; cont.back() &lt;&lt; endl;

   // Element 3 an Pos. 2 einfügen
   it = cont.begin();      // Pos. 0
   it++;                   // Pos. 1
   it++;                   // Pos. 2
   cont.insert(it, 3);
   cout &lt;&lt; &quot;3: &quot; &lt;&lt; cont;         // 1 2 3 4 5

   // letztes Element entfernen
   cont.pop_back(); 
   cout &lt;&lt; &quot;4: &quot; &lt;&lt; cont;         // 1 2 3 4

   // Erstes Element mit dem Wert 3 suchen und entfernen
   for (it = cont.begin(); it != cont.end(); it++)
   {
      if (*it == 3)
      {
         cont.erase(it);
         break;
      }
   }
   cout &lt;&lt; &quot;5: &quot; &lt;&lt; cont;         // 1 2 4

   // Anzahl der Elemente
   cout &lt;&lt; &quot;6: &quot; &lt;&lt; cont.empty() &lt;&lt; endl;
   cout &lt;&lt; &quot;7: &quot; &lt;&lt; cont.size() &lt;&lt; endl;

   // Alle Elemente löschen
   cont.clear();
   cout &lt;&lt; &quot;8: &quot; &lt;&lt; cont.size() &lt;&lt; endl;

   return 0;
}

/* Ausgabe:
------------------------------------------------------------------------
1: 1 2 4 5
2: 1 5
3: 1 2 3 4 5
4: 1 2 3 4
5: 1 2 4
6: 0
7: 3
8: 0
----------------------------------------------------------------------*/
</code></pre>
<p>Ich will dazu sagen, dass der Code (wie im Kommentar steht) von meinem Professor ist, und ich gehe davon aus, dass er kompilier bar ist. Leider habe ich keine Möglichkeit den Code in einem Windows Compiler zu testen.</p>
<p>Die Befehle zum compilieren sind bei mir folgendermaßen:</p>
<pre><code>g++ -Wall -c datei.c
</code></pre>
<pre><code>g++ -Wall -o datei datei.c
</code></pre>
<p>Muss ich vllt noch einen Parameter hunzufügen, wenn ich Iteratoren benutze, bzw Container?</p>
<p>Oder an was könnte es dann liegen?</p>
<p>Wäre über jeden Rat dankbar.</p>
<p>MfG, exaveal</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/243869/g-probleme-mit-containern-und-iteratoren</link><generator>RSS for Node</generator><lastBuildDate>Sat, 19 Sep 2026 16:22:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/243869.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 22 Jun 2009 12:31:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to g++ Probleme mit Containern und Iteratoren?! on Mon, 22 Jun 2009 12:31:06 GMT]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich habe ein Problem mit meinem Compiler (glaube ich).<br />
Ich bin erst vor einem Monat auf Linux (Ubuntu 8.10) umgestiegen und bin deswegen noch nicht all zu bewandert. Zum Programmieren benutze ich das Programm Geany. Als Compiler wird der GNU Compiler benutzt.</p>
<p>Es geht um folgenden C++ Code mit Verwendung von Templates und Containern:</p>
<pre><code class="language-cpp">/*-------------------+----------------------------+--------------------+
| Ohm-Hochschule     |                            | cont1.cpp          |
| Nuernberg          |    Programmieren in C++    |--------------------|
| Peter Jesorsky     |                            | 03.07.2007         |
+--------------------+----------------------------+-------------------*/
/*
Einsatz eines vector-, list- oder deque-Containers
Die Klasse vector kann gegen list oder deque ausgetauscht werden.
*/

#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

template &lt;class T&gt; 
ostream &amp; operator&lt;&lt;(ostream &amp;os, const vector&lt;T&gt; &amp;co)
{
   // über alle Elemente iterieren
   // Wenn der Container-Inhalt nicht verändert werden darf, muss statt
   // des iterator- ein const_iterator-Objekt eingesetzt werden.
   vector&lt;T&gt;::const_iterator iter;
   for (iter = co.begin(); iter != co.end(); iter++)
      os &lt;&lt; *iter &lt;&lt; ' ';
   os &lt;&lt; endl;
   return os;
}

int main(void)
{
   vector&lt;int&gt; cont;
   vector&lt;int&gt;::iterator it;

   // Elemente hinzufügen
   cont.push_back(1);
   cont.push_back(2);
   cont.push_back(4);
   cont.push_back(5);
   cout &lt;&lt; &quot;1: &quot; &lt;&lt; cont;         // 1 2 4 5

   // auf das erste und letzte Element zugreifen
   cout &lt;&lt; &quot;2: &quot; &lt;&lt; cont.front() &lt;&lt; ' ' &lt;&lt; cont.back() &lt;&lt; endl;

   // Element 3 an Pos. 2 einfügen
   it = cont.begin();      // Pos. 0
   it++;                   // Pos. 1
   it++;                   // Pos. 2
   cont.insert(it, 3);
   cout &lt;&lt; &quot;3: &quot; &lt;&lt; cont;         // 1 2 3 4 5

   // letztes Element entfernen
   cont.pop_back(); 
   cout &lt;&lt; &quot;4: &quot; &lt;&lt; cont;         // 1 2 3 4

   // Erstes Element mit dem Wert 3 suchen und entfernen
   for (it = cont.begin(); it != cont.end(); it++)
   {
      if (*it == 3)
      {
         cont.erase(it);
         break;
      }
   }
   cout &lt;&lt; &quot;5: &quot; &lt;&lt; cont;         // 1 2 4

   // Anzahl der Elemente
   cout &lt;&lt; &quot;6: &quot; &lt;&lt; cont.empty() &lt;&lt; endl;
   cout &lt;&lt; &quot;7: &quot; &lt;&lt; cont.size() &lt;&lt; endl;

   // Alle Elemente löschen
   cont.clear();
   cout &lt;&lt; &quot;8: &quot; &lt;&lt; cont.size() &lt;&lt; endl;

   return 0;
}

/* Ausgabe:
------------------------------------------------------------------------
1: 1 2 4 5
2: 1 5
3: 1 2 3 4 5
4: 1 2 3 4
5: 1 2 4
6: 0
7: 3
8: 0
----------------------------------------------------------------------*/
</code></pre>
<p>Ich will dazu sagen, dass der Code (wie im Kommentar steht) von meinem Professor ist, und ich gehe davon aus, dass er kompilier bar ist. Leider habe ich keine Möglichkeit den Code in einem Windows Compiler zu testen.</p>
<p>Die Befehle zum compilieren sind bei mir folgendermaßen:</p>
<pre><code>g++ -Wall -c datei.c
</code></pre>
<pre><code>g++ -Wall -o datei datei.c
</code></pre>
<p>Muss ich vllt noch einen Parameter hunzufügen, wenn ich Iteratoren benutze, bzw Container?</p>
<p>Oder an was könnte es dann liegen?</p>
<p>Wäre über jeden Rat dankbar.</p>
<p>MfG, exaveal</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1730967</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730967</guid><dc:creator><![CDATA[ex.aveal]]></dc:creator><pubDate>Mon, 22 Jun 2009 12:31:06 GMT</pubDate></item><item><title><![CDATA[Reply to g++ Probleme mit Containern und Iteratoren?! on Mon, 22 Jun 2009 12:35:09 GMT]]></title><description><![CDATA[<blockquote>
<p>Oder an was könnte es dann liegen?</p>
</blockquote>
<p>Funktioniert was nicht, wo ist das Problem oder was sagt der Compiler?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1730969</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730969</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Mon, 22 Jun 2009 12:35:09 GMT</pubDate></item><item><title><![CDATA[Reply to g++ Probleme mit Containern und Iteratoren?! on Mon, 22 Jun 2009 12:43:52 GMT]]></title><description><![CDATA[<p>Da du keine Fehlermeldung angibst, habe ich das mal durch den GNU-Compiler laufen lassen. Die Ausgabe ist eigentlich ziemlich eindeutig:</p>
<pre><code>test.cc: In function 'std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const std::vector&lt;T, std::allocator&lt;_CharT&gt; &gt;&amp;)':
test.cc:21: error: expected `;' before 'iter'
test.cc:22: error: 'iter' was not declared in this scope
test.cc: In function 'std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const std::vector&lt;T, std::allocator&lt;_CharT&gt; &gt;&amp;) [with T = int]':
test.cc:38:   instantiated from here
test.cc:21: error: dependent-name 'std::vector&lt;T,std::allocator&lt;_CharT&gt; &gt;::const_iterator' is parsed as a non-type, but instantiation yields a type
test.cc:21: note: say 'typename std::vector&lt;T,std::allocator&lt;_CharT&gt; &gt;::const_iterator' if a type is meant
</code></pre>
<p>Und genau das ist auch die Lösung, Zeile 21 muss</p>
<pre><code class="language-cpp">typename vector&lt;T&gt;::const_iterator iter;
</code></pre>
<p>lauten.</p>
<p>edit: Und hier noch eine ganz gute Erklärung, warum das so ist:<br />
<a href="http://pages.cs.wisc.edu/~driscoll/typename.html" rel="nofollow">http://pages.cs.wisc.edu/~driscoll/typename.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1730972</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730972</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 22 Jun 2009 12:43:52 GMT</pubDate></item><item><title><![CDATA[Reply to g++ Probleme mit Containern und Iteratoren?! on Mon, 22 Jun 2009 12:51:12 GMT]]></title><description><![CDATA[<p>dankedankedankedanke!</p>
<p>das ging ja schnell wie sonst was <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1730979</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730979</guid><dc:creator><![CDATA[ex.aveal]]></dc:creator><pubDate>Mon, 22 Jun 2009 12:51:12 GMT</pubDate></item><item><title><![CDATA[Reply to g++ Probleme mit Containern und Iteratoren?! on Mon, 22 Jun 2009 13:19:06 GMT]]></title><description><![CDATA[<p>Meine ganz persönliche und unmassgebliche Meineung: wenn schon Container und Iteratoren,</p>
<pre><code>vector&lt;T&gt;::const_iterator iter;
   for (iter = co.begin(); iter != co.end(); iter++)
      os &lt;&lt; *iter &lt;&lt; ' ';
</code></pre>
<p>dann auch Alorithmen:</p>
<pre><code>copy(co.begin(), co.end(), ostream_iterator&lt;T&gt;(os, &quot; &quot;);
</code></pre>
<p>Siehe auch: <a href="http://www.ddj.com/cpp/184401446" rel="nofollow">http://www.ddj.com/cpp/184401446</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1730985</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730985</guid><dc:creator><![CDATA[jencas]]></dc:creator><pubDate>Mon, 22 Jun 2009 13:19:06 GMT</pubDate></item><item><title><![CDATA[Reply to g++ Probleme mit Containern und Iteratoren?! on Mon, 22 Jun 2009 13:14:00 GMT]]></title><description><![CDATA[<p>ex.aveal schrieb:</p>
<blockquote>
<p>das ging ja schnell wie sonst was <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
</blockquote>
<p>die Antwort hat dir der Compiler aber schon gesagt:</p>
<pre><code>test.cc:21: note: say 'typename std::vector&lt;T,std::allocator&lt;_CharT&gt; &gt;::const_iterator' if a type is meant
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1730993</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1730993</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Mon, 22 Jun 2009 13:14:00 GMT</pubDate></item><item><title><![CDATA[Reply to g++ Probleme mit Containern und Iteratoren?! on Mon, 22 Jun 2009 13:47:38 GMT]]></title><description><![CDATA[<p>jencas schrieb:</p>
<blockquote>
<p>Meine ganz persönliche und unmassgebliche Meineung: wenn schon Container und Iteratoren,</p>
<pre><code>vector&lt;T&gt;::const_iterator iter;
   for (iter = co.begin(); iter != co.end(); iter++)
      os &lt;&lt; *iter &lt;&lt; ' ';
</code></pre>
<p>dann auch Alorithmen:</p>
<pre><code>copy(co.begin(), co.end(), ostream_iterator&lt;T&gt;(os, &quot; &quot;);
</code></pre>
<p>Siehe auch: <a href="http://www.ddj.com/cpp/184401446" rel="nofollow">http://www.ddj.com/cpp/184401446</a></p>
</blockquote>
<p>Genau - wieso soll man den Studenten am Anfang Code vorsetzen, den sie auch verstehen? Ich weiß ja nicht, nach wie vielen Monaten C++ du den copy-Weg übersichtlicher fandest, als ne normale Schleife - aber bei mir hat das ein wenig gedauert...<br />
Jetzt find ich den copy-Weg auch lesbarer und eleganter (nicht zu letzt, weil er kürzer ist) - aber am Anfang würde ich von dessen Verwendung absehen...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1731015</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1731015</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 22 Jun 2009 13:47:38 GMT</pubDate></item></channel></rss>