say iter1 and iter2 are both iterators

*iter1++=*iter2++;

what does this line means? Can anybody help explain? ++ and * which is done first?
thanks

Recommended Answers

All 4 Replies

Using the ++ operator on a pointer is different from a traditional variable. *ptr++ will increment the memory space which ptr is pointing to based on the variable type.

For example, if you have

int *ptr;

and suppose it's pointing at memory location 1000. We know that an integer is 4 bytes, right? So if we do

*ptr++;

our pointer will now be pointing to location 1004.

As for which occurs first, I'm pretty sure that since the ++ operator is on the right of the statement, it occurs first, then assignment. If it were the other way around (e.g., ++*ptr) you would first assign the values and the increment. Someone should probably check my answer on this part though...

say iter1 and iter2 are both iterators

*iter1++=*iter2++;

what does this line means? Can anybody help explain? ++ and * which is done first?
thanks

Here's a page that will be very helpful!

Operator precedence in c++ is important, but I prefer explicit code to implicit trickery. Parentheses are your friend, and they make sure that your logic is plain. In this case the post-increment operator is evaluated before the dereference. Try this code first to see how this works:

#include <iostream>

using namespace std;

int main()
{
    int arr[4] = { 2, 3, 5, 7 };
    int* ptr = arr + 1;
    *ptr++;
    cout << *ptr << endl;
    return 0;
}

output:

2,3,5,7,
5

Even though I am using pointers here, the logic is the same.
Here's what's happening:
Line 8: ptr references the second value of the array == 3
Line 9, part 1: Post-increment is evaluated. This means the pointer will be incremented, but the increment will happen after this line of code is executed. So, the pointer will still refer to the second element of the array for this line
Line 9, part 2: The pointer is dereferenced. However, nothing else happens here, so...boring.
Line 10,11: Output the values of the array
Line 12: Output the value of the element to which the pointer refers. Since, between line 9 and line 10 the pointer was incremented, it now points to the third value of the array == 5

Now let's try something a little wonkier:

#include <iostream>

using namespace std;

int main()
{
    int arr[4] = { 2, 3, 5, 7 };
    int* ptr = arr + 1;
    (*ptr++)++;
    for( int i=0; i<4; i++ )
        cout << arr[i] << "," << flush;
    cout << endl << *ptr << endl;
    return 0;
}

output:

2,4,5,7,
5

Here's what's happening:
Line 8: ptr references the second value of the array == 3
Line 9, part 1: Post-increment is evaluated. This means the pointer will be incremented, but the increment will happen after this line of code is executed. So, the pointer will still refer to the second element of the array for this line
Line 9, part 2: The pointer is dereferenced.
Line 9, part 3: The second post-increment operator is evaluated. Now the derefrenced value will be incremented between line 9 and 10
Line 10,11: Output the values of the array. Notice that the second element of the array is now 4. This is because the derefereced value was incremented after line 9 was executed.
Line 12: The pointer was also incremented and now points to the third element

Now, as to your original code, you should be able to understand what is happening. Just remember that an iterator behaves very much like a pointer. *iter1++=*iter2++; 1. iter1 is post-incremented. After this line, it will refer to its next value. However, for the remainder of this line, it will still refer to its original value.
2. iter2 is post-incremented. Same logic step 1.
3. iter1 is dereferenced. Remember that it still refers to the same value it did at the start of the line
4. iter2 is dereferenced same logic as step 3.
5. The value to which iter1 is referring is set to the value to which iter 2 is referring. Remember that these are the same values that each iterator was referencing before the start of this line.
6. After the line finishes execution, both iterators will be incremented to refer to their next values.

I hope this helped some. Like I said, I recommend using parenthesis so you don't get lost. You should do this at least until you are more comfortable with iterators and how they work: *(iter1++)=*(iter2++); Alternatively, you could split this into more lines to make the logic much more clear:

*iter1 = *iter2;
iter1++;
iter2++;

Good luck!

Someone should probably check my answer on this part though...

I think you have your funcionality backward.

Using this code:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
using namespace std;

typedef vector<int>::const_iterator constIntIter;
typedef vector<int>::iterator intIter;

int main() {
  const int NUM_COUNT = 10;
  const int NUM_RANGE = 101;
  srand((unsigned)time(0));

  //declare the vector
  vector<int> numbers;
  numbers.reserve(NUM_COUNT);

  //populate the vector
  for(int i = 0; i < numbers.capacity(); ++i) {
    int temp = rand() % NUM_RANGE;   //generate a random number from 0-100
    numbers.push_back(temp);
  }

  //display the vector
  cout << "The vector's contents (for reference): " << endl;
  for (constIntIter pcIter = numbers.begin(); pcIter != numbers.end(); ++pcIter) {
    cout << *pcIter << " ";
  }

  cout << endl;

  cout << "Set pIter and pIter2 to beginning of vector:\n";
  intIter pIter = numbers.begin();
  intIter pIter2 = numbers.begin();
  cout << "The pointer stored in pIter is: " << pIter._Myptr << endl;
  cout << "The pointer stored in pIter2 is: " << pIter2._Myptr << endl;
  cout << "The value stored @ pIter is: " << *pIter << endl;
  cout << "The value stored @ pIter2 is: " << *pIter2 << endl;
  cout << "\nAdding 4 to pIter2, its new pointer is: ";
  pIter2 += 4;
  cout << pIter2._Myptr << endl;
  cout << "The value stored @ pIter2 is now: " << *pIter2 << endl;
  cout << "\nExecuting statement \"*pIter++=*pIter2++\" ..." << endl;
  *pIter++=*pIter2++;

  cout << "The pointer stored in pIter is now: " << pIter._Myptr << endl;
  cout << "The pointer stored in pIter2 is now: " << pIter2._Myptr << endl;
  cout << "The value stored @ pIter is now: " << *pIter << endl;
  cout << "The value stored @ pIter2 is now: " << *pIter2 << endl;
  cout << "\nThe value stored @ numbers.begin() is now: " << *(numbers.begin()) << endl;
  cout << "The value stored @ (pIter2 - 1) is now: " << *(pIter2 - 1) << endl;
  cout << "\nThe vector's new contents (for reference): " << endl;
  for (constIntIter pcIter = numbers.begin(); pcIter != numbers.end(); ++pcIter) {
    cout << *pcIter << " ";
  }

  return 0;
}

You get this

The vector's contents (for reference):
30 32 47 63 56 67 43 52 29 26
Set pIter and pIter2 to beginning of vector:
The pointer stored in pIter is: 00345C10
The pointer stored in pIter2 is: 00345C10
The value stored @ pIter is: 30
The value stored @ pIter2 is: 30

Adding 4 to pIter2, its new pointer is: 00345C20
The value stored @ pIter2 is now: 56

Executing statement "*pIter++=*pIter2++" ...
The pointer stored in pIter is now: 00345C14
The pointer stored in pIter2 is now: 00345C24
The value stored @ pIter is now: 32
The value stored @ pIter2 is now: 67

The value stored @ numbers.begin() is now: 56
The value stored @ (pIter2 - 1) is now: 56

The vector's new contents (for reference):
56 32 47 63 56 67 43 52 29 26

EDIT:
Which seems to be consistent with dusk's explanation...

commented: Thanks for the correction. +7

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.