I am using a List where I wonder how it is possible to find the MaxValue searching trough
Value1[0] until Value1[3].
In this case it should return: 12
I have found a ::Max function but this will only consider the 2 values inside the paranthes wich then will return: 100.
For the std:: you can use std::max_element( , ) for this purpose to search through a range of elements.

List<double>^ Value1= gcnew List<double>();
Value1->Add(12);
Value1->Add(2);
Value1->Add(7);
Value1->Add(4);

double MaxValue1 = 0;

MaxValue1 = System::Math::Max( 0, 100 );

Recommended Answers

All 6 Replies

Well You can use the sort function to list and get the max value .

For that you will need to write a bool comp() function for your datatype.

Comparison function that, taking two values of the same type than those contained in the list object, returns true if the first argument goes before the second argument in the specific order (i.e., if the first is less than the second), and false otherwise.

After sorting. The item in your list will be the greatest i guess.

I understand. I have a wondering here. If you would use a for loop to iterate through a series of elements to replace a greater value to find the greatest value.
Will this be as fast as the sortingmethod or perheps this is difficult to say ?
Why I wonder is because I am not 100 % sure how to write that comparison before I begin.

Well I dint see it before. But seems that you have a list of doubles in the list.

So you can just use

Value1.sort();

And then print out the last item in the list.

Well You can use the sort function to list and get the max value .

For that you will need to write a bool comp() function for your datatype.

Comparison function that, taking two values of the same type than those contained in the list object, returns true if the first argument goes before the second argument in the specific order (i.e., if the first is less than the second), and false otherwise.

After sorting. The item in your list will be the greatest i guess.

Erm, what's wrong with a simple loop?

double max = Value1[0];

for each ( double value in Value1 ) {
  if ( value > max )
    max = value;
}

>Will this be as fast as the sortingmethod or perheps this is difficult to say ?
I'd be surprised if the sorting method was faster.

Erm, what's wrong with a simple loop?

double max = Value1[0];

for each ( double value in Value1 ) {
  if ( value > max )
    max = value;
}

>Will this be as fast as the sortingmethod or perheps this is difficult to say ?
I'd be surprised if the sorting method was faster.

Well I dint see that . The List was containing Double's I thought that it is some datatype . I stand corrected though. I guess i already have replied too.

I think I will go with the loop then, I found out it was very fast also.
I did iterate a ListValue 200000000 times and it only took about 1 second.

/j

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.