C++ 如何将数组复制到更大的数组中,并将零添加到未使用的空间中?C++;
C++ 我正在制作一个程序,要求用户输入一个数字,将该数字设置为第一个数组的大小,然后根据大小要求用户输入数字。然后它请求另一个数组大小并将其设置为第二个数组大小。如果第二个数组的大小大于第一个,我需要将第一个数组复制到第二个数组中,并将0添加到未使用的空间中 例如,输出应如下所示:C++ 如何将数组复制到更大的数组中,并将零添加到未使用的空间中?C++;,c++,arrays,C++,Arrays,C++ 我正在制作一个程序,要求用户输入一个数字,将该数字设置为第一个数组的大小,然后根据大小要求用户输入数字。然后它请求另一个数组大小并将其设置为第二个数组大小。如果第二个数组的大小大于第一个,我需要将第一个数组复制到第二个数组中,并将0添加到未使用的空间中 例如,输出应如下所示: Enter first array size: 3 Enter a number: 45 Enter a number: 54 Enter a number: 65 45 54 65 // First array
Enter first array size: 3
Enter a number: 45
Enter a number: 54
Enter a number: 65
45 54 65 // First array printed out
Enter second array size: 10
45 54 65 0 0 0 0 0 0 0 // Second array printed out
如果第二个数组更大,我需要帮助将0添加到未使用的空间
此外,我不确定如何复制阵列是最好的方法
代码片段:
int *secondArray = new int[secondSize]; // Create array
for(int j = 0; j < secondSize; ++j)
{
secondArray[j] = firstArray[j]; // Set second array equal to first array
if (secondSize > firstSize)
{
// ?
}
}
int*secondArray=newint[secondSize];//创建数组
对于(int j=0;j第一个大小)
{
// ?
}
}
就是这么简单(但请注意,j
替换了代码中的secondSize
)--
编辑:请参阅下面@Fred Larson关于避免冗余分配的评论。以下是简洁的版本:
secondArray[j] = (j >= firstSize) ? 0 : firstArray[j];
就是这么简单(但请注意,j
替换了代码中的secondSize
)--
编辑:请参阅下面@Fred Larson关于避免冗余分配的评论。以下是简洁的版本:
secondArray[j] = (j >= firstSize) ? 0 : firstArray[j];
我建议使用STL向量,因为它们会自动处理内存管理()。但是,如果出于任何原因必须使用数组,我建议您使用以下类似的方法:
int *secondArray = new int[secondSize]; // Create array
//Initialize secondArray to 0
for(int i = 0; i < secondSize; i++) secondArray[i] = 0;
//Then use your code
for(int j = 0; j < secondSize; ++j)
{
secondArray[j] = firstArray[j]; // Set second array equal to first array
}
int*secondArray=newint[secondSize];//创建数组
//将secondArray初始化为0
对于(int i=0;i
但这不会检查secondSize
int *secondArray = new int[secondSize]; // Create array
//Initialize secondArray to 0
for(int i = 0; i < secondSize; i++) secondArray[i] = 0;
//Then use your code
for(int j = 0; j < secondSize; ++j)
{
secondArray[j] = firstArray[j]; // Set second array equal to first array
}
int*secondArray=newint[secondSize];//创建数组
//将secondArray初始化为0
对于(int i=0;i
但这不会检查secondSize
int* secondArray = new int[secondSize];
memset(secondArray, 0, sizeof(int) * secondSize);
然后只需将第一个数组复制到第二个数组的第一个元素中:
for(int j = 0; j < firstSize; ++j)
{
secondArray[j] = firstArray[j]; // Set second array equal to first array
}
for(int j=0;j
这是假设secondSize始终大于firstSize。您还可以创建第二个数组并将其所有元素设置为0:
int* secondArray = new int[secondSize];
memset(secondArray, 0, sizeof(int) * secondSize);
然后只需将第一个数组复制到第二个数组的第一个元素中:
for(int j = 0; j < firstSize; ++j)
{
secondArray[j] = firstArray[j]; // Set second array equal to first array
}
for(int j=0;j
这是假设secondSize始终大于firstSize。如果secondSize大于firstSize,代码段将从firstArray复制不存在的数据 不要将if语句放入循环中。那是浪费时间 试试这个:
size_t i;
if(firstSize < secondSize) {
for(i=0; i<firstSize; ++i)
secondArray[i] = firstArray[i];
for(;i<secondSize; ++i)
secondArray[i] = 0;
} else {
for(i=0; i<secondSize; ++i)
secondArray[i] = firstArray[i];
}
size\u ti;
如果(第一个大小<第二个大小){
对于(i=0;i,如果secondSize大于firstSize,代码片段将从firstArray复制不存在的数据
不要把if语句放在循环中,那是浪费时间的
试试这个:
size_t i;
if(firstSize < secondSize) {
for(i=0; i<firstSize; ++i)
secondArray[i] = firstArray[i];
for(;i<secondSize; ++i)
secondArray[i] = 0;
} else {
for(i=0; i<secondSize; ++i)
secondArray[i] = firstArray[i];
}
size\u ti;
如果(第一个大小<第二个大小){
对于(i=0;i如果使用标准类std::vector
下面是一个使用动态分配数组和使用classstd::vector
完成任务的示例。您可以在两个代码块中选择所需的内容
#include <iostream>
#include <vector>
#include <cstring>
int main()
{
{
std::cout << "Enter first array size: ";
size_t n1 = 0;
std::cin >> n1;
int *a1 = new int[n1]();
for ( size_t i = 0; i < n1; i++ )
{
std::cout << "Enter a number: ";
std::cin >> a1[i];
}
std::cout << "Enter second array size: ";
size_t n2 = 0;
std::cin >> n2;
int *a2 = new int[n2]();
std::memcpy( a2, a1, ( n2 < n1 ? n2 : n1 ) * sizeof( int ) );
for ( size_t i = 0; i < n2; i++ ) std::cout << a2[i] << ' ';
std::cout << std::endl;
delete [] a1;
delete [] a2;
}
{
std::cout << "Enter first array size: ";
std::vector<int>::size_type n1 = 0;
std::cin >> n1;
std::vector<int> v1( n1 );
for ( std::vector<int>::size_type i = 0; i < n1; i++ )
{
std::cout << "Enter a number: ";
std::cin >> v1[i];
}
std::cout << "Enter second array size: ";
std::vector<int>::size_type n2 = 0;
std::cin >> n2;
std::vector<int> v2( v1.begin(), v1.begin() + ( n2 < n1 ? n2 : n1 ) );
v2.resize( n2 );
for ( int x : v2 ) std::cout << x << ' ';
std::cout << std::endl;
}
return 0;
}
#包括
#包括
#包括
int main()
{
{
std::cout>n1;
int*a1=新的int[n1]();
对于(大小i=0;ia1[i];
}
std::cout>n2;
int*a2=新的int[n2]();
std::memcpy(a2,a1,(n2 对于(intx:v2)std::cout来说,如果使用标准类std::vector
下面是一个使用动态分配数组和使用classstd::vector
完成任务的示例。您可以在两个代码块中选择所需的内容
#include <iostream>
#include <vector>
#include <cstring>
int main()
{
{
std::cout << "Enter first array size: ";
size_t n1 = 0;
std::cin >> n1;
int *a1 = new int[n1]();
for ( size_t i = 0; i < n1; i++ )
{
std::cout << "Enter a number: ";
std::cin >> a1[i];
}
std::cout << "Enter second array size: ";
size_t n2 = 0;
std::cin >> n2;
int *a2 = new int[n2]();
std::memcpy( a2, a1, ( n2 < n1 ? n2 : n1 ) * sizeof( int ) );
for ( size_t i = 0; i < n2; i++ ) std::cout << a2[i] << ' ';
std::cout << std::endl;
delete [] a1;
delete [] a2;
}
{
std::cout << "Enter first array size: ";
std::vector<int>::size_type n1 = 0;
std::cin >> n1;
std::vector<int> v1( n1 );
for ( std::vector<int>::size_type i = 0; i < n1; i++ )
{
std::cout << "Enter a number: ";
std::cin >> v1[i];
}
std::cout << "Enter second array size: ";
std::vector<int>::size_type n2 = 0;
std::cin >> n2;
std::vector<int> v2( v1.begin(), v1.begin() + ( n2 < n1 ? n2 : n1 ) );
v2.resize( n2 );
for ( int x : v2 ) std::cout << x << ' ';
std::cout << std::endl;
}
return 0;
}
#包括
#包括
#包括
int main()
{
{
std::cout>n1;
int*a1=新的int[n1]();
对于(大小i=0;ia1[i];
}
std::cout>n2;
int*a2=新的int[n2]();
std::memcpy(a2,a1,(n2 对于(intx:v2)std::cout使用向量(而不是数组)然后调整它的大小。完成。你考虑过这个吗?有什么可能?新的大小可以更大、更小或相同。写下覆盖每一个的if条件和for循环,你就完成了。@ZanLynx我已经有了数组更小或相同的条件。我只是需要这一小部分的帮助。如果你必须这样做的话,请啊,