473,385 Members | 1,468 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,385 software developers and data experts.

Sorting output

I want to sort dome output from
a routine which writes to an ostream.
Following is what I am doing now and it works, but it looks
overyly complicated to me.
I assume that there is an easier way (avoiding any char[] buffer).
Any suggestions?
Thanks,
Marc
stringstream strS;
for( SizeT i=0; i<nEnv; ++i)
{
help_item( &strS, i); // writes one line about some property #i
}
deque<DString> toSort;
for( SizeT i=0; i<nEnv; ++i)
{
char buf[ 256];
strS.getline( buf, 256);
toSort.push_back( buf);
}
sort( toSort.begin(), toSort.end());
for( SizeT i=0; i<nEnv; ++i)
{
cout << toSort[ i] << endl;
}

Mar 7 '06 #1
4 1553
In article <11**********************@u72g2000cwu.googlegroups .com>,
"m_*********@hotmail.com" <m_*********@hotmail.com> wrote:
I want to sort dome output from
a routine which writes to an ostream.
Following is what I am doing now and it works, but it looks
overyly complicated to me.
I assume that there is an easier way (avoiding any char[] buffer).
Any suggestions?
Thanks,
Marc
stringstream strS;
for( SizeT i=0; i<nEnv; ++i)
{
help_item( &strS, i); // writes one line about some property #i
}
deque<DString> toSort;
for( SizeT i=0; i<nEnv; ++i)
{
char buf[ 256];
strS.getline( buf, 256);
toSort.push_back( buf);
}
sort( toSort.begin(), toSort.end());
for( SizeT i=0; i<nEnv; ++i)
{
cout << toSort[ i] << endl;
}


Use a set instead of a deque. The data will be sorted as it is inserted.

set<string> helpstrings;
for ( int i = 0; i < nEnv; ++i ) {
stringstream ss;
help_item( &ss, i );
helpstrings.insert( ss.str() );
}
copy( helpstrings.begin(), helpstrings.end(),
ostream_iterator<string>( cout, "\n" ) );

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 7 '06 #2
On 2006-03-07, Daniel T. <po********@earthlink.net> wrote:
In article <11**********************@u72g2000cwu.googlegroups .com>,
"m_*********@hotmail.com" <m_*********@hotmail.com> wrote:
I want to sort dome output from
a routine which writes to an ostream.
Following is what I am doing now and it works, but it looks
overyly complicated to me.
I assume that there is an easier way (avoiding any char[] buffer).
Any suggestions?
Thanks,
Marc


Use a set instead of a deque. The data will be sorted as it is inserted.

set<string> helpstrings;
for ( int i = 0; i < nEnv; ++i ) {
stringstream ss;
help_item( &ss, i );
helpstrings.insert( ss.str() );
}
copy( helpstrings.begin(), helpstrings.end(),
ostream_iterator<string>( cout, "\n" ) );


multiset is possibly more appropriate, in case there are duplicate
strings, which should be retained.

--
Neil Cerutti
Mar 7 '06 #3
m_*********@hotmail.com wrote:
stringstream strS;
for( SizeT i=0; i<nEnv; ++i)
{
help_item( &strS, i); // writes one line about some property #i
Why isn't the stream passed by reference as is conventional for
typical output functions?
deque<DString> toSort;
for( SizeT i=0; i<nEnv; ++i)
{
char buf[ 256];
strS.getline( buf, 256);


The above two lines should be replaced by

std::string buf;
std::getline(strS, buf);

This will avoid problems with long lines. Other than this, the
code looks quite reasonable to me.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 7 '06 #4
Thank you all for the really helpful suggestions!
Marc

Mar 8 '06 #5

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

Similar topics

9
by: p0wer | last post by:
Let's suppose I have this sample document: <root> <entry id="1" <date>2003-08-03</date> <param_1>5</param_1> <param_2>10</param_2> </entry> <entry id="2"> ...
3
by: Jef Driesen | last post by:
The number of items in my std::list is changed after sorting (using std::list member function sort). I'm using MSVC6 and the actual (pseudo) code is posted below. The output from size() is...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
0
by: sweta kandula | last post by:
hello this is sweta and under grad from india i have some doubt regarding my programming i shall explain you the project in detaillll my project is concerned with DFS and Topological sorting...
4
by: Ron Adam | last post by:
This is for a new version of pydoc if I can get the class attributes sorted out. The module level attributes aren't too difficult to categorize. (I might be just too tired to see the obvious.) ...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
2
by: Bob Laubla | last post by:
Hello I have a very complex maketable query with many records and involving multiple VB functions which call other functions. I need this table to be sorted by the first field. But no matter what...
1
by: LittlBUGer | last post by:
Hello. First of all I'm programming in VB.NET/ASP.NET doing a page for a website. Now, to my question.... I have a simple array of integer numbers (15 characters in length) which can hold up to...
18
by: =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= | last post by:
Hello. It seems a year is all it takes for one's proficiency in C++ to become too rusty. Professionally, I've been away from the language (and from programming in general), but I still preserve...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.