iterators question??

I am trying to do an example , I am trying to swap the number but I do not think I am taking the right aproach.

Here is the code, please see if something is not right:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>

using namespace std;

int main()
   {
   int j;

   vector<int> theVector;
   vector<int>::iterator iter1;
   vector<int>::reverse_iterator iter2;

   for(j=2; j<16; j+=2)           //fill vector with 2, 4, 6, ...
      theVector.push_back(j);

   cout << "Before reversal: ";   //display vector
   for(iter1=theVector.begin(); iter1 != theVector.end(); iter1++)
      cout << *iter1 << " ";

   iter1 = theVector.begin();     
   iter2 = theVector.rbegin();    
   --iter2;                       

   while(*iter1 != *iter2)
      {
      swap(*iter1, *iter2);       //swap front and back
      ++iter1;                    //increment front
      if(*iter1==*iter2)          //if even number of elements
         break;
      --iter2;                    //decrement back
      }

   cout << "\nAfter reversal: ";  //display vector
   for(iter1=theVector.begin(); iter1 != theVector.end(); iter1++)
      cout << *iter1 << " ";
   cout << endl;
   return 0;
   }
Last edited on
are you trying to swap their memory addresses or the actual bytes?
the bytes(values)

Foward I want to print: 2 4 6 8 10 12 14

Reverse I want to print: 14 12 19 8 6 4 2
Last edited on
Someone?
You don't need to subtract 1 from rbegin(). It starts pointing at an element. Also, if the vector has an even number of elements or contains the same data in two elements, your loop will either not terminate when it should or possibly terminate early.
Still nowhere,
Why can't I incremment and decrement the iterators?

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// ex15_3.cpp
// home-made reverse() algorithm reverses a list
#include <iostream>
#include <vector>

using namespace std;

int main()
   {
   int j;

   vector<int> theVector;
   vector<int>::iterator iter1;
   vector<int>::reverse_iterator iter2;

   for(j=2; j<16; j+=2)           //fill list with 2, 4, 6, ...
      theVector.push_back(j);

   cout << "Before reversal: ";   //display list
   for(iter1=theVector.begin(); iter1 != theVector.end(); iter1++)
      cout << *iter1 << " ";

   iter1 = theVector.begin();       //set to first element
   iter2 = theVector.rbegin();         //set to one-past-last

   int length = theVector.size();
  for(int i = 0; i<= length/2; i++)
  {
      swap(*iter1, *iter2);  //swap front and back
      --iter1;             // why cant I do this? 
      ++iter2;             // same thing in here as well
  }

   cout << "\nAfter reversal: ";  //display list
   for(iter1=theVector.begin(); iter1 != theVector.end(); iter1++)
      cout << *iter1 << " ";
   cout << endl;
   return 0;
   }
Last edited on
someone look at this please?
On line 30, you can't do that because begin is already at the start, --'ing it will go into memory that isn't yours.
thanks for the reply firedraco

can you tell me then why I cant do the following code:
I got the program to do what I nedd.

But I made some changes as I am trying to figure out the process

Changes are the pointer is set at end instead of begining.

Thanks for the help everyone

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

   // I changed the pointer to the end
   iter1 = theVector.end();       //set to first element
   iter2 = theVector.rend();      //set to first element    
  
  for(int i = 0; i<=theVector.size()/2; i++)
	{
	  swap(*iter1, *iter2);  //swap front and back

	  --iter1; // Why not?????? I am pointing at the end so i shpuld decrement?!?!
	  --iter2; // Why not?????? same here

             // ++iter1; // what happenes here with the pointer set at end 
                               // asuming it points at nothingas it is at the end????
	
             // ++iter2; // what happens in her as well????
             }
Last edited on
end is not the last element, end is one element *after* the end. Why don't you just start at begin and increment?
thanks again for the fast reply firedraco

I did solve like that in the first place using begin amd rbegin

but I wanted to see if I placed it at the end I should decrement.

Your explenation made it clear for me

All I have to do call end() -1 and then --iterator

same for rend()-1 and then --reverse_iterator

thanks a lot for the help

Last edited on
Topic archived. No new replies allowed.