Note that there are some explanatory texts on larger screens.

plurals
  1. POOperation over iterators in c++
    text
    copied!<p>I have some problem in translating some rows of code from ANSI C and arrays, to C++ with vectors.</p> <p>To operate iteratively along the element of the array, in ANSI C, I wrote:</p> <pre><code>int i; struct Sys{ double *v; }; Sys sys; sys.v = malloc(10*sizeof(double)); //initialize the array with some values... {...} for (i = 5; i &lt; 10; i++){ //overwrite the cumulative sum starting from position 4 sys.v[i] = sys.v[i] + function_that_return_a_double(i); } </code></pre> <p>Now, I wont to translate in C++ with vectors. Here is my trial.</p> <pre><code>Sys { vector&lt;double&gt; v; }; Sys sys; sys.v.resize(10); // initialize the vector with some values... {...} for (vector&lt;double&gt;::iterator it = sys.v.begin() + 5; it != sys.v.end(); ++it){ //yyy k = k+1; tmp = function_that_return_a_double(k); *it = *it + tmp; //xxx } </code></pre> <p>But I get the following error:</p> <pre><code>code.cpp:xxx: error: name lookup of ‘it’ changed for ISO ‘for’ scoping code.cpp:xxx: note: (if you use ‘-fpermissive’ G++ will accept your code) </code></pre> <p>If i complile with -fpermissive, I get:</p> <pre><code>code.cpp:xxx: warning: name lookup of ‘it’ changed for ISO ‘for’ scoping code.cpp:yyy: warning: using obsolete binding at ‘it’ </code></pre> <p>I don't undenstand if this is the right way to use the iterators and STD:vector</p> <p>I hope you can solve my doubts,</p> <p>Cheers,</p> <p>Al.</p> <p>PS: I corrected the declaration of v in the c++ case. v is not a pointer! PPS: The code snippet is fine!! see below. </p>
 

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