c++ - Reversing vector's elements by their iterators -


i tried write following function:

typedef std::vector<int>::iterator v_itt;   void reverse(v_itt first, v_itt second)  {          std::cout << "first = " << *first << ", second = " << *second << std::endl;          int *temp = &*first;          *second = *first;          *first = *temp;          std::cout << "after reversing: first = " << *first << ", second = " << *second << std::endl;  } 

but when tried call got following output:

first = 322, second = 12 after reversing: first = 322, second = 322 

i thought, when we're initializing pointer pointer, in initialized pointer copy of value. isn't true?

to swap elements should this:

temp = first first = second // here have "second = first". it's other way around! second = temp 

you have made mistake @ second step, results in reverse operation giving first both. key idea is: store value (first) temp, following operation can overwrite that value (first) because won't lose it.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -