[英]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: 这些是您的代码上的一些注意事项:
p2
declaration: int *p2, *p2;
p2
声明: int *p2, *p2;
. 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
来实现相同的效果)。 *p2 = *p1;
*p2 = *p1;
not *p1 = *p2;
*p1 = *p2;
- the right-hand side is assigned to the left hand side. j
, you do not need j[i] = &p2;
j
,不需要j[i] = &p2;
which alters j
's contents. j
的内容。 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 ++特色。
return 0;
return 0;
from main; i
and j
are common variable names for integer scalars, eg counters - not arrays. i
和j
是整数标量的通用变量名,例如,计数器-而不是数组。 I'd suggest you use a
and b
for the arrays, or values
and copy_of_values
etc. a
和b
表示数组,或者使用values
和copy_of_values
等。 std::vector
. std::vector
的类似数组的容器类。 It's not exactly the same as an array; 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";
}
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_each
或std::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.