<?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[Generischer Iterator]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich habe 2 Schleifen ueber STL Container. Einmal ueber eine Map und einmal ueber eine List:</p>
<pre><code class="language-cpp">map&lt;int, Foo&gt;::iterator it1 = myMap.begin();
for(; it1 != myMap.end(); ++it1) {
   const Foo&amp; f = it1-&gt;second;  // A
   ...
}

list&lt;Foo&gt;::iterator it2 = myList.begin();
for(; it2 != myList.end(); ++it2) {
    const Foo&amp; f = *it2; // B
}
</code></pre>
<p>In beiden Schleifen hol ich mir das Foo Objekt und arbeite damit. Nur die Dereferenzierung in Zeilen A und B ist anders. Ich will es so haben, das beide Schleifen gleich aussehen. Aber ich weiss nicht wie das geht, da ich bei einem Map Iterator ja anders an das Foo komme als bei einem list iterator.<br />
Wie koennte ich das generalisieren?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/286769/generischer-iterator</link><generator>RSS for Node</generator><lastBuildDate>Thu, 20 Aug 2026 19:08:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/286769.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 16 May 2011 10:50:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Generischer Iterator on Mon, 16 May 2011 10:50:33 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich habe 2 Schleifen ueber STL Container. Einmal ueber eine Map und einmal ueber eine List:</p>
<pre><code class="language-cpp">map&lt;int, Foo&gt;::iterator it1 = myMap.begin();
for(; it1 != myMap.end(); ++it1) {
   const Foo&amp; f = it1-&gt;second;  // A
   ...
}

list&lt;Foo&gt;::iterator it2 = myList.begin();
for(; it2 != myList.end(); ++it2) {
    const Foo&amp; f = *it2; // B
}
</code></pre>
<p>In beiden Schleifen hol ich mir das Foo Objekt und arbeite damit. Nur die Dereferenzierung in Zeilen A und B ist anders. Ich will es so haben, das beide Schleifen gleich aussehen. Aber ich weiss nicht wie das geht, da ich bei einem Map Iterator ja anders an das Foo komme als bei einem list iterator.<br />
Wie koennte ich das generalisieren?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2063974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2063974</guid><dc:creator><![CDATA[generalAD]]></dc:creator><pubDate>Mon, 16 May 2011 10:50:33 GMT</pubDate></item><item><title><![CDATA[Reply to Generischer Iterator on Mon, 16 May 2011 11:00:18 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template&lt;class T&gt;
const Foo&amp; GetMyFoo(T&amp; Iter)
{
  static_assert(false, &quot;Ich kenne den Iterator nicht...&quot;);
}
template&lt;&gt;
const Foo&amp; GetMyFoo&lt;std::list&lt;Foo&gt;::iterator&gt;(std::list&lt;Foo&gt;::iterator&amp; Iter)
{
  return *Iter;
}
template&lt;class T&gt;
const Foo&amp; GetMyFoo&lt;std::map&lt;int, Foo&gt;::iterator&gt;(std::map&lt;int, Foo&gt;::iterator&amp; Iter)
{
  return Iter-&gt;second;
}
</code></pre>
<p>EDIT: Relativ schmierige Lösung, weil er bei <code>std::map</code> nur <code>int</code> unterstützt und weil die <code>const_</code> - und <code>reverse_</code> -Versionen der Iterator nicht kennt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2063977</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2063977</guid><dc:creator><![CDATA[EOutOfResources]]></dc:creator><pubDate>Mon, 16 May 2011 11:00:18 GMT</pubDate></item><item><title><![CDATA[Reply to Generischer Iterator on Mon, 16 May 2011 11:17:34 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template&lt;class T&gt;
T&amp; second(T&amp; t) { return t; }

template&lt;class T, class U&gt;
U&amp; second(std::pair&lt;T, U&gt;&amp; x) { return x.second; }

template&lt;class Iter&gt;
Foo&amp; get_foo(Iter&amp; iter) { return second(*iter); }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2063990</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2063990</guid><dc:creator><![CDATA[ipsec]]></dc:creator><pubDate>Mon, 16 May 2011 11:17:34 GMT</pubDate></item><item><title><![CDATA[Reply to Generischer Iterator on Mon, 16 May 2011 11:22:32 GMT]]></title><description><![CDATA[<p>EOutOfResources schrieb:</p>
<blockquote>
<pre><code class="language-cpp">template&lt;class T&gt;
const Foo&amp; GetMyFoo(T&amp; Iter)
{
  static_assert(false, &quot;Ich kenne den Iterator nicht...&quot;);
}
template&lt;&gt;
const Foo&amp; GetMyFoo&lt;std::list&lt;Foo&gt;::iterator&gt;(std::list&lt;Foo&gt;::iterator&amp; Iter)
{
  return *Iter;
}
template&lt;class T&gt;
const Foo&amp; GetMyFoo&lt;std::map&lt;int, Foo&gt;::iterator&gt;(std::map&lt;int, Foo&gt;::iterator&amp; Iter)
{
  return Iter-&gt;second;
}
</code></pre>
<p>EDIT: Relativ schmierige Lösung, weil er bei <code>std::map</code> nur <code>int</code> unterstützt und weil die <code>const_</code> - und <code>reverse_</code> -Versionen der Iterator nicht kennt.</p>
</blockquote>
<p>Das hätte man auch mit normaler Funktionsüberladung machen können...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2063991</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2063991</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 16 May 2011 11:22:32 GMT</pubDate></item><item><title><![CDATA[Reply to Generischer Iterator on Mon, 16 May 2011 11:33:31 GMT]]></title><description><![CDATA[<p>ipsec schrieb:</p>
<blockquote>
<pre><code class="language-cpp">template&lt;class T&gt;
T&amp; second(T&amp; t) { return t; }

template&lt;class T, class U&gt;
U&amp; second(std::pair&lt;T, U&gt;&amp; x) { return x.second; }

template&lt;class Iter&gt;
Foo&amp; get_foo(Iter&amp; iter) { return second(*iter); }
</code></pre>
</blockquote>
<p>Perfekt! Vielen Dank! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2063996</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2063996</guid><dc:creator><![CDATA[generalAD]]></dc:creator><pubDate>Mon, 16 May 2011 11:33:31 GMT</pubDate></item><item><title><![CDATA[Reply to Generischer Iterator on Mon, 16 May 2011 11:57:46 GMT]]></title><description><![CDATA[<p>Ohne gewähr...</p>
<pre><code class="language-cpp">#include &lt;iterator&gt;
#include &lt;map&gt;
#include &lt;list&gt;
#include &lt;iostream&gt;

using namespace std;

template &lt;class Pair&gt; struct PairTraits;
template &lt;class First, class Second&gt;
struct PairTraits&lt;std::pair&lt;First, Second&gt; &gt;
{
  typedef First first_t;
  typedef Second second_t;
};

template &lt;class Iterator&gt;
struct PairAccessSecondPolicy
{
  typedef typename PairTraits&lt;typename iterator_traits&lt;Iterator&gt;::value_type&gt;::second_t value_type;
  typedef value_type&amp; reference;
  typedef value_type* pointer;

  static reference access(Iterator const&amp; it)
  {
    return it-&gt;second;
  }
  static pointer dereference(Iterator const&amp; it)
  {
    return &amp;(it-&gt;second);
  }
};

template &lt;class Iterator, class AccessPolicy&gt;
struct AccessingIterator : public Iterator
{
  typedef typename iterator_traits&lt;Iterator&gt;::difference_type difference_type;
  typedef typename iterator_traits&lt;Iterator&gt;::iterator_category iterator_category;
  typedef typename AccessPolicy::value_type value_type;
  typedef typename AccessPolicy::reference reference;
  typedef typename AccessPolicy::pointer pointer;

  AccessingIterator(Iterator const&amp; it) : Iterator(it) {}

  reference operator* ()
  {
    return AccessPolicy::access(*this);
  }

  pointer operator-&gt; ()
  {
    return AccessPolicy::dereference(*this);
  }
};

template &lt;class Map&gt;
struct MapSecondAccessor
{
  typedef typename Map::iterator MapIter;
  typedef AccessingIterator&lt;MapIter, PairAccessSecondPolicy&lt;MapIter&gt; &gt; type;
};

struct Foo { int i; };

int main()
{
  map&lt;int, Foo&gt; myMap;
  list&lt;Foo&gt; myList;
  for (int j = 0; 0 &lt; 5; ++j)
  {
    myMap[j].i = 5-j;
    myList.push_back(myMap[j]);
  }

  MapSecondAccessor&lt;map&lt;int, Foo&gt; &gt;::type it1 = myMap.begin();
  for(; it1 != myMap.end(); ++it1) {
    const Foo&amp; f = *it1;  // A
    cout &lt;&lt; f.i &lt;&lt; ' ';
  }
  cout &lt;&lt; '\n';

  list&lt;Foo&gt;::iterator it2 = myList.begin();
  for(; it2 != myList.end(); ++it2) {
    const Foo&amp; f = *it2; // B
    cout &lt;&lt; f.i &lt;&lt; ' ';
  }
  cout &lt;&lt; '\n';
}
</code></pre>
<p>compiliert zumindest - weiter testen konnte ichs nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064003</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 16 May 2011 11:57:46 GMT</pubDate></item><item><title><![CDATA[Reply to Generischer Iterator on Mon, 16 May 2011 21:22:48 GMT]]></title><description><![CDATA[<p>Man kann auch einfach <code>boost::make_transform_iterator</code> verwenden.<br />
Beispiel:</p>
<p><a href="http://stackoverflow.com/questions/259240/iterator-adapter-to-iterate-just-the-values-in-a-map" rel="nofollow">http://stackoverflow.com/questions/259240/iterator-adapter-to-iterate-just-the-values-in-a-map</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064353</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064353</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 16 May 2011 21:22:48 GMT</pubDate></item></channel></rss>