如何将此INT数组的一部分复制到C 中的另一个数组
How would I copy a part of this int array into another in C++?
这就是我到目前为止所拥有的:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int size;
int sizeCopy= 0;
int *array;
cin >> size;
cout << endl;
cin >> sizeCopy;
array = new int [size];
int i = 0;
int counter = 1;
while(i<size)
{
array[i] = counter;
++counter;
++i;
}
cout << "The array contains: ";
for(i=0; i<size; i++)
{
cout << array[i] << ", ";
}
cout << "n";
return 0;
}
到目前为止,我创建了一个程序,该程序将要求使用数组大小的用户输入,以及在新数组中将复制数组的数量的大小(这是Sizecopy)。
我设法创建了第一个数组,我希望它是这样的:如果用户输入为3,则数组将包含1、2、3。i用户输入为10,数组将包含1、2、3、4、5、6、7、8、9、10。
这似乎很好。现在,我想将我的一部分int阵列复制到另一个阵列,但比这要复杂得多。假设第一个数组的用户输入(大小)为10,而Sizecopy(您想要复制到第二个数组中的元素有多少个元素)为2,那么我希望第二个数组为:1,2。不过还没有结束,我希望整个数组在一段时间后将整个数组复制到第二个数组中。我想要这样的东西:
- 1,2
- 收到第1部分
- 1,2,3,4
- 收到第2部分
- 1、2、3、4、5、6
- 收到第3部分
- 1、2、3、4、5、6、7、8
- 收到第4部分
- 1、2、3、4、5、6、7、8、9、10
- 收到第5部分
计划中包含"收到的部分"。然后将完成。
如果第一个数组大小的用户为4,而sizecopy是2,那么我想要这样的东西:
- 1,2
- 收到第1部分
- 1,2,3,4
- 收到第2部分
我可以在如何完成此操作方面获得一些帮助吗?我认为我可以管理如何将整个数组复制到另一个数组,或者至少一部分。但是,我想做什么?我以为我需要循环,但我不知道更多。
tl; dr我要回答标题中的问题。
与构造函数一起使用 std::vector
template< class InputIt >
vector( InputIt first, InputIt last,
const Allocator& alloc = Allocator() );
或std::copy
我不喜欢C 方法(sTD :: copy) - 我宁愿使用memcpy()
或memmove()
。我在 _control_ </sarcasm>
相关文章:
- 在c++中为double类型的数组创建一个unique_ptr
- 从数组构造一个跨度<shared_ptr<T>><shared_ptr<void>>
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 如何将 v8::FunctionCallbackInfo<v8::Value> 数组从一个隔离复制到另一个隔离?
- 我怎么能有一个以2D数组为参数的函数,而数组有一个我想更改的参数/维度
- 为什么我的数组输出一个不在其中的元素
- 数组 - 最后一个索引
- 将数组作为一个参数从一个函数传递到C 中的另一个函数
- 从字符串数组创建一个字符数组
- 如何在C 中的小写中将文本从数组的一个维度传递到另一个维度
- 在对结构数组的一个成员进行排序后,移动其成员的其余部分
- 为什么数组在一个文件中打印,但在发送到另一个文件时却缺少数字
- C++,为什么多维数组的一个元素的增加似乎增加了另一个元素
- 将数组的一个元素与整个数组元素进行比较
- 如何将数组从一个函数传递到另一个函数
- 查找最大和连续子数组-另一个版本
- 使用for循环将char数组的一个元素复制到另一个元素
- C++如何将变量char数组从一个函数传递到另一个函数
- 数组 ->另一个空的 int 数组混淆
- 用另一个字符数组替换一个字符数组