Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 2 Iterators/Iterator Math

Reply
Thread Tools

2 Iterators/Iterator Math

 
 
Mike Copeland
Guest
Posts: n/a
 
      11-26-2012
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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-27-2012
On Monday, November 26, 2012 3:43:03 AM UTC, Mike Copeland wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
Math.min and Math.max for byte / short Philipp Java 9 07-23-2008 12:37 AM
math.h trig functions questions (and some forgotten high school math) Mark Healey C Programming 7 05-22-2006 10:42 AM
Re: Is still math.h the C++ math library ? AciD_X C++ 4 04-01-2004 07:29 PM
Why can I not use: Math a=new Math(); chirs Java 18 03-02-2004 06:00 PM



Advertisments