std::fill arrays

C

cpisz

I saw that using std::fill was the way to go for setting all elements
of an array to some value in one foul swoop. However when I tryed it I
am getting an error.

Can I only use this for vectors or can I use it for a regular array?

what I called:
std::fill(m_start_times[0], m_start_times[8], 0);

error:
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\xutility(1136): error C2100: illegal indirection

// TEMPLATE FUNCTION fill
template<class _FwdIt,
class _Ty> inline
void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
{ // copy _Val through [_First, _Last)
for (; _First != _Last; ++_First)
*_First = _Val;
}
 
V

Victor Bazarov

I saw that using std::fill was the way to go for setting all elements
of an array to some value in one foul swoop. However when I tryed it I
am getting an error.

Can I only use this for vectors or can I use it for a regular array?

what I called:
std::fill(m_start_times[0], m_start_times[8], 0);

The arguments to 'fill' have to comply with 'iterator' traits. Elements
of the array don't. You need to use pointers:

std::fill(m_start_times + 0, m_start_times + 8, 0);
error:
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\xutility(1136): error C2100: illegal indirection

// TEMPLATE FUNCTION fill
template<class _FwdIt,
class _Ty> inline
void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
{ // copy _Val through [_First, _Last)
for (; _First != _Last; ++_First)
*_First = _Val;
}

V
 
M

Mike Wahler

I saw that using std::fill was the way to go for setting all elements
of an array to some value in one foul swoop. However when I tryed it I
am getting an error.

Can I only use this for vectors or can I use it for a regular array?

what I called:
std::fill(m_start_times[0], m_start_times[8], 0);

std::fill(&m_start_times[0], &m_start_times[8], 0);

or

std::fill(m_start_times, m_start_times + 8, 0);

The second argument should be the address of the
'one-past-the-end' element; e.g. your example above
assumes an eight element array (indices 0 through
7).

-Mike
error:
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\xutility(1136): error C2100: illegal indirection

-Mike
 

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

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top