Sorting output

M

m_schellens

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;
}
 
D

Daniel T.

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" ) );
 
N

Neil Cerutti

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

Dietmar Kuehl

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top