By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,110 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,110 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.