Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Sort vector in ascending order, but stop prematurely

Reply
Thread Tools

Sort vector in ascending order, but stop prematurely

 
 
Eric Lilja
Guest
Posts: n/a
 
      01-27-2007
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

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-27-2007
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:artial_sort().


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      01-27-2007
In article <. com>,
"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:artial_sort.

-Howard
 
Reply With Quote
 
Eric Lilja
Guest
Posts: n/a
 
      01-27-2007


On 27 Jan, 16:58, Kai-Uwe Bux <jkherci...@gmx.net> 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:artial_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

 
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
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how to sort the data present in file in ascending or descending order sudhir C Programming 8 12-06-2005 03:06 PM
sort ascending not working geopelia Computer Support 15 10-18-2004 10:28 AM
Sort Column In Table (ascending) Using attributes AwanJohnie XML 1 10-02-2004 04:36 PM
Ascending & Descending sort in datagrid Steve Chatham ASP .Net 2 02-19-2004 09:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57