Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initializing an array from iterators

Reply
Thread Tools

initializing an array from iterators

 
 
Marc
Guest
Posts: n/a
 
      11-02-2010
Hello,

I want to initialize a std::array<Type,dim> from an iterator. I can do:
std::array<T,d> a={{iter[0],iter[1],...}};
and with index manipulations it even works if d is known at compile time
but not at code writing time.
If the iterator is not random access but only forward, I can replace
iter[i] with next(iter,i), but that gives a quadratic number of ++,
which is wrong. Besides, it should be possible to use a pure input
iterator.

While I am describing the situation, let me mention that I dont want to
do:
T t=*iter;
array a={{t,...}};
because then t is copied/moved to the array instead of being constructed
directly into it (this iterator does some computations and returns a
temporary ripe for copy elision).

The best solution I can think of is to use a tuple-like class of
iterators where the constructor from iter initializes the first element
with *iter and the rest with ++iter (recursively, assuming tuple is
implemented like pair<head,tail>). I can then get the array by
dereferencing them all. The drawbacks are that std::tuple doesn't
provide this interface (the closest is cat_tuple, but I don't think it
would do), and that I can never remember if this is a case where the
order of evaluation is imposed or not:
Tuple(Iter i):head(*i),tail(++i){}
(I believe it is, but if not I am not sure how to make it work for an
input iterator).
 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      11-02-2010
Marc wrote:

> The best solution I can think of is to use a tuple-like class of
> iterators where the constructor from iter initializes the first element
> with *iter and the rest with ++iter (recursively, assuming tuple is
> implemented like pair<head,tail>). I can then get the array by
> dereferencing them all. The drawbacks are that std::tuple doesn't
> provide this interface (the closest is cat_tuple, but I don't think it
> would do), and that I can never remember if this is a case where the
> order of evaluation is imposed or not:
> Tuple(Iter i):head(*i),tail(++i){}
> (I believe it is, but if not I am not sure how to make it work for an
> input iterator).


Hmm, I mixed two potential solutions in there, and I am getting
something that isn't a solution
What the text describes corresponds to removing '*' from the previous
paragraph. But then it only works for forward iterators. With *, it
shows how to create a Tuple<T...>, but I want an array instead because
tuple doesn't have a constant time operator[].
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing iterators to one past end() in for loops Nikos Chantziaras C++ 15 04-14-2011 06:45 PM
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Why no compiler-warning at invalid array-initializing ? carlos@gkpwdun.com Java 11 11-07-2005 08:34 PM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM
initializing an array newby1 Java 3 07-04-2004 04:05 AM



Advertisments