首页 > 编程语言 >C++编写一个程序,初始化一个 double 类型的数组,然后把该数组的内容拷贝至3个其他数组中(在 main()中声明这 4 个数组)。使用带数组表示法的函数进行第1份拷贝。使用带指针表示法和指针递

C++编写一个程序,初始化一个 double 类型的数组,然后把该数组的内容拷贝至3个其他数组中(在 main()中声明这 4 个数组)。使用带数组表示法的函数进行第1份拷贝。使用带指针表示法和指针递

时间:2022-10-13 20:33:26浏览次数:52  
标签:double source 数组名 数组 拷贝 copy

也就是说,给定以下声明,则函数调用如下所示: double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5}; double target1[5]; double target2[5]; double target3[5]; copy_arr(target1, source, 5); copy_ptr(target2, source, 5); copy_ptrs(target3, source, source + 5);

#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
void copy_arr(double target1[], double source[], int len) {
    for (int i = 0; i < len; i++) {
        target1[i] = source[i];
    }
}
void copy_ptr(double* target2, double* source, int len) {
    for (int i = 0; i < len; i++) {
        *target2++ = *(source +i);
    }
}
void copy_ptrs(double* target3, double* source_start, double* source_end) {
    for (; source_start < source_end; source_start++) {
        *target3 = *source_start;
        target3++;
    }
}
int main() {
    double source[5] = {1.1,2.2,3.3,4.4,5.5};
    double target1[5];
    double target2[5];
    double target3[5];

    copy_arr(target1, source, 5);
    cout << "target1:";
    for (int i = 0; i < sizeof(source) / sizeof(source[0]); i++){
        cout << target1[i] << " ";
    }
    cout << endl;

    copy_ptr(target2, source, 5);
    cout << "target2:";
    for (int i = 0; i < sizeof(source) / sizeof(source[0]); i++) {
        cout << target2[i] << " ";
    }
    cout << endl;

    copy_ptrs(target3, source, source+5);
    cout << "target3:";
    for (int i = 0; i < sizeof(source) / sizeof(source[0]); i++) {
        cout << target3[i] << " ";
    }
    cout << endl;

    system("pause");
    return 0;
}

标签:double,source,数组名,数组,拷贝,copy
From: https://www.cnblogs.com/smartlearn/p/16789550.html

相关文章

  • Java基础(四)| 数组及内存分配详解
    ⭐本专栏旨在对JAVA的基础语法及知识点进行全面且详细的讲解,完成从0到1的java学习,面向零基础及入门的学习者,通过专栏的学习可以熟练掌握JAVA编程,同时为后续的框架学习,进阶开......
  • 对象与数组的复杂拼接
    把一个对象中自己需要的某些数据拼接到数组中的对象里面;要求:现在有一个数组testData:[{name:'张三',gender:'男',class:'20181421'},{name:'李四',gender:'男',class:'......
  • 53. 最大子数组和
    给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。子数组 是数组中的一个连续部分。示例1:输入:nums=[-2,1,-3,4,-1......
  • js数组去重
    数组去重关键点在于indexOf()的使用,未查询到目标字符串时返回值为-1//数组去重vararr=[45,12,1,2,4,45,12,3,4,5,5,6];varnewArr=[];......
  • 冒泡排序(对于数组元素较少的可以采用这种方法进行比较)
    对于数组个数比较少的,我们可以采用冒泡排序的方法来进行排序,他的原理其实是利用两层循环来进行比较,如果n个数要进行排序,那至少要进行n-1次的回合,而且每次需要排n-i次,就像吐......
  • 数组的find/findIndex详解
    ​​find()​​ 返回数组中满足提供的测试函数的第一个元素的值。否则返回undefined。​​find​​​方法对数组中的每一项元素执行一次​​callback​​​ 函数,直至有一......
  • 将多层级数组转化为一级数组(即提取嵌套数组元素最终合并为一个数组)
    需求:多维数组=>一维数组letary=[1,[2,[3,[4,5]]],6];//->[1,2,3,4,5,6]1.调用ES6中的flat方法ary=ary.flat(Infinity); ​​flat()​​ 方法会移除数......
  • JS判断数组中是否包含某个值
    方法一:array.indexOf此方法判断数组中是否存在某个值,如果存在,则返回数组元素的下标,否则返回-1。vararr=[1,2,3,4]varindex=arr.indexOf(3)console.log(index)方法......
  • arguments详解,类数组转数组方法
    为什么需要arguments对象由于​​JavaScript​​​允许函数有不定数目的参数,所以需要一种机制,可以在函数体内部读取所有参数。这就是​​arguments​​对象的由来。通......
  • 剑指Offer03.数组中重复的数字
    1.题目描述找出数组中重复的数字。在一个长度为n的数组nums里的所有数字都在0~n-1的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复......