简体   繁体   English

我们可以将 integer 添加到 c++ 中的数组吗

[英]Can we add an integer to an array in c++

#include <bits/stdc++.h> 
using namespace std; 

/*Prototype for utility functions */
void printArray(int arr[], int size); 
void swap(int arr[], int fi, int si, int d); 

void leftRotate(int arr[], int d, int n) 
{ 
    /* Return If number of elements to be rotated 
    is zero or equal to array size */
    if(d == 0 || d == n) 
        return; 
        
    /*If number of elements to be rotated 
    is exactly half of array size */
    if(n - d == d) 
    { 
        swap(arr, 0, n - d, d); 
        return; 
    } 
        
    /* If A is shorter*/        
    if(d < n - d) 
    { 
        swap(arr, 0, n - d, d); 
        leftRotate(arr, d, n - d);   
    } 
    else /* If B is shorter*/       
    { 
        swap(arr, 0, d, n - d); 
        leftRotate(arr + n - d, 2 * d - n, d); /*This is tricky*/
    } 
} 

/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int size) 
{ 
    int i; 
    for(i = 0; i < size; i++) 
        cout << arr[i] << " "; 
    cout << endl; 
} 

/*This function swaps d elements starting at index fi 
with d elements starting at index si */
void swap(int arr[], int fi, int si, int d) 
{ 
    int i, temp; 
    for(i = 0; i < d; i++) 
    { 
        temp = arr[fi + i]; 
        arr[fi + i] = arr[si + i]; 
        arr[si + i] = temp; 
    } 
} 

// Driver Code 
int main() 
{ 
    int arr[] = {1, 2, 3, 4, 5, 6, 7}; 
    leftRotate(arr, 2, 7); 
    printArray(arr, 7); 
    return 0; 
} 

// This code is contributed by Rath Bhupendra 

I found this code on the geek for geeks website.我在 geek for geeks 网站上找到了这段代码。 The code is used to rotate the elements of an array.该代码用于旋转数组的元素。 It is mentioned as block swap algorithm in the website, my questions are:它在网站上被称为块交换算法,我的问题是:

Can we add integers to an array in c++ as given in the else part of the left rotate function while passing the arguments (arr+nd)?我们可以在传递 arguments (arr+nd) 的同时向左旋转 function 的 else 部分中给出的 c++ 中的数组添加整数吗?

How can we add integers to an array?我们如何将整数添加到数组中?

I tried adding an integer to an array in an online compiler and it didn't work.我尝试在在线编译器中将 integer 添加到数组,但没有成功。 But the above code works perfectly giving the desired output 34567.但是上面的代码可以完美地给出所需的 output 34567。

The link to the website is https://www.geeksforgeeks.org/block-swap-algorithm-for-array-rotation/ .该网站的链接是https://www.geeksforgeeks.org/block-swap-algorithm-for-array-rotation/

Can we add integers to an array in c++ as given in the else part of the left rotate function while passing the arguments (arr+nd)?我们可以在传递 arguments (arr+nd) 的同时向左旋转 function 的 else 部分中给出的 c++ 中的数组添加整数吗?

How can we add integers to an array?我们如何将整数添加到数组中?

The answer is you can't, and that's not what's happening here.答案是你不能,这不是这里发生的事情。

int arr[] argument decays to a pointer to the first element of the array. int arr[]参数衰减为指向数组第一个元素的指针。 It's the same as having int* arr so what you are doing in arr + n - d is simple pointer arithmetic.它与int* arr相同,所以您在arr + n - d中所做的是简单的指针算术。

The pointer will be moved n - d positions relative to the position it's at before the expression is evaluated.在计算表达式之前,指针将相对于它所在的 position 移动n - d位置。

Supposing the result of n - d is 4, and arr is pointing to the beginning of the array passed as an argument, that is to &arr[0] (in array notation) or arr + 0 (in pointer notation), which is where it's pointing to in its inicial state, you'll have arr + 4 or &arr[4] , after the evaluation, the expression provides access to the address of index 4 (the 5th element of the array).假设n - d的结果是 4,并且arr指向作为参数传递的数组的开头,即指向&arr[0] (以数组表示法)或arr + 0 (以指针表示法),这是它指向它的 initial state,你将拥有arr + 4&arr[4] ,在评估之后,该表达式提供对索引 4(数组的第 5 个元素)的地址的访问。 To access the value within that address you'd use *(arr + 4) or arr[4] .要访问该地址中的值,您将使用*(arr + 4)arr[4]

On a side note I wouldn't advise the use of geeksforgeeks.com to learn C++, or any other language, for that matter, this should be done by reading a good book .另外,我不建议使用 geeksforgeeks.com 来学习 C++ 或任何其他语言,就此而言,这应该通过阅读一本好书来完成。

A function parameter having an array type is adjusted by the compiler to pointer to the array element type.编译器将具有数组类型的 function 参数调整为指向数组元素类型的指针。 That is these two function declarations are equivalent and declare the same one function.即这两个 function 声明是等价的,声明同一个 function。

void leftRotate(int arr[], int d, int n);

and

void leftRotate(int *arr, int d, int n);

You even may write for example你甚至可以写例如

void leftRotate(int arr[100], int d, int n);
void leftRotate(int arr[10], int d, int n);
void leftRotate(int arr[1], int d, int n);

Again these declarations declare the function这些声明再次声明 function

void leftRotate(int *arr, int d, int n);

So within the function this expression所以在 function 这个表达式内

arr + n - d

uses the pointer arithmetic applied to the pointer arr .使用应用于指针arr的指针算法。

For example the expression arr + 0 is equivalent to arr and points to the first element of the array.例如,表达式arr + 0等同于arr并指向数组的第一个元素。 The expression arr + n points to the n-th element of the array.表达式arr + n指向数组n-th元素。

Here is a demonstrative program where there is used the pointer arithmetic to output elements of an array in a loop.这是一个演示程序,其中在一个循环中使用指向数组的 output 个元素的指针算法。

#include <iostream>

int main() 
{
    int a[] = { 1, 2, 3, 4, 5 };
    
    for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
    {
        std::cout << *( a + i ) << ' ';
    }
    std::cout << '\n';
    
    return 0;
}

The program output is程序 output 是

1 2 3 4 5

In the expression *( a + i ) the array designator a is implicitly converted to pointer to its first element.在表达式*( a + i )中,数组指示符a被隐式转换为指向其第一个元素的指针。

Here is one more demonstrative program that shows that a function parameter having an array type is adjusted by the compiler to pointer to the array element type.下面是另一个演示程序,显示编译器将具有数组类型的 function 参数调整为指向数组元素类型的指针。

#include <iostream>
#include <iomanip>
#include <type_traits>

const size_t N = 100;

void f( int a[N] )
{
    std::cout << "\nin function\n";
    std::cout << "sizeof( a ) = " << sizeof( a ) << '\n';
    std::cout << "a is a pointer " << std::boolalpha <<std:: is_same<decltype( a ), int *>::value << '\n';
}

int main() 
{
    int a[N];
    
    std::cout << "In main\n";
    std::cout << "sizeof( a ) = " << sizeof( a ) << '\n';
    std::cout << "a is an array " << std::boolalpha <<std:: is_same<decltype( a ), int [N]>::value << '\n';

    f( a );
    
    return 0;
}

The program output is程序 output 是

In main
sizeof( a ) = 400
a is an array true

in function
sizeof( a ) = 8
a is a pointer true

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM