Vectors

double offensive_rating = points_per_48 + (offensive_rebounds_per_48 * .4) + (assists_per_48 * .4) - (turnovers_per_48 * .4);
offensive.push_back(offensive_rating);

I have all of the variables coded above, but I am not sure how to pick out the vectors and print out all of the elements of the vectors to find the largest. What is the best way to do so?
but I am not sure how to pick out the vectors

Which vectors?

and print out all of the elements of the vectors to find the largest.

Do you want to print them or find the largest element?
The latter is done using max_element:
http://www.cplusplus.com/reference/algorithm/max_element/
void findTopOffensivePlayer(vector<double> offensive)

I have the vector in this function and using <push_back> to place the values in the vector.

I tried using the max_element in the program but i get a no matching funtion error:
main.cpp:188: error: no matching function for call to 'max_element(std::vector<double, std::allocator<double> >&)'
It's max_element(offensive.begin(),offensive.end()) and it returns an iterator.
Last edited on
I tried that too but still come out with a crazy function error. Thanks for the help anyways though.
Code, error message.
What is the crazy function error you're getting?

A few notes:
1. You can't directly copy Athar's code snippet. You will have to change a few tiny things (like the name of the vector being used.) :)
2. You need to #include <algorithm> to use the function.
3. If you continue to have problems, you could do the search yourself.
3.1. Have a temporary double initialized to the first element of your vector.
3.2. Go over each element of the vector.
3.2.1. If the element is smaller than the value recorded in your temporary double, ignore it.
3.2.2. If the element is larger than the value recorded in your temporary double, reassign the temporary double to that element.
3.3. When your loop is over, if all went well, the temporary double will contain your largest element. :)

-Albatross
@arthar its 300 lines of code and all im trying to do is create a function with a vector reference, such as:

funtion_name(vector<double> offensive)** if thats even right**

then later on go into the vector, print it out and find the highest number in the vector.
It's not necessary to post the whole code, just the relevant parts. And always the full error message. There's usually no point in posting without providing both.
And sorry, the third parameter was nonsense.

For future reference:
http://www.cplusplus.com/reference/
http://www.cplusplus.com/reference/algorithm/
It's best to familiarize yourself with the standard library so you know what's at your disposal.
Just try this its similar like Albatross mentioned here is the loop:
1
2
3
4
5
6
7
8
9
10
11
12
vector<double>::iterator it;
double number;
it=yourvector.begin();
number=*it;
it++;
for(;it!=yourvector.end();it++)
{
      if(*it>number)
     {
           number=*it;
     }
}


I'm sure this above will solve your issue!
Note! the variable number will have your max element so all you need to do is print number!
Last edited on
Topic archived. No new replies allowed.