简体   繁体   English

将一个int数组复制到另一个C ++

[英]Copying one int array to another C++

Was trying to write a program which converts the value`s from one assigned array to another unassigned one. 试图编写一个程序,将值从一个分配的数组转换为另一个未分配的数组。 The code i wrote: 我写的代码:

#include "stdafx.h";
#include <iostream>;

using namespace std;

int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int j[10];

int copy_array(int *p1, int n);
int *p2, *p2;

int main() {

    for (int l = 0; l < 10; l++) {
        cout << a[l] << endl;
    }

    copy_array(a, 10);

    for (int i = 0; i < 10; i++) {
        j[i] = &p2;
        cout << j[i] << endl;
    }

    system("PAUSE");
    return 0;
}

int copy_array(int *p1, int n) {
    while (n-- > 0) {
        *p1 = *p2;
            *p1++;
        *p2++;
    }
}

Im using the Microsoft visual studio platform and the error i got was "There is no context in which this conversion is possible". 我正在使用Microsoft Visual Studio平台,但出现的错误是“没有上下文可以进行这种转换”。 Why i cant use this int convert path? 为什么我不能使用此int转换路径? how can i fix and connect the 2 arrays using int type conversion(if its possible)? 我如何使用int类型转换(如果可能)修复并连接2个数组?

What i tried was manipulating the local function copy_array so it makes the conversion using the addresses of the j[10] array integers, but this gave me another error. 我尝试操作的是本地函数copy_array,以便它使用j [10]数组整数的地址进行转换,但这给了我另一个错误。 Any support and advice would be appreciated. 任何支持和建议,将不胜感激。

You don't need p2 to be global. 您不需要p2是全局的。

Just add parameter to copy_array . 只需将参数添加到copy_array

like this: 像这样:

void copy_array(int *p1, int *p2, int n) {
    while (n-- > 0) {
        *p1 = *p2;
        p1++;
        p2++;
    }
}

and call like this: 并这样调用:

copy_array(j, a, 10);

Also: to print the copy you just do: 另外:要打印副本,您只需执行以下操作:

 for (int i = 0; i < 10; i++) {
        cout << j[i] << endl;
    }

These are some notes on your code: 这些是您的代码上的一些注意事项:

  1. you have redundant p2 declaration: int *p2, *p2; 您有多余的p2声明: int *p2, *p2; . Also you need to initialize it. 您还需要初始化它。 so make it: int *p2 = j; 所以做到: int *p2 = j; (in fact, you don't actually need to use this global variable - you can achieve the same effect by passing j as necessary). (实际上,您实际上不需要使用此全局变量-您可以根据需要传递j来实现相同的效果)。
  2. Inside your copy function, your assignment should be in reverse: *p2 = *p1; 在复制函数中,您的分配应该相反: *p2 = *p1; not *p1 = *p2; 不是*p1 = *p2; - the right-hand side is assigned to the left hand side. -右侧分配给左侧。
  3. When printing j , you do not need j[i] = &p2; 当打印j ,不需要j[i] = &p2; which alters j 's contents. 这会改变j的内容。
  4. It is better to define the arrays inside the function not in the general scope. 最好不在函数范围内定义函数内部的数组。

Correct them and your code should work fine. 更正它们,您的代码应该可以正常工作。

However, You do not need pointers to do this at all. 但是,您根本不需要指针来执行此操作。

Consider the following code and compare it to yours: 考虑以下代码并将其与您的代码进行比较:

#include <iostream>
using namespace std;  

void copy_array(int [], int [], int);
void print_array(int [], int);

int main() {

    int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int j[10];

    print_array(a,10);
    copy_array(a, j, 10);
    print_array(j,10);
    return 0;
}

void copy_array(int s[], int d[], int n) {
    for (int i = 0; i < n; i++)
        d[i] = s[i];   
}    // s for source & d for destination

void print_array(int arr[], int n) {
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << "\n\n";
}

I want to build on @Shadi's answer, which you should upvote, and make the code more C++-idiomatic. 我想以@Shadi的答案为基础,您应该对此予以支持,并使代码更具C ++特色。

  • In C++, we don't need to explicitly return 0; 在C ++中,我们不需要显式return 0; from main; 从主要 it is implied, if you haven't returned anything else. 如果您还没有返回任何其他信息,则暗示。
  • It's better to use names in a similar scheme for similar variables. 对于类似的变量,最好在类似的方案中使用名称。 Specifically, i and j are common variable names for integer scalars, eg counters - not arrays. 具体来说, ij是整数标量的通用变量名,例如,计数器-而不是数组。 I'd suggest you use a and b for the arrays, or values and copy_of_values etc. 我建议您使用ab表示数组,或者使用valuescopy_of_values等。
  • The C++ standard library has an array-like container class named std::vector . C ++标准库具有一个名为std::vector的类似数组的容器类。 It's not exactly the same as an array; 它与数组不完全相同; for example, it uses dynamically-allocated memory, and can grow or shrink in size. 例如,它使用动态分配的内存,并且大小可以增加或缩小。 The reason you might want to use it is that it allows you to perform plain assignment, and use other standard library facilities with it. 您可能要使用它的原因是它允许您执行普通分配,并与它一起使用其他标准库功能。

Thus Shadi's program becomes: 因此,沙迪的程序变为:

#include <iostream>
#include <vector>

void print_vector(const std::vector<int>& vec);

int main() {
    std::vector<int> a { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    std::vector<int> b;

    print_vector(a);
    b = a;
    print_vector(b);
}

void print_vector(const std::vector<int>& vec) {

    // this next line uses syntax from the 2011 version of
    // the C++ language standard ("C++11").
    for(int x : vec) {
        std:cout << x << " ";
    }
    cout << "\n\n";
}
  • You can also avoid the loop in print_vector entirely, using std::for_each or std::for_each_n , but that would require some knowledge of iterators and lambda functions, which may be a bit advanced for a beginner, so I won't go into that. 您还可以使用std::for_eachstd::for_each_n完全避免print_vector的循环,但这将需要一些迭代器和lambda函数的知识,对于初学者来说可能有些高级,所以我不再赘述那。 But better yet, you could define a out-streaming operator for std::vector 's, as seen here , with which you could write std::cout << a; 但更重要的是,你可以定义为一个出流运营商std::vector的,因为看到这里 ,与您可以写std::cout << a; and have that work. 并有工作。

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

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