470,862 Members | 1,720 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

std::uninitialized_fill

Simple newbie question:

I want to set an int array to value 5

int i[3];
int val=5;
std::uninitialized_fill(&i[0], &i[3], val);
OR
std::uninitialized_fill(i, i+3, val);

I need to pass as last iterator an out of index reference to completly fill
the array.
I think because std::uninitialized_fill do something like "while ( First !=
Last ) {...do something...}"
So my iterator will be incremented out of index? Is it dangerous?

Many thanks

Michele
Feb 23 '06 #1
5 2168
JE
Michele Moccia wrote:
Simple newbie question:

I want to set an int array to value 5

int i[3];
int val=5;


<snip>

std::fill( i, i + sizeof(i)/sizeof(*i), val );

Leave uninitialized_fill alone, unless you're writing allocators or
something...

Feb 24 '06 #2
Michele Moccia wrote:
I want to set an int array to value 5

int i[3];
int val=5;
std::uninitialized_fill(&i[0], &i[3], val);
OR
std::uninitialized_fill(i, i+3, val);
.... or:

template <typename T, int size>
T* begin(T(&array)[size]) { return array; }
template <typename T, int size>
T* end(T(&array)[size]) { return array + T; }

int main()
{
int i[3];
std::fill(begin(i), end(i), 5);
}
I need to pass as last iterator an out of index reference to completly
fill the array.


The pointer to the element one past the last array element is always
defined. Thus, it is safe to pass this pointer as an argument to a
function and, in fact, the STL algorithms take heavy advantage of
this features. Note, however, that you may not dereference this
pointer!
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 24 '06 #3
JE
Michele Moccia wrote:
Simple newbie question:

I want to set an int array to value 5

int i[3];
int val=5;


<snip>

std::fill( i, i + sizeof(i)/sizeof(*i), val );

Leave uninitialized_fill alone, unless you're writing allocators or
something...

Feb 24 '06 #4
Dietmar Kuehl wrote:
[redacted
template <typename T, int size>
T* end(T(&array)[size]) { return array + T; } I assume you mean { return array + size; }

Feb 24 '06 #5
red floyd wrote:
Dietmar Kuehl wrote:
[redacted
template <typename T, int size>
T* end(T(&array)[size]) { return array + T; }

I assume you mean { return array + size; }


Yes, of course: I just typed the code without testing it...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 24 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Lasse Skyum | last post: by
4 posts views Thread by Steven T. Hatton | last post: by
822 posts views Thread by Turamnvia Suouriviaskimatta | last post: by
4 posts views Thread by Hora | last post: by
4 posts views Thread by Urs Thuermann | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.