473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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 10792
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bartek d | last post by:
Hello, I have a class which is used to encapsulate a RenderMan Interface variable. Generally speaking, such variable may be of integral, float, string type, or an array of those. I thought I...
5
by: bartek d | last post by:
Hello, Regarding my previous question about a class which is used to store a variable type vector. I tried to be more elaborate on the code. I'd be grateful for your suggestions. Am I going in...
2
by: Morten Aune Lyrstad | last post by:
Hi! I haven't been using the standard template libraries much. I have a hard time trying to figure out just what is the practical difference between std::vector and std::list. Aren't both basically...
6
by: slyi | last post by:
Is it ok to assume that the following assertion is valid for all implementations of std::vector?: std::vector<T> v(10); T* p = &v; for (size_t n=0; n < v.size(); n++) assert( p+n == &v ); ...
2
by: Buck Brown | last post by:
Hi, I am using Visual C++ 6.0 MFC. I have been trying to build an array of objects. First I tried using CArray but it was just giving me fits. Once I got my copy contructor built so I would not...
3
by: mast2as | last post by:
sorry i am too sure how to write a more explicit subject but this code doesn't compile for the type string and I am not sure why (and I am not sure either how to describe the problem but by looking...
3
by: DevNull | last post by:
I have a program where we load a mapfile, comprised of a .csv with numbers which represent object types at a given position. Each position is in the map is called a Cell, a cell contains only a...
5
MrPickle
by: MrPickle | last post by:
I googled and got some answers but whenever I used it I kept getting and std::out_of_range exception and I'm not completely clear on how to use it. I know how to initialize it; std::vector<...
8
MrPickle
by: MrPickle | last post by:
Is this the correct way to convert a std::vector to a C array or is there a function that returns the vector as an array? std::vector<int> MyVector; //Fill MyVector int a; for(int i = 0; i <...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.