P: n/a

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 iteratorbased.
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<intv;
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  
Share this Question
P: n/a

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
KaiUwe Bux  
P: n/a

In article <11*********************@v45g2000cwv.googlegroups. com>,
"Eric Lilja" <mi********@gmail.comwrote:
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  
P: n/a

On 27 Jan, 16:58, KaiUwe Bux <jkherci...@gmx.netwrote:
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
KaiUwe Bux
 Eric   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1717
 replies: 3
 date asked: Jan 27 '07
