简体   繁体   English

所以我必须将一个数组旋转一个 position 但不分配数组以外的额外空间。 这是一个正确的解决方案吗?

[英]So I have to rotate an array by one position but withou allocating extra space other than the array. Is this a correct solution?

Here is my solution.这是我的解决方案。 Is it correct?这是对的吗? Ignore bits/stdc++.h etc. I just want to know if it only uses the space allocated for the vector.忽略 bits/stdc++.h 等。我只想知道它是否只使用分配给向量的空间。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cout << "Size of array?\n";
    cin >> n;
    int x[n];
    for (int i = 0; i < n; i++) {
        cout << "Input x[" << i << "]:\n";
        cin >> x[i];
    }
    for (int i = 0; i < n; i++) {
        cout << x[i] << "; ";
    }
    for (int i = 1; i<n; i++) {
        swap(x[0],x[i]);
    }
    for (int i = 0; i < n; i++) {
        cout << x[i] << "; ";
    }
}

For starters variable length arrays like this对于初学者可变长度 arrays 像这样

int n;
cout << "Size of array?\n";
cin >> n;
int x[n];

is not a standard C++ feature.不是标准的 C++ 功能。 Instead you should use the standard container std::vector<int> .相反,您应该使用标准容器std::vector<int>

To rotate an array or an object of the type std::vector<int> you can use the standard algorithm std::rotate .要旋转std::vector<int>类型的数组或 object,您可以使用标准算法std::rotate

As for your code then if you want to rotate the array left then this loop至于你的代码,那么如果你想向左旋转数组,那么这个循环

for (int i = 1; i<n; i++) {
        swap(x[0],x[i]);
}

should be written like应该写成

for (int i = 1; i<n; i++) {
        swap(x[i-1],x[i]);
}

Otherwise your loop rotates an array right.否则,您的循环将向右旋转数组。

Here is a demonstrative program.这是一个演示程序。

#include <iostream>
#include <utility>
#include <vector>

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

    for ( const auto &item : v )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    for ( size_t i = 1; i < v.size(); i++ )
    {
        std::swap( v[i-1], v[i] );
    }
    
    for ( const auto &item : v )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    
    return 0;
}

The program output is程序 output 是

1 2 3 4 5 
2 3 4 5 1 

Here is a demonstrative program where there is used the standard algorithm std::rotate .这是一个演示程序,其中使用了标准算法std::rotate

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

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

    for ( const auto &item : v )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    std::rotate( std::begin( v ), std::next( std::begin( v ) ), std::end( v ) );
    
    for ( const auto &item : v )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    
    return 0;
}

The program output is the same as shown above程序output同上图

1 2 3 4 5 
2 3 4 5 1 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (C++) 我必须使用幻数来显示数组中正确 position 中的值。 我怎样才能摆脱它? - (C++) I have to use magic numbers to display values in the correct position in an array. How can I get rid of it? 为数组分配空间 - Allocating space for an array 这是在线性时间和恒定空间中吗? (排序数组,让所有的0都在最后,其他数字在前面(解决方案)) - Is this in linear time and constant space? (sort array, so that all 0s are at the end, other numbers are at front (solution)) 我的阵列有足够的堆栈空间,为什么这个循环失败? - I have enough stack space for my array, so why does this loop fail? 我正在使用快速排序对数组进行排序。 但是我让数组未排序。 我试图找到错误但失败了 - I am using quick sort to sort an array. But am getting the array unsorted. I have tried to find the error but have failed 动态数组仅分配一个元素 - Dynamic array only allocating one element 如何在不分配更多存储空间的情况下从数组初始化向量? - How to initialize vector from array without allocating more storage space? 为什么C ++为动态数组分配如此大的内存空间? - Why is C++ allocating such a large space in memory for my dynamic array? 我如何创建一个数组类,其下界可以为零(在C ++中)? - How would I create an Array class that can have lower bounds other than zero (in C++)? 我想让用户 select 有一个他们想要保留并放入数组中的数字。 并将它们放入单独的索引中 - I want to have a user select a number that they want to keep and put in an array. And have them put into separate index's
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM