Sunday 15 August 2010

c++ - Changing a function to use vectors instead of arrays -


I'm getting some stranded functions to use vectors, I have the task given below which creates a new dynamic array Now I have to do this, but vectors should be used.

  int * makeData (integer shape) {int * ptr = nullptr; Ptr = new int [size]; Return PTR; }  

All I have got so far and can not figure out what to do next. Do I need an indicator to return?

  int * makeData (integer shape) {vector & lt; Int & gt; Data (100); }  

This function is fine, but there is a way to optimize it (make it simple and cleaner).

zero getMovieData2 (vector & lt; int> data, integer number) {int temp; Cout & lt; & Lt; "Enter the number of movies each student has seen. \ N"; For (integer number = 0; count & lt; num; count ++) {cout & lt; & Lt; "Student" & lt; & Lt; (Counting +1) & lt; & Lt; ":"; Cin & gt; & Gt; Temporary; Data.push_back (temp); While (AR [Calculation] MacData

/ P> < Pre> vector & lt; Int & gt; MacData (integer shape) {return vector & lt; Int & gt; (Shape); }

As you see it effectively

  vector & lt; Int & gt; Data (size);  

However, if you use it manually, it can be useful

  auto data = macadata (size);  

According to your getMovieData function: No, this function is not working as expected. You are passing the vector as value and in it "Return" is data - except that you are putting your data in a local copy which will be lost on returning the function.

The solution is to use the context, as

  zero getMovieData (vector & lt; int & gt; outdata, int count);  

However, I recommend that you return only a new vector

  vector & lt; Int & gt; GetMovieData (integer number);  

No comments:

Post a Comment