本文介绍了派生类型的数组:选择条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在我的code我有一个二维数组

Currently in my code I have a 2D array

integer, allocatable :: elements(:,:)

和定义一些常量

integer, parameter :: TYP = 1
integer, parameter :: WIDTH = 2
integer, parameter :: HEIGHT = 3
! ...
integer, parameter :: NUM_ENTRIES = 10

和分配类似

allocate(elements(NUM_ENTRIES,10000))

所以我可以访问元素,比如

so I can access elements like

write(*,*) elements(WIDTH,100) ! gives the width of the 100th element

现在我想不仅整数但类型每个元素的混合物。
所以我定义了一个派生类型

Now I would like to have not only integer but a mixture of types for every element.So I define a derived type

type Element
    logical active
    integer type
    real width
    ! etc
end type

和使用元件的阵列

type(Element), allocatable :: elements(:)

随着二维数组的版本,我可以调用一个子程序,告诉它使用哪个条目。
例如。

With the 2d array version I could call a subroutine telling it which entry to use.E.g.

subroutine find_average(entry, avg)
    integer, intent(in) :: entry
    real, intent(out) :: avg
    integer i,
    real s

    s = 0
    do i = lbound(elements,1), ubound(elements,1)
        if (elements(TYP,i) .gt. 0) s = s + elements(entry,i)
    end do
    avg = s/(ubound(elements,1)-lbound(elements,1))
end subroutine

所以我可以通话find_average(高度)来找到的平均身高或通过宽度来获得平均宽度。
(和我做的子程序比找到的平均高度或宽度更先进的东西,这只是一个例子。)

So I could call find_average(HEIGHT) to find the average height or pass WIDTH to get the average width.(And my subroutines do more advanced things than finding the average height or width, this is just an example.)

问:我如何可以使用不同类型(与派生类型),而且还重用我的功能,不同的项目工作(如示例子程序)

Question: How can I use different types (as with the derived type) but also reuse my functions to work with different entries (as in the example subroutine)?

推荐答案

有关数组的情况下,而不是传递参数数组和索引我,你可以传递一个参数数组(I)相同。当您切换到具有派生类型,同样可以传递variable_of_type%的元素,而不是通过在整个variable_of_type并以某种方式指示其指明子元素它应该去努力的过程。如果code需要为不同类型的元素的不同(例如,逻辑,整数,实数),那么你可以写为每个特定的程序,而是通过一个通用的接口块有一个共同的名字,然后调用。编译器能够通过的参数的一些特性来区分通用接口块的操作,在这里它们的类型。对于其中的显着特点是阵列等级看到一个code例如

For the array case, instead of passing in arguments array and index i, you could pass in the single argument array (i). When you switch to having the derived type, similarly you could pass in variable_of_type % element rather than passing in the entire variable_of_type and somehow instructing the procedure which subelement it is supposed to work on. If the code needs to be different for the different types of elements (e.g., logical, integer, real), then you could write specific procedures for each, but call then with a common name via a generic interface block. The compiler has to be able to distinguish the procedures of the generic interface block by some characteristic of the arguments, here their type. For a code example in which the distinguishing characteristic is array rank see how to write wrapper for 'allocate'

编辑:例如code。这是否你想要做什么?

example code. does this do what you want?

module my_subs

   implicit none

   interface my_sum
      module procedure sum_real, sum_int
   end interface my_sum

contains

subroutine sum_real (array, tot)
   real, dimension(:), intent (in) :: array
   real, intent (out) :: tot
   integer :: i

   tot = 1.0
   do i=1, size (array)
      tot = tot * array (i)
   end do
end subroutine sum_real

subroutine sum_int (array, tot)
   integer, dimension(:), intent (in) :: array
   integer, intent (out) :: tot
   integer :: i

   tot = 0
   do i=1, size (array)
      tot = tot + array (i)
   end do
end subroutine sum_int

end module my_subs


program test_dt

use my_subs

implicit none

type my_type
   integer weight
   real length
end type my_type

type (my_type), dimension (:), allocatable :: people
type (my_type) :: answer

allocate (people (2))

people (1) % weight = 1
people (1) % length = 1.0
people (2) % weight = 2
people (2) % length = 2.0

call my_sum ( people (:) % weight, answer % weight )
write (*, *)  answer % weight

call my_sum ( people (:) % length, answer % length )
write (*, *)  answer % length

end program test_dt

这篇关于派生类型的数组:选择条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:17