473,230 Members | 1,982 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,230 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 10783
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.