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

N

none

Hi,
Consider the following piece of code:
int t[]={1,2,3,4,5,6};
vector<int> v;
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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi,
Consider the following piece of code:
int t[]={1,2,3,4,5,6};
vector<int> v;
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.
 
G

Gianni Mariani

none said:
Hi,
Consider the following piece of code:
int t[]={1,2,3,4,5,6};
vector<int> v;
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.
 
J

Juha Nieminen

none said:
int t[]={1,2,3,4,5,6};
vector<int> v;
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<int> v(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]));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top