468,733 Members | 2,304 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

std::vector, std::copy and array of int

Hi,
Consider the following piece of code:
int t[]={1,2,3,4,5,6};
vector<intv;
std::copy (t, t+sizeof(t)/sizeof(t[0]), std::back_inserter (v));

Could someone explain me why we can pass "t" as an argument to the copy
method though it is expecting an iterator as its firts argument like
written in the STL doc:

iterator copy( iterator start, iterator end, iterator dest );

Thanks,
JF
Apr 12 '07 #1
3 10524
On 12 Apr, 15:40, none <""jf\"@(none)"wrote:
Hi,
Consider the following piece of code:
int t[]={1,2,3,4,5,6};
vector<intv;
std::copy (t, t+sizeof(t)/sizeof(t[0]), std::back_inserter (v));

Could someone explain me why we can pass "t" as an argument to the copy
method though it is expecting an iterator as its firts argument like
written in the STL doc:

iterator copy( iterator start, iterator end, iterator dest );
Because a pointer full fills all the requirements we have on an
iterator in this case. Notice that iterator in this case is not a
class but rather a template parameters, so anything having the right
interface will do.

--
Erik Wikström

Apr 12 '07 #2
none wrote:
Hi,
Consider the following piece of code:
int t[]={1,2,3,4,5,6};
vector<intv;
std::copy (t, t+sizeof(t)/sizeof(t[0]), std::back_inserter (v));

Could someone explain me why we can pass "t" as an argument to the copy
method though it is expecting an iterator as its firts argument like
written in the STL doc:

iterator copy( iterator start, iterator end, iterator dest );
....

That's not the signature for std::copy

this is it:
template<typename _InputIterator, typename _OutputIterator>
inline _OutputIterator
copy(_InputIterator __first, _InputIterator __last,
_OutputIterator __result)

Pointers behave like iterators and so you can use an pointer in place of
any of the args in std::copy.

Same applies to std::vector::append and one of the std::vector
constructor overloads.
Apr 12 '07 #3
none wrote:
int t[]={1,2,3,4,5,6};
vector<intv;
std::copy (t, t+sizeof(t)/sizeof(t[0]), std::back_inserter (v));
Unrelated to your question, but in case you didn't know, there's a
shorter and more efficient way of doing that same thing:

int t[]={1,2,3,4,5,6};
vector<intv(t, t+sizeof(t)/sizeof(t[0]));

(This is more efficient because the vector can allocate the right
amount of memory right away instead of having to resize itself
repeatedly like when using the back inserter.)

In case you can't call the vector's constructor like that (because
you have to use a vector instance which has already been constructed
elsewhere, or you simply have to perform that operation repeatedly),
then use: v.assign(t, t+sizeof(t)/sizeof(t[0]));
Apr 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by bartek d | last post: by
2 posts views Thread by Morten Aune Lyrstad | last post: by
2 posts views Thread by Buck Brown | last post: by
MrPickle
5 posts views Thread by MrPickle | last post: by
MrPickle
8 posts views Thread by MrPickle | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.