Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shifting a vector in C++20

I'd like to use this new feature from C++20 of shifting a vector to the right std::shift_right.

Something that I'm not sure about is how can I get the last element to the beginning of the vector after shifting?

Like in the example above... Where I'm saving the last element in a temp variable and then after shifting I set the first element to be the temp variable.

#include <iostream>
#include <vector>
#include <algorithm>



int main()
{
    std::vector<int> seq = { 5, 4, 3, 2, 1 };

    std::vector<int>::iterator it;
    std::vector<int>::iterator temp;
    temp = seq.end() - 1;


    std::shift_right(seq.begin(), seq.end(), 1);
    std::replace(seq.begin(), seq.end(), seq.at(0), *temp);

    for (it = seq.begin(); it != seq.end(); it = std::next(it))
        std::cout << *it << " ";

    return 0;
}
like image 557
Daniel Avatar asked Aug 02 '20 21:08

Daniel


People also ask

What is left shift and right shift in C++?

Left Shift and Right Shift Operators in C/C++. << (left shift) Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. Or in other words left shifting an integer “x” with an integer “y” (x<<y) is equivalent to multiplying x with 2^y (2 raise to power y).

How to shift a single dimensional array in C?

This C program is to shift the elements of a single dimensional array in the right direction by one position.For example, if an array a consists of elements a= {1,2,3}, then on shifting these elements towards the right direction we would get a= {3,1,2}.

How to use the left shift operator in C printf?

Example 1: Program to demonstrate the use of the Left Shift operator in C printf (" After shifting the binary bits to the left side. "); Enter a positive number: 25 After shifting the binary bits to the left side. The new value of the variable num = 100

What is bit shift in C programming?

It is a binary operator that requires two operands to shift or move the position of the bits to the left side and add zeroes to the empty space created at the right side after shifting the bits.


1 Answers

While you can probably achieve what you want with std::shift_right, the appropriate algorithm to do what you've described is std::rotate, which you can use without c++20 as well:

std::rotate(seq.begin(), seq.end() - 1, seq.end());    

Here's a demo.

The reason the shift_right version doesn't work is that you have an iterator to the last element. When you finish shifting, the original last element is overwritten, and the iterator now points to the shifted element instead. So what you can do is make a copy of the last element and then put that at the beginning:

int temp = seq.back();
std::shift_right(seq.begin(), seq.end(), 1);
seq[0] = temp;

Here's a demo. But note that this is a hack; use shift_right if you don't care about those elements that you are overwriting. If you care about the elements, then rotate the range.

like image 198
cigien Avatar answered Oct 17 '22 06:10

cigien