2 Iterators/Iterator Math

M

Mike Copeland

I am trying to print/display a list of strings in 2 columns. My
attempt to use 2 iterators (one for the first half of the list and
another for the 2nd half) doesn't compile - and I can't establish the
starting point of the 2nd iterator.
Note that I'm not dealing with an odd set of data, nor am I handling
the end of each subset of the list's data (not sure how best to do that,
either).
Perhaps this approach is entirely wrong, but it's the best I can come
up with. Any thoughts? TIA


size_t ppp = 0;
string str1, str2;
list<string> sList;
list<string>::iterator it1;
list<string>::iterator it2;
// populate list with some data...
ppp=sList.size();
sList.sort(), ii = 3;
it2 = sList.begin()+(ppp/2); // compiler won't allow!
for(it1 = sList.begin(); it1 != sList.end(); it1++)
{
str1 = *it1; // left side
str2 = *it2; // right side
// format and print a line
} // for
 
J

James Kanze

I am trying to print/display a list of strings in 2 columns. My
attempt to use 2 iterators (one for the first half of the list and
another for the 2nd half) doesn't compile - and I can't establish the
starting point of the 2nd iterator.
Note that I'm not dealing with an odd set of data, nor am I handling
the end of each subset of the list's data (not sure how best to do that,
either).
Perhaps this approach is entirely wrong, but it's the best I can come
up with. Any thoughts? TIA
size_t ppp = 0;
string str1, str2;
list<string> sList;
list<string>::iterator it1;
list<string>::iterator it2;
// populate list with some data...
ppp=sList.size();
sList.sort(), ii = 3;

Just curious, but what's this line? I can understand the sort,
but why the ii = 3 (which should be a separate statement if it
has any purpose).
it2 = sList.begin()+(ppp/2); // compiler won't allow!
for(it1 = sList.begin(); it1 != sList.end(); it1++)
{
str1 = *it1; // left side
str2 = *it2; // right side
// format and print a line
} // for

It's a valid approach, but you probably should be using
std::vector<std::string>, rather than std::list<>. std::list
does not allow random access; you can only move one forward or
backward from a known position.

And of course, you're loop is completely wrong, even if you got
the iterator. As it is, you'll get all of the elements in the
first column, and the middle element will repeat itself in the
second column of every line.

Note that getting the border conditions right isn't necessarily
obvious. If you have an odd number of elements, you'll probably
want to put the extra element in the first column. The easiest
way to handle this is to add an extra empty string to the list
if the number of elements is odd. Providing this doesn't screw
up your formatting. Something like:

sList.sort();
if ( sList.size() % 2 != 0 ) {
sList.push_back( "" );
}
std::vector<std::string>::const_iterator pivot
= sList.begin() + sList.size() / 2;
for ( std::vector<std::string>::const_iterator current1 = sList.begin(),
current2 = pivot;
current1 != pivot; // or current2 != sList.end();
++ current1, ++ current2 ) {
// format and output *current1, *current2
}

If the empty entry will cause problems when formatting, you'll
have to add some extra logic to handle it.
 

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,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top