Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you don't mind using the Boost libraries...</p> <pre><code>#include &lt;string&gt; #include &lt;vector&gt; #include &lt;boost/lexical_cast.hpp&gt; #include &lt;boost/regex.hpp&gt; std::vector&lt;int&gt; ParseFile(std::istream&amp; in) { const boost::regex cItemPattern(" *([0-9]+),?"); std::vector&lt;int&gt; return_value; std::string line; while (std::getline(in, line)) { string::const_iterator b=line.begin(), e=line.end(); boost::smatch match; while (b!=e &amp;&amp; boost::regex_search(b, e, match, cItemPattern)) { return_value.push_back(boost::lexical_cast&lt;int&gt;(match[1].str())); b=match[0].second; }; }; return return_value; } </code></pre> <p>That pulls the lines from the stream, then uses the Boost::RegEx library (with a capture group) to extract each number from the lines. It automatically ignores anything that isn't a valid number, though that can be changed if you wish.</p> <p>It's still about twenty lines with the <code>#include</code>s, but you can use it to extract essentially <em>anything</em> from the file's lines. This is a trivial example, I'm using pretty much identical code to extract tags and optional values from a database field, the only major difference is the regular expression.</p> <p>EDIT: Oops, you wanted three separate vectors. Try this slight modification instead:</p> <pre><code>const boost::regex cItemPattern(" *([0-9]+), *([0-9]+), *([0-9]+)"); std::vector&lt;int&gt; vector1, vector2, vector3; std::string line; while (std::getline(in, line)) { string::const_iterator b=line.begin(), e=line.end(); boost::smatch match; while (b!=e &amp;&amp; boost::regex_search(b, e, match, cItemPattern)) { vector1.push_back(boost::lexical_cast&lt;int&gt;(match[1].str())); vector2.push_back(boost::lexical_cast&lt;int&gt;(match[2].str())); vector3.push_back(boost::lexical_cast&lt;int&gt;(match[3].str())); b=match[0].second; }; }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload