簡體   English   中英

將一個int數組復制到另一個C ++

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

試圖編寫一個程序,將值從一個分配的數組轉換為另一個未分配的數組。 我寫的代碼:

#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++;
    }
}

我正在使用Microsoft Visual Studio平台,但出現的錯誤是“沒有上下文可以進行這種轉換”。 為什么我不能使用此int轉換路徑? 我如何使用int類型轉換(如果可能)修復並連接2個數組?

我嘗試操作的是本地函數copy_array,以便它使用j [10]數組整數的地址進行轉換,但這給了我另一個錯誤。 任何支持和建議,將不勝感激。

您不需要p2是全局的。

只需將參數添加到copy_array

像這樣:

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

並這樣調用:

copy_array(j, a, 10);

另外:要打印副本,您只需執行以下操作:

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

這些是您的代碼上的一些注意事項:

  1. 您有多余的p2聲明: int *p2, *p2; 您還需要初始化它。 所以做到: int *p2 = j; (實際上,您實際上不需要使用此全局變量-您可以根據需要傳遞j來實現相同的效果)。
  2. 在復制函數中,您的分配應該相反: *p2 = *p1; 不是*p1 = *p2; -右側分配給左側。
  3. 當打印j ,不需要j[i] = &p2; 這會改變j的內容。
  4. 最好不在函數范圍內定義函數內部的數組。

更正它們,您的代碼應該可以正常工作。

但是,您根本不需要指針來執行此操作。

考慮以下代碼並將其與您的代碼進行比較:

#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";
}

我想以@Shadi的答案為基礎,您應該對此予以支持,並使代碼更具C ++特色。

  • 在C ++中,我們不需要顯式return 0; 從主要 如果您還沒有返回任何其他信息,則暗示。
  • 對於類似的變量,最好在類似的方案中使用名稱。 具體來說, ij是整數標量的通用變量名,例如,計數器-而不是數組。 我建議您使用ab表示數組,或者使用valuescopy_of_values等。
  • C ++標准庫具有一個名為std::vector的類似數組的容器類。 它與數組不完全相同; 例如,它使用動態分配的內存,並且大小可以增加或縮小。 您可能要使用它的原因是它允許您執行普通分配,並與它一起使用其他標准庫功能。

因此,沙迪的程序變為:

#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";
}
  • 您還可以使用std::for_eachstd::for_each_n完全避免print_vector的循環,但這將需要一些迭代器和lambda函數的知識,對於初學者來說可能有些高級,所以我不再贅述那。 但更重要的是,你可以定義為一個出流運營商std::vector的,因為看到這里 ,與您可以寫std::cout << a; 並有工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM