473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sort vector in ascending order, but stop prematurely

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<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

Jan 27 '07 #1
3 2155
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
Jan 27 '07 #2
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
Jan 27 '07 #3


On 27 Jan, 16:58, Kai-Uwe 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

Kai-Uwe Bux
- Eric

Jan 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ken R. | last post by:
Hello all, I am relatively new to python but I am having an issue with custom sort functions.. I am trying to sort a list of lists or tuples with arbitrary ascending or descending sorts. For...
2
by: Alpay Eno | last post by:
Hello all... I'm using asp to get records from an access database, very similar to the way datagrid would work. The title of each column in my table is a link that alternates the sort order between...
4
by: jeffsal | last post by:
I am using sorttable.js to sort a table which works fine which allows a user to sort the table by clicking on the column header. Is there some code I could add to the page (onload or something) to...
3
by: Petterson Mikael | last post by:
Hi, I have the following package names ( in an xml) that I will transform to html. I need to sort them. <package name="se.company.product.subproduct.boam.fpx.testsignals"> <package...
1
by: reiks | last post by:
I have a datatable . My requirement is to sort the table rows in ascending and descending order basing on the columns which I give it. I've used the following expression ...
2
by: Patrick | last post by:
I got a page that loads a xml file into a treeview control. I want it to sort the data before sending it to the treeview control is that possible? <root> <level1 name="" src="" order="0">...
11
by: Alan T | last post by:
Does the ListView supports sort? I want to have a up/down arrow/triangle that show it is sorted asc or desc on the column headers when I click the column header. May be I need a third-party...
15
by: bcochofel | last post by:
Hi, I want to use a variable to sort elements. That var his passed with query string (I'm using Perl CGI to generate XML). Here's a sample of my output:...
13
by: Vbbeginner07 | last post by:
its about Sorting a list view but its not working. please help....... Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader) 'Determine whether the column is the same...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.