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.
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.