<?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[struct in map, wie zugriff und iterator?]]></title><description><![CDATA[<p>Holla,</p>
<p>ich habe ein struct und würde das gern in eine Map bringen, ich weiß allerdings nicht wie ich die map aufbauen kann oder wie ich dann den iterator drüber laufen lasse:</p>
<pre><code>struct myStruct {
    double a;
    int b;
    char c;
};

typedef std::map&lt;myStruct, int&gt; MyMap;

//....in funktion...

MyMap mm;

//der Zugriff ab hier klappt net so
struct myStruct ms;

ms.a = 2.0;
ms.b = 8;
ms.c = 'c';

mm[ms] = 3;

//...iterieren über das map schaffe ich auch nicht...

MyMap::iterator iter

 for(iter = mm.begin(); iter != mm.end(); iter++)
{
     std::cout &lt;&lt; (*iter).first &lt;&lt; std::endl;    
}
</code></pre>
<p>leider kann mir weder sufu noch goole hier weiterhelfen.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/153408/struct-in-map-wie-zugriff-und-iterator</link><generator>RSS for Node</generator><lastBuildDate>Mon, 07 Sep 2026 07:27:59 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/153408.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 16 Jul 2006 14:44:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 14:44:12 GMT]]></title><description><![CDATA[<p>Holla,</p>
<p>ich habe ein struct und würde das gern in eine Map bringen, ich weiß allerdings nicht wie ich die map aufbauen kann oder wie ich dann den iterator drüber laufen lasse:</p>
<pre><code>struct myStruct {
    double a;
    int b;
    char c;
};

typedef std::map&lt;myStruct, int&gt; MyMap;

//....in funktion...

MyMap mm;

//der Zugriff ab hier klappt net so
struct myStruct ms;

ms.a = 2.0;
ms.b = 8;
ms.c = 'c';

mm[ms] = 3;

//...iterieren über das map schaffe ich auch nicht...

MyMap::iterator iter

 for(iter = mm.begin(); iter != mm.end(); iter++)
{
     std::cout &lt;&lt; (*iter).first &lt;&lt; std::endl;    
}
</code></pre>
<p>leider kann mir weder sufu noch goole hier weiterhelfen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1098609</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098609</guid><dc:creator><![CDATA[vani]]></dc:creator><pubDate>Sun, 16 Jul 2006 14:44:12 GMT</pubDate></item><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 15:34:57 GMT]]></title><description><![CDATA[<p>Hallo.<br />
Zunächst einmal: &quot;Geht nicht / Funktioniert nicht / Schaff ich nicht&quot; sind keine ausreichende Fehlerbeschreibungen...</p>
<p>Da eine map Schlüssel-Wert-Paare enthält, die nach den Schlüsseln sortiert sind, und da der Schlüssel-Typ in deinem Fall myStruct ist, braucht dieser natürlich auch einen Vergleichsoperator (vom &quot;strict-weak-ordering&quot;-Prinzip). Du musst deiner myStruct-Struktur also am besten einen operator&lt; spendieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1098637</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098637</guid><dc:creator><![CDATA[michba]]></dc:creator><pubDate>Sun, 16 Jul 2006 15:34:57 GMT</pubDate></item><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 15:40:01 GMT]]></title><description><![CDATA[<p>ich habe keine fehlerbeschreibung gepostet sondern um hilfe gebeten da mein Unwissen mich nicht weiterbringt. Wenn das in diesem Forum unerlaubt ist unterlasse ich das in Zukunft - ich habe gedacht solche foren sind unter anderem auch da um Hilfe zu bekommen wenn man nicht weiterkommt.</p>
<p>Leider kann ich mit der Antwort so nichts anfangen - aber ich werde weiter googeln mit stichwörtern die in der antwort auftauchen -</p>
<p>Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1098639</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098639</guid><dc:creator><![CDATA[vani]]></dc:creator><pubDate>Sun, 16 Jul 2006 15:40:01 GMT</pubDate></item><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 15:45:52 GMT]]></title><description><![CDATA[<p>Naja, wenn du es kompilierst, dürfte er dir doch einige Fehlermeldungen ausspucken?! Die könntest du hier z.B. posten.</p>
<p>zu deinem Problem:</p>
<pre><code class="language-cpp">struct myStruct {
    double a;
    int b;
    char c;

    // Vergleichsoperator, damit die map sortiert werden kann
    bool operator&lt;(const myStruct&amp; rhs) const
    {
        return a &lt; rhs.a;    // hier ggf. die Bedingung anpassen
    }
};
</code></pre>
<p>//edit: Einrückung angepasst...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1098642</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098642</guid><dc:creator><![CDATA[michba]]></dc:creator><pubDate>Sun, 16 Jul 2006 15:45:52 GMT</pubDate></item><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 16:00:06 GMT]]></title><description><![CDATA[<p>Vielen Dank!</p>
<p>Ich werde in Zukunft dann auch die Fehlermeldungen in meine Posts miteinbeziehen.<br />
Verstehe ich das jetzt richtig dass weil mein schlüsel in der map selbst definiert worden ist muss ich einen vergleichsoperator implementieren damit dann auch eine Ordnung definiert werden kann?</p>
<p>ich versuche auch gleichzeitig einen iterator auf die map anzuwenden:</p>
<pre><code class="language-cpp">MyMap::const_iterator iter;
    for(iter = m.begin(); iter != m.end(); iter++)
    {
        std::cout &lt;&lt; (*iter).first &lt;&lt; std::endl;    
    }
</code></pre>
<p>die fehlermeldungen sind wie folgt:</p>
<pre><code>error: no match for 'operator&lt;&lt;' in 'std::cout &lt;&lt; (&amp;iter)-&gt;std::_Rb_tree_iterator&lt;_Val, _Ref, _Ptr&gt;::operator* [with _Val = std::pair&lt;const Cell_Coords, int&gt;, _Ref = const std::pair&lt;const Cell_Coords, int&gt;&amp;, _Ptr = const std::pair&lt;const Cell_Coords, int&gt;*]()-&gt;std::pair&lt;const Cell_Coords, int&gt;::first'
/usr/include/c++/3.3/bits/ostream.tcc:63: error: candidates are: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;(*)(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;)) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:74: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::basic_ios&lt;_CharT, _Traits&gt;&amp;(*)(std::basic_ios&lt;_CharT, _Traits&gt;&amp;)) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:86: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::ios_base&amp;(*)(std::ios_base&amp;)) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:122: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long int) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:156: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long unsigned int) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:98: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(bool) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/ostream:178: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(short int) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/ostream:189: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(short unsigned int) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/ostream:193: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(int) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/ostream:204: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(unsigned int) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:181: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long long int) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:216: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long long unsigned int) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:241: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(double) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/ostream:219: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(float) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:265: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long double) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:289: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(const void*) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
/usr/include/c++/3.3/bits/ostream.tcc:313: error: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::basic_streambuf&lt;_CharT, _Traits&gt;*) [with _CharT = char, _Trai
</code></pre>
<p>ich dachte das evtl. jetzt auch der operator &lt;&lt; definiert werden muss...habe es so versucht z.B.:</p>
<pre><code class="language-cpp">string operator&lt;&lt;(const MyStruct&amp; rhs) const
    {
        return &quot;(&quot; + rhs.a + &quot;,&quot; + rhs.b + &quot;)&quot;;   
    }
</code></pre>
<p>Klappt wohl noch nicht so ganz....</p>
<p>Danke für Hilfe</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1098654</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098654</guid><dc:creator><![CDATA[vani]]></dc:creator><pubDate>Sun, 16 Jul 2006 16:00:06 GMT</pubDate></item><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 16:05:29 GMT]]></title><description><![CDATA[<p>tschuldigung...<br />
string gibts natürlich net als solches...</p>
<p>schaue gerade was möglich wäre.<br />
mit char kommt :</p>
<pre><code>error: invalid operands of types `const char*' and `const char[2]' to binary `operator+'
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1098658</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098658</guid><dc:creator><![CDATA[vani]]></dc:creator><pubDate>Sun, 16 Jul 2006 16:05:29 GMT</pubDate></item><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 16:26:10 GMT]]></title><description><![CDATA[<p>vani schrieb:</p>
<blockquote>
<p>Verstehe ich das jetzt richtig dass weil mein schlüsel in der map selbst definiert worden ist muss ich einen vergleichsoperator implementieren damit dann auch eine Ordnung definiert werden kann?</p>
</blockquote>
<p>Genau.</p>
<p>vani schrieb:</p>
<blockquote>
<p>ich dachte das evtl. jetzt auch der operator &lt;&lt; definiert werden muss...</p>
</blockquote>
<p>Das stimmt.<br />
Da der operator&lt;&lt; aber nicht auf deine Struktur myStruct angewandt wird, sondern auf ein std::ostream-Objekt (in deinem Fall cout), kannst du ihn nicht innerhalb deiner Struktur definieren.</p>
<pre><code class="language-cpp">std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, const myStruct&amp; rhs)
{
    out &lt;&lt; &quot;(&quot; &lt;&lt; rhs.a &lt;&lt; &quot;,&quot; &lt;&lt; rhs.b &lt;&lt; &quot;)&quot;;
    return out;
}
</code></pre>
<p>So müsste es gehen.<br />
Zur Operatoren-Überladung siehe auch <a href="http://tutorial.schornboeck.net/operatoren_ueberladung.htm" rel="nofollow">hier</a> und <a href="http://tutorial.schornboeck.net/operatoren_ueberladung2.htm" rel="nofollow">hier</a>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1098669</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098669</guid><dc:creator><![CDATA[michba]]></dc:creator><pubDate>Sun, 16 Jul 2006 16:26:10 GMT</pubDate></item><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 16:35:08 GMT]]></title><description><![CDATA[<p>vielen dank nochmals.<br />
lerne wieder was dazu danke.</p>
<p>leider beschwert er sich noch:</p>
<pre><code>std::ostream&amp;MyStruct::operator&lt;&lt;(std::ostream&amp;, const MyStruct&amp;)' must take exactly one argument
</code></pre>
<p>ich versuche mich weiter...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1098673</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098673</guid><dc:creator><![CDATA[vani]]></dc:creator><pubDate>Sun, 16 Jul 2006 16:35:08 GMT</pubDate></item><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 16:41:50 GMT]]></title><description><![CDATA[<p>tschuldigung hab überlesen dass du geschrieben hast &quot;darf es nicht in deiner struktur stehen&quot;.</p>
<p>Außerhalb definiert gehts jetzt!</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1098681</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098681</guid><dc:creator><![CDATA[vani]]></dc:creator><pubDate>Sun, 16 Jul 2006 16:41:50 GMT</pubDate></item><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 19:47:17 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>meine map sieht so aus:</p>
<pre><code class="language-cpp">struct Cell_Coords {
    int row;
    int col;

    bool operator&lt;(const Cell_Coords&amp; cc) const 
    { 
        if(row == cc.row)
            return col &lt; cc.col;   
        return row &lt; cc.row;
    } 
};

typedef std::map&lt;Cell_Coords,int&gt; MatrixMap;
</code></pre>
<p>jetzt würde ich gern testen ob die map einen gewissen schlüssel besitzt.<br />
also sowas wie :</p>
<pre><code class="language-cpp">if map.has_key(&lt;2,3&gt;) ...
</code></pre>
<p>wie schaffe ich das wenn ich obige struktur als schlüsseltyp habe?</p>
<p>Mein ansatz war folgender:</p>
<pre><code class="language-cpp">struct Cell_Coords cc;
cc.row = 0;
cc.col = 0;
int test = blur_operator[cc];
std::cout &lt;&lt; test &lt;&lt; std::endl;
</code></pre>
<p>aber so klappt es nicht mit der Meldung:</p>
<pre><code>error: passing `const MatrixMap' as `this' argument of `_Tp&amp; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::operator[](const _Key&amp;) [with _Key = Cell_Coords, _Tp = int, _Compare = std::less&lt;Cell_Coords&gt;, _Alloc = std::allocator&lt;std::pair&lt;const Cell_Coords, in
</code></pre>
<p>danke euch für Hilfe!!!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1098779</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098779</guid><dc:creator><![CDATA[vani]]></dc:creator><pubDate>Sun, 16 Jul 2006 19:47:17 GMT</pubDate></item><item><title><![CDATA[Reply to struct in map, wie zugriff und iterator? on Sun, 16 Jul 2006 19:50:35 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>siehe <a href="http://www.cppreference.com/cppmap/find.html" rel="nofollow">map::find</a>.</p>
<p>bis bald<br />
akari</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1098781</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1098781</guid><dc:creator><![CDATA[akari]]></dc:creator><pubDate>Sun, 16 Jul 2006 19:50:35 GMT</pubDate></item></channel></rss>