Note that there are some explanatory texts on larger screens.

plurals
  1. POiterate through an array of vector in c++
    text
    copied!<p>I am trying to iterate through my vector array, but I cannot seem to get it to work, my error is at the second for loop and the error message is the follow:</p> <blockquote> <p>expected primary expression before 'double'</p> </blockquote> <p>I have looked through on how to iterate a regular vector, but how do I iterator a vector array? I followed this structure:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; int main () { std::vector&lt;int&gt; myvector; for (int i=1; i&lt;=5; i++) myvector.push_back(i); std::cout &lt;&lt; "myvector contains:"; for (std::vector&lt;int&gt;::iterator it = myvector.begin() ; it != myvector.end(); ++it) std::cout &lt;&lt; ' ' &lt;&lt; *it; std::cout &lt;&lt; '\n'; return 0; } </code></pre> <p>I can't seem to get my vector array version to work. </p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;vector&gt; using namespace std; int main(int arc, char *argv[]) { vector&lt;double&gt; vector[7]; double num[7]; ifstream infile("data.txt"); string temp; for(int i = 0; i &lt;= 6; i++) { infile &gt;&gt; temp; cout &lt;&lt; temp &lt;&lt; ' '; } cout &lt;&lt; endl; while(infile &gt;&gt; num[0] &gt;&gt; num[1] &gt;&gt; num[2] &gt;&gt; num[3] &gt;&gt; num[4] &gt;&gt; num[5] &gt;&gt; num[6]) { for(int i = 0; i &lt;= 6; i++) { vector[i].push_back(num[i]); } } cout &lt;&lt; endl; for(int i = 0; i &lt;= 6; i++) { // error on this line // not sure what is wrong before vector&lt;double&gt;:: iterator it = vector[i].begin() for(vector&lt;double&gt;::iterator it = vector[i].begin(); it != vector[i].end(); ++it) { cout &lt;&lt; ' ' &lt;&lt; *it; } } return 0; } </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