Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > After sorting a vector, how to get max in this vector?

Reply
Thread Tools

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

 
 
cylin
Guest
Posts: n/a
 
      12-19-2003
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[i] << '\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.


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-19-2003
cylin wrote:
> 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[i] << '\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.
>
>


 
Reply With Quote
 
 
 
 
cylin
Guest
Posts: n/a
 
      12-19-2003
Thanks............
I forgot the index is out of range.

Regards,
cylin.
"Gianni Mariani" <(E-Mail Removed)>
???????:brudno$(E-Mail Removed)...
cylin wrote:
> 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[i] << '\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.
>
>




 
Reply With Quote
 
Cy Edmunds
Guest
Posts: n/a
 
      12-19-2003
"cylin" <(E-Mail Removed)> wrote in message
news:brugig$7oc7b$(E-Mail Removed)-berlin.de...
> Thanks............
> I forgot the index is out of range.
>
> Regards,
> cylin.
> "Gianni Mariani" <(E-Mail Removed)>
> ???????:brudno$(E-Mail Removed)...
> cylin wrote:
> > 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[i] << '\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.

--
Cy
http://home.rochester.rr.com/cyhome/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
want max width and max height as long as not exceed 100px Summercool HTML 9 10-23-2007 02:27 AM
ADSL Max/Max Gordy NZ Computing 6 11-24-2006 11:41 PM
Max Apperture and Max. Shutter Speed Confusion-HELP bhaskar Digital Photography 12 07-22-2003 05:17 PM



Advertisments