2 Iterators/Iterator Math

Discussion in 'C++' started by Mike Copeland, Nov 26, 2012.

  1. 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
    Mike Copeland, Nov 26, 2012
    #1
    1. Advertising

  2. Mike Copeland

    James Kanze Guest

    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.
    James Kanze, Nov 27, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chirs
    Replies:
    18
    Views:
    736
    Chris Uppal
    Mar 2, 2004
  2. AciD_X
    Replies:
    4
    Views:
    8,059
    Jonathan Turkanis
    Apr 1, 2004
  3. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    473
    Kai-Uwe Bux
    May 8, 2005
  4. , India
    Replies:
    10
    Views:
    1,053
    James Kanze
    Aug 8, 2009
  5. VK
    Replies:
    15
    Views:
    1,093
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page