Boost C++ Libraries: Ticket #7828: use BOOST_FOREACH push_back some thing into vector,the first value is blank. https://svn.boost.org/trac10/ticket/7828 <p> When I use BOOST_FOREACH push_back value into std::vector,the first value is blank.After this I have to erase the first element. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7828 Trac 1.4.3 anonymous Thu, 27 Dec 2012 01:20:27 GMT <link>https://svn.boost.org/trac10/ticket/7828#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7828#comment:1</guid> <description> <p> I use BOOST_FOREACH paser a xml document.The xml document like this: &lt;c&gt; </p> <blockquote> <p> &lt;client&gt;00801038&lt;/client&gt; &lt;client&gt;00802980&lt;/client&gt; &lt;client&gt;01600817&lt;/client&gt; </p> </blockquote> <p> &lt;/client_set&gt; </p> <p> I ues BOOST_FOREACH like this: std::vector&lt;std::string&gt; <a class="missing wiki">ClientSet</a>; BOOST_FOREACH(boost::property_tree::ptree::value_type &amp;v, pt.get_child("client_set")) </p> <blockquote> <p> <a class="missing wiki">ClientSet</a>.push_back(v.second.data()); </p> </blockquote> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Sun, 30 Dec 2012 23:26:07 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7828#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7828#comment:2</guid> <description> <p> This ticket will get more interest if you: </p> <ul><li>Post actual code </li><li>Set the component to Boost.Foreach </li><li>Set the "owned by" to the Boost.Foreach maintainer (eric_niebler); <a class="ext-link" href="https://svn.boost.org/trac/boost/report/15"><span class="icon">​</span>the list of maintainers</a> </li></ul> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Thu, 03 Jan 2013 16:21:25 GMT</pubDate> <title>component changed; owner set https://svn.boost.org/trac10/ticket/7828#comment:3 https://svn.boost.org/trac10/ticket/7828#comment:3 <ul> <li><strong>owner</strong> set to <span class="trac-author">Eric Niebler</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">foreach</span> </li> </ul> Ticket Eric Niebler Thu, 03 Jan 2013 23:46:47 GMT <link>https://svn.boost.org/trac10/ticket/7828#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7828#comment:4</guid> <description> <p> Please post minimal code that reproduces the error. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 04 Jan 2013 09:16:20 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7828#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7828#comment:5</guid> <description> <p> Sorry for reply late. The file name of xml document is "axml.xml".The content of the file like this: </p> <blockquote> <p> &lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;client_set&gt;&lt;client&gt;00801038&lt;/client&gt;&lt;client&gt;00802980&lt;/client&gt;&lt;client&gt;01600817&lt;/client&gt;&lt;/client_set&gt; </p> </blockquote> <p> My code is like this: </p> <p> std::vector&lt;std::string&gt; <a class="missing wiki">ClientSet</a>; ... boost::property_tree::ptree pt; try { boost::property_tree::read_xml("axml.xml",pt); ... BOOST_FOREACH(boost::property_tree::ptree::value_type &amp;v, pt.get_child("client_set")) </p> <blockquote> <p> <a class="missing wiki">ClientSet</a>.push_back(v.second.data()); </p> </blockquote> <p> ... } catch(std::exception &amp; e) { } </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Eric Niebler</dc:creator> <pubDate>Mon, 18 Feb 2013 00:30:40 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/7828#comment:6 https://svn.boost.org/trac10/ticket/7828#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">worksforme</span> </li> </ul> <p> I tested the following code against Boost trunk: </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;boost/property_tree/ptree.hpp&gt; #include &lt;boost/property_tree/xml_parser.hpp&gt; #include &lt;boost/foreach.hpp&gt; int main() { std::vector&lt;std::string&gt; ClientSet; boost::property_tree::ptree pt; try { boost::property_tree::read_xml("C:\\axml.xml",pt); BOOST_FOREACH( boost::property_tree::ptree::value_type &amp;v, pt.get_child("client_set")) { ClientSet.push_back(v.second.data()); } std::cout &lt;&lt; "client_set size = " &lt;&lt; ClientSet.size() &lt;&lt; std::endl; std::cout &lt;&lt; "client_set[0] = " &lt;&lt; ClientSet[0] &lt;&lt; std::endl; std::cout &lt;&lt; "client_set[1] = " &lt;&lt; ClientSet[1] &lt;&lt; std::endl; std::cout &lt;&lt; "client_set[2] = " &lt;&lt; ClientSet[2] &lt;&lt; std::endl; } catch(std::exception &amp; e) { } } </pre><p> I used a data file <code>C:\axml.xml</code> that contains this: </p> <pre class="wiki">&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;client_set&gt; &lt;client&gt;00801038&lt;/client&gt; &lt;client&gt;00802980&lt;/client&gt; &lt;client&gt;01600817&lt;/client&gt; &lt;/client_set&gt; </pre><p> I get this: </p> <pre class="wiki">client_set size = 3 client_set[0] = 00801038 client_set[1] = 00802980 client_set[2] = 01600817 </pre><p> Looks ok to me. </p> Ticket