f



iterator and reverse_iterator

Hi

I have a vector of pairs. I want to perform an operation on each of
the pair's first values. As soon as an operation fails I want to
perform the same operation on the corresponding second value as well
as all second value's corresponding to all the previously operated on
first values. A bit like a horrible manual rollback.

My problem: I cannot seem to implicitly convert an iterator into a
reverse_iterator. So the following code does not work. How to fix this
please! Please don't be annoyed - I'm using Microsoft's compiler
V13.00.9466.

Emma Middlebrook
emma_middlebrook@fastmail.fm

const operations_and_their_inverses_t::iterator begin =
operations_with_rollbacks.begin();
const operations_and_their_inverses_t::iterator end =
operations_with_rollbacks.end();

operations_and_their_inverses_t::iterator it = begin;

bool rollback_necessary = false;

for (; it != end; it++)
{
   if (!process_operation(it->first))
   {
       rollback_necessary = true;
       break;
   }
}

const operations_and_their_inverses_t::reverse_iterator rend =
operations_with_rollbacks.rend();

operations_and_their_inverses_t::reverse_iterator rolling_back = it;

for (; rolling_back != rend ; rolling_back++)
{
    process_operation(rolling_back->second);
}

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
emma_middlebrook
6/29/2003 10:19:40 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

4 Replies
472 Views

Similar Articles

[PageSpeed] 41

emma middlebrook <emma_middlebrook@fastmail.fm> wrote:

 > Hi
 >
 > I have a vector of pairs. I want to perform an operation on each of
 > the pair's first values. As soon as an operation fails I want to
 > perform the same operation on the corresponding second value as well
 > as all second value's corresponding to all the previously operated on
 > first values. A bit like a horrible manual rollback.
 >
 > My problem: I cannot seem to implicitly convert an iterator into a
 > reverse_iterator. So the following code does not work. How to fix this
 > please! Please don't be annoyed - I'm using Microsoft's compiler
 > V13.00.9466.
 > [snip]
    you seem to want to do this:
// BI is a bidirectional iterator...
template <class BI>
void forward_and_rollback(BI begin,BI end)
{

         if(begin==end)
                 return;
         BI it;
         struct bad_news{};
         try
         {
                 for(it=begin;it!=end;++it)
                         if(!process_operation(it->first))
                                 throw bad_news();
         }
         catch(bad_news)
         {
                 for(;;--it)
                 {
                         process_operation(it->second);
                         if(it == begin)
                                 break;
                 }
         }
}

bool process_operation(your_type &);
std::vector<std::pair<your_type,your_type> > items;
//...
forward_and_rollback(items.begin(),items.end());

This looks like the logic you want. Is it?

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
cbarron3
6/30/2003 9:40:12 AM
"emma middlebrook" <emma_middlebrook@fastmail.fm> wrote in message

 > operations_and_their_inverses_t::iterator it = begin;
 >
 > bool rollback_necessary = false;
 >
 > for (; it != end; it++)
 > {
 >    if (!process_operation(it->first))
 >    {
 >        rollback_necessary = true;
 >        break;
 >    }
 > }

Should there be an if on rollback_necessary here?  Actually if it!=end then
it means rollback is necessary, so you can get rid off the variable
rollback_necessary.


 > const operations_and_their_inverses_t::reverse_iterator rend =
 > operations_with_rollbacks.rend();
 >
 > operations_and_their_inverses_t::reverse_iterator rolling_back = it;

operations_and_their_inverses_t::reverse_iterator rolling_back(++it);

I think the increment is necessary because the reverse_iterator actually
refers to the element right before it. Anyway try it and let us know.


To make the code look safe for a code review try this

    if (it != end) {
       ++it;
       operations_and_their_inverses_t::reverse_iterator rolling_back(it);
       for (; rolling_back != rend ; rolling_back++)
       {
          process_operation(rolling_back->second);
       }
    }


--
+++++++++++
Siemel Naran


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Siemel
6/30/2003 9:41:59 AM
Good Morning, Emma!

emma middlebrook schrieb:

 > Hi
 >
 > I have a vector of pairs. I want to perform an operation on each of
 > the pair's first values. As soon as an operation fails I want to
 > perform the same operation on the corresponding second value as well
 > as all second value's corresponding to all the previously operated on
 > first values. A bit like a horrible manual rollback.
 >
 > My problem: I cannot seem to implicitly convert an iterator into a
 > reverse_iterator. So the following code does not work. How to fix this
 > please! Please don't be annoyed - I'm using Microsoft's compiler
 > V13.00.9466.
 >

According to the current C++ standard, the fix is simple (In the moment I
can't
check, whether the fix is ok for your very compiler). The reverse_iterator

class template has an *explicit* constructor, accepting an iterator, so
you only
need to substitute your line

operations_and_their_inverses_t::reverse_iterator rolling_back = it;

by

operations_and_their_inverses_t::reverse_iterator rolling_back(it);

or (less recommended) by

operations_and_their_inverses_t::reverse_iterator rolling_back =
operations_and_their_inverses_t::reverse_iterator(it);

Greetings from Bremen,

Daniel



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Daniel
6/30/2003 9:44:38 AM
> I cannot seem to implicitly convert an iterator into a
> reverse_iterator.

there is function called base() to be called like this:

vector<int>::reverse_iterator ritr = .....(pointing somewhere)
vector<int>::iterator itr = ritr.base()  //this casts reverse_iterator to
iterator so use this

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Chandra
6/30/2003 6:39:17 PM
Reply: