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

Discussion in 'C++' started by cylin, Dec 19, 2003.

  1. cylin

    cylin Guest

    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.
     
    cylin, Dec 19, 2003
    #1
    1. Advertising

  2. 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 << '\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.
    >
    >
     
    Gianni Mariani, Dec 19, 2003
    #2
    1. Advertising

  3. cylin

    cylin Guest

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

    Regards,
    cylin.
    "Gianni Mariani" <>
    ???????:brudno$...
    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 << '\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.
    >
    >
     
    cylin, Dec 19, 2003
    #3
  4. cylin

    Cy Edmunds Guest

    "cylin" <> wrote in message
    news:brugig$7oc7b$-berlin.de...
    > Thanks............
    > I forgot the index is out of range.
    >
    > Regards,
    > cylin.
    > "Gianni Mariani" <>
    > ???????:brudno$...
    > 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 << '\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/
     
    Cy Edmunds, Dec 19, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    2,005
    Csaba
    Feb 18, 2006
  2. Summercool
    Replies:
    9
    Views:
    927
    dorayme
    Oct 23, 2007
  3. Replies:
    2
    Views:
    1,491
    James Kanze
    Jul 6, 2010
  4. Greg Ferris

    Textarea max rows and max characters per row

    Greg Ferris, Jan 16, 2004, in forum: Javascript
    Replies:
    2
    Views:
    630
    Greg Ferris
    Jan 16, 2004
  5. kriton
    Replies:
    0
    Views:
    370
    kriton
    Apr 22, 2005
Loading...

Share This Page