-
Notifications
You must be signed in to change notification settings - Fork 10
Example: Vector of integers
Tom Clune edited this page Feb 21, 2019
·
3 revisions
The following module will define derived types Vector
and VectorIterator
:
module IntegerVector_mod
#define _type integer
#include "templates/vector.inc"
end module IntegerVector_mod
Yes - it really is that easy.
If you want to export the types under custom names, you can also do this:
module IntegerVector_mod
#define _type integer
#define _vector IntegerVector
#define _iterator IntegerVectorIterator
#include "templates/vector.inc"
end module IntegerVector_mod
Here is a small driver program that exercises the module:
program IntegerVectorDriver
use IntegerVector_mod
type(IntegerVector), target :: primes
type(IntegerVectorIterator) :: iter
integer, pointer :: p
call primes%push_back(2) ! vector now has one element
call primes%push_back(3) ! vector now has two elements
call primes%push_back(5) ! vector now has three elements
print*,primes%size() ! prints "3"
print*,primes%at(3) ! prints "5"
p => primes%at(1) ! p is a pointer to the "2" integer stored inside the container
p = 0
print*,primes%at(1) ! prints "0"
! The following is a simple loop to print the square of all elements
! Prints 0, 9, 25 (on separate lines)
iter = primes%begin()
do while (iter /= primes%end())
p => iter%get()
print*,p**2
call iter%next()
end do
end program