C++ Coding Reference: Copy N values by using std::fill_n()

  • Time:2020-09-17 11:25:43
  • Class:Weblog
  • Read:270

We talked about std::fill() that we can use to copy over a single value to a range [First, Last). If you know the number of elements you want to copy into, you can use the std::fill_n() instead. In the header xutility, the std::fill_n() using generic template definition is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template<class _OutIt,
    class _Diff,
    class _Ty> inline
    _OutIt _Fill_n_unchecked2(_OutIt _Dest, _Diff _Count, const _Ty& _Val, true_type)
    {   // copy _Val _Count times through [_Dest, ...), memset optimization
    _CSTD memset(_Dest, static_cast<unsigned char>(_Val), static_cast<size_t>(_Count));
    return (_Dest + _Count);
    }
 
template<class _OutIt,
    class _Diff,
    class _Ty> inline
    _OutIt fill_n(_OutIt _Dest, _Diff _Count_raw, const _Ty& _Val)
    {   // copy _Val _Count times through [_Dest, ...)
    const _Algorithm_int_t<_Diff> _Count = _Count_raw;
    if (0 < _Count)
        {
        const auto _UDest = _Get_unwrapped_n(_Dest, _Count);
        _Seek_wrapped(_Dest,
            _Fill_n_unchecked2(_UDest, _Count, _Val, _Fill_memset_is_safe(_UDest, _Val)));
        }
 
    return (_Dest);
    }
template<class _OutIt,
	class _Diff,
	class _Ty> inline
	_OutIt _Fill_n_unchecked2(_OutIt _Dest, _Diff _Count, const _Ty& _Val, true_type)
	{	// copy _Val _Count times through [_Dest, ...), memset optimization
	_CSTD memset(_Dest, static_cast<unsigned char>(_Val), static_cast<size_t>(_Count));
	return (_Dest + _Count);
	}

template<class _OutIt,
	class _Diff,
	class _Ty> inline
	_OutIt fill_n(_OutIt _Dest, _Diff _Count_raw, const _Ty& _Val)
	{	// copy _Val _Count times through [_Dest, ...)
	const _Algorithm_int_t<_Diff> _Count = _Count_raw;
	if (0 < _Count)
		{
		const auto _UDest = _Get_unwrapped_n(_Dest, _Count);
		_Seek_wrapped(_Dest,
			_Fill_n_unchecked2(_UDest, _Count, _Val, _Fill_memset_is_safe(_UDest, _Val)));
		}

	return (_Dest);
	}

As we can see, it is based on memset.

Example Usages of std::fill_n()

The std::fill_n() takes three parameters: the destination, the count and the value to copy over, namely:

1
std::fill_n(destination, count, value);
std::fill_n(destination, count, value);

For example, using std::fill_n() to copy value of 3 into a static array (of integer) for the first 4 elements:

1
2
int nums[10];
std::fill_n(begin(nums), 4, 3); // nums is now [3, 3, 3, 3, .., .., ..]
int nums[10];
std::fill_n(begin(nums), 4, 3); // nums is now [3, 3, 3, 3, .., .., ..]

std::fill_n() takes generic types, thus, you can use it on array of chars:

1
2
char buf[6];
std::fill_n(begin(buf), 3, 'a'); // buf is ['a', 'a', 'a', .., ..]
char buf[6];
std::fill_n(begin(buf), 3, 'a'); // buf is ['a', 'a', 'a', .., ..]

You can also use std::fill_n() to copy over custom data type, for example, the following copies over 3 structs:

1
2
3
4
5
6
typedef struct {
    int x, y;
} data;
vector<data> arr(10);
data x = { 1, 2 };
std::fill_n(begin(arr), 3, x);
typedef struct {
	int x, y;
} data;
vector<data> arr(10);
data x = { 1, 2 };
std::fill_n(begin(arr), 3, x);

Similar to std::fill(), you would need to make sure the destination memory locations can be written to otherwise a runtime memory Access Violation could be expected.

–EOF (The Ultimate Computing & Technology Blog) —

Recommend:
3 Ways to Convert Blog Readers to Leads
7 Ways To Deal With AdBlock Users If You’re A Blogger
2020 Design Trends [Infographic]
8 Content Marketing Plugins You Need For Your WordPress Blog
Tips to Make Money from the Comfort of Your Own Home
5 Tips for Protecting Your Freelance Business from Liabilities
5 Easy Ways On How To Build An Authority Website
How to Protect Your WordPress Site From Hackers
Top 10 Relationship Blogs With the Best Pieces of Advice in 2020
How to Construct Binary Search Tree from Preorder Traversal in P
Share:Facebook Twitter
Comment list
Comment add