0
int main(){
    std::vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(vec[0]);
    for (auto it = vec.begin(); it != vec.end(); it++) {
        std::cout << *it << std::endl;
    }
    return 0;
}

This outputs [1, 2, 1], which I understand because the vector is making a copy of vec[0] and pushing it back. However, I would like the output to be like [2, 1] and was curious if I can accomplish this without using push_back() and then having to use erase();

The goal: I want to be able to move an element in the vector to the end while the vector adjusts itself accordingly, so if I move an element at the 5th position to the end, all the elements after the original 5th position would move up, so the new 5th position would have the element at the old 6th position and so forth.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
ygongdev
  • 264
  • 3
  • 19
  • "move an element in the vector to the end" is very different from "push back by reference", whatever the latter could mean. – n. m. could be an AI Jun 18 '16 at 20:30
  • Would it be better using a list instead? – skypjack Jun 18 '16 at 20:30
  • @skypjack Unlikely. The cost of finding the deletion/insertion point tends to dominate. Prefetchers don't like node based data structures. – Baum mit Augen Jun 18 '16 at 20:32
  • @BaummitAugen Maybe I read it too fast, but I understood that the OP wants to move around elements within its _vector_. – skypjack Jun 18 '16 at 20:33
  • @skypjack I got that. Somewhat counterintuitively, a vector tends to do this faster than a list. – Baum mit Augen Jun 18 '16 at 20:36
  • @BaummitAugen I've just learned something new. Interesting. Any reference about the off-topic topic? :-) – skypjack Jun 18 '16 at 20:37
  • @skypjack https://www.youtube.com/watch?v=YQs6IC-vgmo for example. – Baum mit Augen Jun 18 '16 at 20:38
  • @BaummitAugen oh, no, ok, but what's the linear search made by the OP? He wants to move an element at the end if the vector. That's something different from what you mentioned. – skypjack Jun 18 '16 at 20:49
  • @skypjack a) Apparently, he does not always want to permute the first element, so he would have to linear search for the element he wants to move. b) He probably will use the data for something after shifting, and traversing the list is incredibly slow. As always, valid unless measurement says otherwise. – Baum mit Augen Jun 18 '16 at 20:54
  • So this is actually for a problem involving LRU Cache that I'm working on and I'm using a vector to store iterators for the unordered_map. The goal was that I iterate through the vector, find the element I was looking for and pushed it back to the end without making a copy. – ygongdev Jun 18 '16 at 21:47

1 Answers1

7

You are probably looking for std::rotate.

int main() {
    //Fill vector
    std::vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);

    //move first element to the end
    std::rotate(vec.begin(), vec.begin() + 1, vec.end());

    //print content
    for (auto e:vec) {
        std::cout << e << std::endl;
    }
}

Explanation for the parameters:

  • The first and last parameter denote the range of all elements that will be moved around (so here the whole container).
  • The second parameter is an iterator to the element that lands on the first position after the rotate. When you want to move the first element to the end, the second one is the one (index 1) to move to the front.

Essentially, you can see the the distance between the second and the first parameter (here 1) as the number of elements, by which the range gets shifted.

MikeMB
  • 20,029
  • 9
  • 57
  • 102