After sorting a vector, how to get max in this vector?

C

cylin

Dear all,

After sorting a vector, how to get max in this vector?
I got a strange result of my test code.
-------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<int> vec;
vec.push_back(5);
vec.push_back(-5);
vec.push_back(10);
sort(vec.begin(),vec.end());
for (int i=0;i<vec.size();++i) {
cout << vec << '\t';
}
cout << endl;
cout << "Max=" << *vec.end() << endl;
cout << "Min=" << vec[0] << endl;
cout << "Max=" << vec[vec.size()] << endl;
cout << endl;
return 0;
}
-------------------------------------------
The max should be 10.
But this program's result is -842150451.
What's wrong?
Thanks for your help.

Regards,
cylin.
 
G

Gianni Mariani

cylin said:
Dear all,

After sorting a vector, how to get max in this vector?
I got a strange result of my test code.
-------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<int> vec;
vec.push_back(5);
vec.push_back(-5);
vec.push_back(10);
sort(vec.begin(),vec.end());
for (int i=0;i<vec.size();++i) {
cout << vec << '\t';
}
cout << endl;
cout << "Max=" << *vec.end() << endl;

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

vec.end() points to one beyond the end of the array.

*( vec.end() -1 )

cout << "Min=" << vec[0] << endl;
cout << "Max=" << vec[vec.size()] << endl;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vec[vec.size()] is undefined.

vec[vec.size()-1] is probably what you want.
 
C

cylin

Thanks............
I forgot the index is out of range.

Regards,
cylin.
"Gianni Mariani" <[email protected]>
???????:[email protected]...
cylin said:
Dear all,

After sorting a vector, how to get max in this vector?
I got a strange result of my test code.
-------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<int> vec;
vec.push_back(5);
vec.push_back(-5);
vec.push_back(10);
sort(vec.begin(),vec.end());
for (int i=0;i<vec.size();++i) {
cout << vec << '\t';
}
cout << endl;
cout << "Max=" << *vec.end() << endl;

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

vec.end() points to one beyond the end of the array.

*( vec.end() -1 )

cout << "Min=" << vec[0] << endl;
cout << "Max=" << vec[vec.size()] << endl;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vec[vec.size()] is undefined.

vec[vec.size()-1] is probably what you want.
 
C

Cy Edmunds

cylin said:
Thanks............
I forgot the index is out of range.

Regards,
cylin.
"Gianni Mariani" <[email protected]>
???????:[email protected]...
cylin said:
Dear all,

After sorting a vector, how to get max in this vector?
I got a strange result of my test code.
-------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<int> vec;
vec.push_back(5);
vec.push_back(-5);
vec.push_back(10);
sort(vec.begin(),vec.end());
for (int i=0;i<vec.size();++i) {
cout << vec << '\t';
}
cout << endl;
cout << "Max=" << *vec.end() << endl;

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

vec.end() points to one beyond the end of the array.

*( vec.end() -1 )

cout << "Min=" << vec[0] << endl;
cout << "Max=" << vec[vec.size()] << endl;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vec[vec.size()] is undefined.

vec[vec.size()-1] is probably what you want.



cout << endl;
return 0;
}
-------------------------------------------
The max should be 10.
But this program's result is -842150451.
What's wrong?
Thanks for your help.

Regards,
cylin.



Even easier, I think:

vec.front() for the first element (a reference, not an iterator) and
vec.back() for the last one (again a reference). Hence:

cout << "Min=" << vec.front() << endl;
cout << "Max=" << vec.back() << endl;

Easier to read and less prone to errors IMHO.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top