C++ Coding Reference: Filling a Range with a Value using std::fi

  • 时间:2020-09-17 11:25:43
  • 分类:网络文摘
  • 阅读:137 次

The C++ fill is provided in xutility and it allows us to copy a value into a range either vector or array. The std::fill() is defined using template:

1
2
3
4
5
6
7
template<class _FwdIt,
    class _Ty> inline
    void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
    {   // copy _Val through [_First, _Last)
    _Adl_verify_range(_First, _Last);
    _Fill_unchecked(_Get_unwrapped(_First), _Get_unwrapped(_Last), _Val);
    }
template<class _FwdIt,
	class _Ty> inline
	void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
	{	// copy _Val through [_First, _Last)
	_Adl_verify_range(_First, _Last);
	_Fill_unchecked(_Get_unwrapped(_First), _Get_unwrapped(_Last), _Val);
	}

One possible implementation could be:

1
2
3
4
5
6
7
8
9
10
11
12
template<class _FwdIt,
    class _Ty> inline
    void fill_me(_FwdIt _First, _FwdIt _Last, _Ty _Val)
{   // apply _Val into [_First, _Last)
    _Adl_verify_range(_First, _Last);
    auto _UFirst = _Get_unwrapped(_First);
    const auto _ULast = _Get_unwrapped(_Last);
    for (; _UFirst != _ULast; ++_UFirst)
    {
        *_UFirst = _Val;
    }
}
template<class _FwdIt,
	class _Ty> inline
	void fill_me(_FwdIt _First, _FwdIt _Last, _Ty _Val)
{	// apply _Val into [_First, _Last)
	_Adl_verify_range(_First, _Last);
	auto _UFirst = _Get_unwrapped(_First);
	const auto _ULast = _Get_unwrapped(_Last);
	for (; _UFirst != _ULast; ++_UFirst)
	{
		*_UFirst = _Val;
	}
}

Example usage:

1
2
3
4
5
6
7
8
9
10
11
// using std::fill on integers
int nums[5];
std::fill(begin(nums), end(nums), 2); // nums is now [2, 2, 2, 2, 2]
 
// using std::fill on doubles
vector<double>vals(5);
std::fill(begin(vals), end(vals), 1.5); // vals is now [1.5, 1.5, 1.5, 1.5, 1.5]
 
// using std::fill on characters
char buf[6];
fill(begin(buf), end(buf), 'a'); // buf is now ['a', 'a', 'a', 'a', 'a', 'a']
// using std::fill on integers
int nums[5];
std::fill(begin(nums), end(nums), 2); // nums is now [2, 2, 2, 2, 2]

// using std::fill on doubles
vector<double>vals(5);
std::fill(begin(vals), end(vals), 1.5); // vals is now [1.5, 1.5, 1.5, 1.5, 1.5]

// using std::fill on characters
char buf[6];
fill(begin(buf), end(buf), 'a'); // buf is now ['a', 'a', 'a', 'a', 'a', 'a']

To compute the increasing values into range [first, last), we can use the std::iota() instead.

You can use std::fill() to copy over custom data types such as structs. For example,

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

To copy over a specific number of elements, you could use std::fill_n() instead.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
A Compilation of the Best Content Creation Strategies from 43 Ex  Why Writing Internships Are an Underrated Way to Become a Great   Compute Number of Lines To Write String (Wordwrap)  How to Reverse Words in a String?  Design a Logger Rate Limiter in C++/Java  C++ Coding Exercise: Smallest Range of Array  Coding For Profit: 4 Types Of Websites To Consider Building  How to Compute the Projection Area of 3D Shapes?  How To Find The Ideal Monitor For Coders?  How to Partition an Array Into Three Parts With Equal Sum? 
评论列表
添加评论