<?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[iterator to access list]]></title><description><![CDATA[<p>Hello,</p>
<p>I have some problems to access the list by iterator accordingly.</p>
<p>1. The list 'mylist_' and struct 'i' have to be redefined in a class file. Should it be done once in a header file? I have tried but have got a Segmentation fault error.</p>
<p>2. When I call the function 'list()', I would like to append a new entry to the list. The list size should grows up while calling list(). But this never happens, mylist_size() is always 1.</p>
<p>3. In the function 'getlist()', the iterator shows strange values of the list even it works fine in the function 'list()'.</p>
<p>Herewith, I attached an example of my code. Any suggestion, I am very welcome to hear <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>
<pre><code class="language-cpp">#aaa.h
struct mystruct {
double x
double y
}
class aaa {
public :
void list();
void getlist();
list&lt; struct mystruct &gt; mylist_;
struct mystruct i;
}
</code></pre>
<pre><code class="language-cpp">#aaa.cc
void aaa::list(){
list&lt; struct mystruct &gt; mylist_;
struct mylist i;

i.x = 10.0;
i.y = 20.0;

mylist_.push_back(i);

list&lt; struct mystruct &gt;::iterator it;
for(it = mylist_.begin() ; it != mylist_.end() ; it++) { 
cout&lt;&lt; it-&gt;x &lt;&lt; &quot; , &quot;&lt;&lt; it-&gt;y &lt;&lt; endl; &lt;-- ok!!
}
}

void aaa::getlist(){
list&lt; struct mystruct &gt;::iterator it;
for(it = mylist_.begin() ; it != mylist_.end() ; it++) {
cout&lt;&lt; it-&gt;x &lt;&lt; &quot; , &quot;&lt;&lt; it-&gt;y &lt;&lt; endl;  &lt;-- strange values!!
}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/223341/iterator-to-access-list</link><generator>RSS for Node</generator><lastBuildDate>Sat, 03 Oct 2026 23:47:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/223341.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 25 Sep 2008 05:37:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to iterator to access list on Thu, 25 Sep 2008 09:08:24 GMT]]></title><description><![CDATA[<p>Hello,</p>
<p>I have some problems to access the list by iterator accordingly.</p>
<p>1. The list 'mylist_' and struct 'i' have to be redefined in a class file. Should it be done once in a header file? I have tried but have got a Segmentation fault error.</p>
<p>2. When I call the function 'list()', I would like to append a new entry to the list. The list size should grows up while calling list(). But this never happens, mylist_size() is always 1.</p>
<p>3. In the function 'getlist()', the iterator shows strange values of the list even it works fine in the function 'list()'.</p>
<p>Herewith, I attached an example of my code. Any suggestion, I am very welcome to hear <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>
<pre><code class="language-cpp">#aaa.h
struct mystruct {
double x
double y
}
class aaa {
public :
void list();
void getlist();
list&lt; struct mystruct &gt; mylist_;
struct mystruct i;
}
</code></pre>
<pre><code class="language-cpp">#aaa.cc
void aaa::list(){
list&lt; struct mystruct &gt; mylist_;
struct mylist i;

i.x = 10.0;
i.y = 20.0;

mylist_.push_back(i);

list&lt; struct mystruct &gt;::iterator it;
for(it = mylist_.begin() ; it != mylist_.end() ; it++) { 
cout&lt;&lt; it-&gt;x &lt;&lt; &quot; , &quot;&lt;&lt; it-&gt;y &lt;&lt; endl; &lt;-- ok!!
}
}

void aaa::getlist(){
list&lt; struct mystruct &gt;::iterator it;
for(it = mylist_.begin() ; it != mylist_.end() ; it++) {
cout&lt;&lt; it-&gt;x &lt;&lt; &quot; , &quot;&lt;&lt; it-&gt;y &lt;&lt; endl;  &lt;-- strange values!!
}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1587620</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587620</guid><dc:creator><![CDATA[aboutjeab]]></dc:creator><pubDate>Thu, 25 Sep 2008 09:08:24 GMT</pubDate></item><item><title><![CDATA[Reply to iterator to access list on Thu, 25 Sep 2008 07:34:07 GMT]]></title><description><![CDATA[<p>Please, for readability of your code, activate BBCode in your postings and use the cpp-Tags instead of code-tags to switch on C/C++ syntax highlighting.</p>
<p>- the variables i and mylist_ should be made private members of your class aaa (granting public access to class member variables breaks encapsulation)<br />
- unlike in C, in C++ there is no need to qualify mystruct as struct everytime. That means, list&lt;mystruct&gt; is sufficient and the declarations of i can (or should) be abbreviated to <code>mystruct i;</code> because mystruct is a complete type specifier on its own.<br />
- the definition of mylist_ in aaa::list() defines a local variable, so the name of the member aaa.mylist_ is hidden. this is probably NOT what you want. The same applies to mystruct i. The result is that you construct a local struct, stuff it in the local list and do the output. After that at the end of aaa::list() both local variables are destroyed. Therefore, when calling aaa::getlist() that method accesses the aaa member variables that have never been touched.<br />
what actually confuses me is that you seem to get into the for-loop inaaa:getlist(). As I said, you never touched the member mylist_ (you only modified the local variable mylist_ in aaa::list()). When you say mylist_.size() is 1 everytime that means you must have put something into the list in aaa's constructor - and the &quot;strange values&quot; in getlist() are those values from the ctor.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1587678</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587678</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Thu, 25 Sep 2008 07:34:07 GMT</pubDate></item><item><title><![CDATA[Reply to iterator to access list on Thu, 25 Sep 2008 07:37:28 GMT]]></title><description><![CDATA[<p>Please read first the examples of the reference:<br />
<a href="http://www.cplusplus.com/reference/stl/list/" rel="nofollow">http://www.cplusplus.com/reference/stl/list/</a></p>
<p>Or you can google other examples with list or other keywords.</p>
<pre><code class="language-cpp">list&lt; struct mystruct &gt; mylist_;
</code></pre>
<p>You don't have to write the struct here. You just have to use the keyword struct in a definition.</p>
<pre><code class="language-cpp">list&lt; mystruct &gt; mylist_;
</code></pre>
<p>Would do this.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1587680</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587680</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 25 Sep 2008 07:37:28 GMT</pubDate></item></channel></rss>