Sort vector in ascending order, but stop prematurely

Discussion in 'C++' started by Eric Lilja, Jan 27, 2007.

  1. Eric Lilja

    Eric Lilja Guest

    Hello, consider the following assignment and my code for it:

    /*
    Write a program that does the following:
    * Integer numbers shall be read from a textfile and stored in a
    std::vector.
    The name of the input file is to be given on the command line.
    * All negative values in the vector shall then be replaced with
    their
    positive counterpart, using the standard algorithm for_each.
    * The five smallest values in the vector shall then be found by
    sorting the
    vector, but not more than necessary.
    * The content of the vector is to be printed onto standard
    output.
    For full credit, all vector computations, including input and
    output, shall
    be iterator-based.
    Appropriate checks regarding the command line and file handling
    shall be
    done.
    */

    #include <algorithm>
    #include <cmath>
    #include <fstream>
    #include <iostream>
    #include <iterator>
    #include <vector>

    using namespace std;

    template<typename T>
    istream& operator>>(istream& is, vector<T>& v)
    {
    copy(istream_iterator<int>(is), istream_iterator<int>(),
    back_inserter(v));

    return is;
    }

    template<typename T>
    ostream& operator<<(ostream& os, const vector<T>& v)
    {
    copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));

    return os;
    }

    void replace_negative(int& n)
    {
    n = abs(n);
    }

    int
    main(int argc, char *argv[])
    {
    if (argc != 2)
    {
    cerr << "Wrong number of arguments." << endl;

    return 1;
    }

    ifstream infile(argv[1]);

    if (!infile)
    {
    cerr << "Failed to open file " << argv[1] << endl;

    return 2;
    }

    vector<int> v;

    infile >> v;

    infile.close();

    cout << "Vector contents before replacing negative values "
    << "with their positive counterpart: " << v << endl;

    for_each(v.begin(), v.end(), replace_negative);

    cout << "Vector contents after replacing negative values "
    << "with their positive counterpart: " << v << endl;

    sort(v.begin(), v.end()); // Problem, this sorts the entire
    vector...

    cout << "Vector contents after sorting enough to place the five "
    << "smallest numbers first: " << v << endl;
    }

    My problem is this particular part of the assignment:
    * The five smallest values in the vector shall then be found by
    sorting the
    vector, but not more than necessary.

    The way I'm using sort it will sort the entire vector, but it sounds
    like the author of this assignment wants me to stop sorting after the
    five smallest values has been placed first in the vector, in ascending
    order. How do I do that?? Or am I misunderstanding what he means? I
    think I have been able to solve the other parts of the assignment in a
    proper way. I'm overloading operator>>/operator<< to enhance
    readability (well, I think it does), I know it's not callec for in the
    assignment.

    - Eric
    Eric Lilja, Jan 27, 2007
    #1
    1. Advertising

  2. Eric Lilja

    Kai-Uwe Bux Guest

    Eric Lilja wrote:
    >
    > My problem is this particular part of the assignment:
    > * The five smallest values in the vector shall then be found by
    > sorting the
    > vector, but not more than necessary.
    >
    > The way I'm using sort it will sort the entire vector, but it sounds
    > like the author of this assignment wants me to stop sorting after the
    > five smallest values has been placed first in the vector, in ascending
    > order. How do I do that??


    I think, you might be interested in std::partial_sort().


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jan 27, 2007
    #2
    1. Advertising

  3. In article <>,
    "Eric Lilja" <> wrote:

    > The way I'm using sort it will sort the entire vector, but it sounds
    > like the author of this assignment wants me to stop sorting after the
    > five smallest values has been placed first in the vector, in ascending
    > order. How do I do that?? Or am I misunderstanding what he means? I
    > think I have been able to solve the other parts of the assignment in a
    > proper way. I'm overloading operator>>/operator<< to enhance
    > readability (well, I think it does), I know it's not callec for in the
    > assignment.


    Check out std::partial_sort.

    -Howard
    Howard Hinnant, Jan 27, 2007
    #3
  4. Eric Lilja

    Eric Lilja Guest

    On 27 Jan, 16:58, Kai-Uwe Bux <> wrote:
    > Eric Lilja wrote:
    >
    > > My problem is this particular part of the assignment:
    > > * The five smallest values in the vector shall then be found by
    > > sorting the
    > > vector, but not more than necessary.

    >
    > > The way I'm using sort it will sort the entire vector, but it sounds
    > > like the author of this assignment wants me to stop sorting after the
    > > five smallest values has been placed first in the vector, in ascending
    > > order. How do I do that??

    >
    >I think, you might be interested in std::partial_sort().


    Thanks! Seems to work just fine, but I guess I have to special case
    the code when the vector contains less than five elements.

    >
    > Best
    >
    > Kai-Uwe Bux


    - Eric
    Eric Lilja, Jan 27, 2007
    #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. =?Utf-8?B?Sm9l?=
    Replies:
    2
    Views:
    608
    =?Utf-8?B?RGVhc3Vu?=
    Nov 19, 2004
  2. Replies:
    0
    Views:
    293
  3. Replies:
    2
    Views:
    287
  4. PRadyut
    Replies:
    2
    Views:
    477
    Barry Schwarz
    Jun 10, 2005
  5. sudhir
    Replies:
    8
    Views:
    819
    Kenny McCormack
    Dec 6, 2005
Loading...

Share This Page