Alec
Alec

Reputation: 4482

How to collect an iterator into an exiting container?

If I have a iterators, say a=1:10 and an array that I've already allocated (x = zeros(10)), how can I collect my array into the container without allocating a new array?

E.g. does something like collect!(x, a) exist?

Upvotes: 1

Views: 58

Answers (2)

DNF
DNF

Reputation: 12664

You can do

x .= (1:10)

.........

Upvotes: 2

jling
jling

Reputation: 2301

copyto!(x, 1:10)

Filling the void here to 30 characters

Upvotes: 2

Related Questions