Fortran Coder

查看: 14161|回复: 10
打印 上一主题 下一主题

[讨论] VB中Redim功能讨论

[复制链接]

1967

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1370 元
贡献
581 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

楼主
发表于 2018-8-3 18:38:38 | 显示全部楼层
realloc 是 Sun 提供的函数,不是标准的。
不管是VB的 Redim Preserve,还是 C++ 容器类的 resize() 成员。其底层的实现本质,都是重新分配,然后复制原来的值。

Fortran需要自己去实现,而且由于Fortran没有模板类,所以每种类型都要写一遍,二维数组又要写一遍,三维还要写一遍。实际上,C++ 压根没有多维数组,VB Redim Preserve 也只能改变最后一维的大小,前面不能改变。
静态编程语言,改变大小都很难。想要自由编程,可以选 Python 或者 matlab 这种动态编程语言。想怎么加怎么加。

用预处理宏定义可以节省一点时间。

[Fortran] 纯文本查看 复制代码
Program Main
  Integer , allocatable :: x(:)
  Real    , allocatable :: y(:)
  allocate(x(4),y(4))
  x = 3 ; y = 4
  write(*,*) x
  write(*,*) y
  call ReallocateInt( x , 3 )
  call ReallocateReal( y , 6 )
  write(*,*) x
  write(*,*) y
  
contains
#define TYPE_  Integer  
  Subroutine ReallocateInt( x , n )
    TYPE_ , allocatable :: x(:) , t(:)
    integer :: n , s
    call move_alloc(x,t)
    allocate(x(n))
    s = min(size(t),n)
    x(:s) = t(:s)
    deallocate(t)
  End Subroutine ReallocateInt
#define TYPE_  Real  
  Subroutine ReallocateReal( x , n )
    TYPE_ , allocatable :: x(:) , t(:)
    integer :: n , s
    call move_alloc(x,t)
    allocate(x(n))
    s = min(size(t),n)
    x(:s) = t(:s)
    deallocate(t)
  End Subroutine ReallocateReal
  
End Program Main

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-14 15:51

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表