|
-
March 2nd, 2006, 05:49 AM
#4
Re: Traversing arrays problem
Code:
#include <algorithm> // for min_element/max_element
#include <iostream> // for cout
using namespace std;
int main()
{
cout <<"witness the awesome power of the LARGEST VALUE IN ARRAY FINDER !!!\n\n";
int array[] = {5,6,3,230,3,4,6,110};
unsigned const num_elements=sizeof(array)/sizeof(int);
int * plargest = max_element(array,array+num_elements);
cout <<"It's position in the array is "<< (plargest-array) <<"\n";
cout <<"Largest value in array: "<< *plargest;
cin.get();
cin.get();
return 0;
}
max_element yields a pointer/iterator to the largest element, not a pointer to a position of the largest element.
To get position, you simply need to compute the difference pointer_to_largest-array
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|