将数组中的值复制到另一个不同的大小
copy values from array into another different size
我有一个数组A[8]={0};而另一个数组B[20]={0};
我想把所有的值从B[12…20]移到A[0…8]。我怎样才能准确地更改索引?有公式吗?所以B[12]->A[0]B[13]-->A[1]
谢谢。
使用std::copy
。它也适用于用户定义的类型:
std::copy(B+12, B+20, A);
或者,在c++11中,
std::copy(std::next(B,12), std::end(B), std::begin(A));
您应该在这里使用std::copy
,无论数组中的元素类型如何,它都能正常工作(说到这一点,您没有显示那种类型——问题的语法无效)。
std::copy(B + 12, B + 20, A);
只需编写一个循环
int offset = 12;
int lenA = 8;
for(int i=0; i < lenA; i++) {
A[i] = B[i+offset];
}
memcpy(A, B + 12, 8 * sizeof(A[0]));
应该能做到这一点。
假设A和B都是同一类型。
相关文章:
- 矢量如何将数据复制到另一个矢量?
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 链表,将列表复制到另一个列表
- 将指向给定子类的指针从一个向量复制到另一个向量
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 将父类的子类的数据复制到具有相同父类的另一个类
- 使用动态分配将 char* 复制到另一个字符**
- 如何在不复制的情况下将一个向量移动到另一个向量中
- 将字符向量复制到另一个向量
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 如何使用函数将一个对象的输入复制到另一个对象中
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 复制矩阵的一部分并粘贴到另一个矩阵C++上
- std::transform 将一个结构数组复制到另一个结构数组
- 如何避免将向量的完整值复制到另一个容器
- 如何将列表从一个类复制到另一个类
- C++将结构复制到另一个程序的资源
- 复制赋值函数如何访问另一个对象的私有成员(Stroustroup 原则和实践书)?
- 如何使用C ++将SQLite数据库中的所有表从一个数据库复制到另一个数据库
- 另一个复制算法