Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to start from the point where you've already read in the line from the file, since it seems like you're OK with that. This is the most basic approach. My code below illustrates this approach using very straightforward code that could be used to understand how this works.</p> <ol> <li>Get the line you want to parse in to a <code>string</code>, this is <code>line</code> in my code below.</li> <li>Search the <code>string</code> for tokens, where each token is seperated by anything that isn't either a digit, a decimal, or a negative sign</li> <li>For each token, convert the string to a <code>double</code> by using <code>stringstream</code>. </li> <li>Add the converted double to your vector</li> </ol> <p>At the end of the loop, I also dump the vector to the screen for inspection.</p> <pre><code>#include &lt;string&gt; #include &lt;vector&gt; #include &lt;sstream&gt; #include &lt;iostream&gt; #include &lt;iterator&gt; using namespace std; int main() { string line = "3.0 2.5 -1.5 0.0 1.7 4.0"; vector&lt;double&gt; doubles; string::size_type n_begin = 0; string::size_type n_end = line.find_first_not_of("-.0123456789", n_begin); while( n_begin != string::npos ) { string cur_token = line.substr(n_begin, n_end-n_begin); stringstream ss; ss &lt;&lt; cur_token; double cur_val = 0.0; ss &gt;&gt; cur_val; doubles.push_back(cur_val); // set up the next loop if( n_end == string::npos ) n_begin = n_end = string::npos; else { n_begin = n_end + 1; n_end = line.find_first_not_of("-.0123456789", n_begin); } } copy(doubles.begin(), doubles.end(), ostream_iterator&lt;double&gt;(cout, "\n")); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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