zoukankan      html  css  js  c++  java
  • 改数组长度

    //改变数组长度
    
    
    #include <iostream>
    
    
    using namespace std;
    
    // #ifndef CHANGELENGTH1D_H
    #define CHANGELENGTH1D_H
    
    #include<stdexcept>
    #include<algorithm>
    
    template<typename T>
    void changeLength1D(T *&a,int oldLength, int newLength)
    {
        if(newLength < 0)
            throw std::runtime_error("New length must be >=0");
    
        T* temp = new T[newLength];
        int number = std::min(oldLength, newLength);
        //copy( from_vector.begin(), from_vector.end(), to_vector.begin())
        std::copy(a, a+number, temp);
        delete [] a;
        a = temp;
    }
    
    // #endif // CHANGELENGTH1D_H
    
    
    
    int main()
    {
        try{
            double *a = new double[2];
            a[0] = 11;
            a[1] = 12;
            changeLength1D(a,2,6);
    
            for(int i=2;i<6;i++)
                a[i]=i;
    
            for(int i=0;i<6;i++)
                cout << a[i] << endl;
            return 0;
        }catch(runtime_error err)
        {
            cout << err.what() << endl;
            return 0;
        }
    }
    

      

  • 相关阅读:
    vim配置文件解析
    VIM使用技巧5
    补不manjaro系统
    linux下终端录制
    VIM的修炼等级
    VIM使用技巧4
    64位linux 汇编
    linux下编译安装gcc5.1
    Git学习笔记
    HTML实体符号代码速查表
  • 原文地址:https://www.cnblogs.com/pythonClub/p/15518628.html
Copyright © 2011-2022 走看看