C++ Coding Reference: iota() – Setting Incrementing Values
- 时间:2020-09-17 11:25:43
- 分类:网络文摘
- 阅读:124 次
The C++ method iota (not to be confused with itoa which is to convert integers to string), is defined in header numeric. Its purpose is to compute increasing values into a range of values such as vector or arrays.
Syntactically, it has the following function signature with generic template in numeric:
1 2 3 4 5 6 7 8 9 10 11 12 13 | // FUNCTION TEMPLATE iota template<class _FwdIt, class _Ty> inline void iota(_FwdIt _First, _FwdIt _Last, _Ty _Val) { // compute increasing sequence into [_First, _Last) _Adl_verify_range(_First, _Last); auto _UFirst = _Get_unwrapped(_First); const auto _ULast = _Get_unwrapped(_Last); for (; _UFirst != _ULast; ++_UFirst, (void)++_Val) { *_UFirst = _Val; } } |
// FUNCTION TEMPLATE iota
template<class _FwdIt,
class _Ty> inline
void iota(_FwdIt _First, _FwdIt _Last, _Ty _Val)
{ // compute increasing sequence into [_First, _Last)
_Adl_verify_range(_First, _Last);
auto _UFirst = _Get_unwrapped(_First);
const auto _ULast = _Get_unwrapped(_Last);
for (; _UFirst != _ULast; ++_UFirst, (void)++_Val)
{
*_UFirst = _Val;
}
}As we can see, the iota takes three parameters: The First, the Last, and the Value. The iota will then set the values within the range [First, Last) with the values incrementing from Value.
1 2 3 4 5 6 7 | *(_first + 0) = _Val; *(_first + 1) = ++_Val; *(_first + 2) = ++_Val; *(_first + 3) = ++_Val; ... ... *(_last - 1) = ++_Val; |
*(_first + 0) = _Val; *(_first + 1) = ++_Val; *(_first + 2) = ++_Val; *(_first + 3) = ++_Val; ... ... *(_last - 1) = ++_Val;
The Last iterator is always one element (position) beyond the actual last element of the range (vector or array).
Using iota on Vectors
The following C++ example initialize a vector of type integer with 10 elements (all zeros). After iota with starting value -5, the vector becomes [-5 -4 -3 -2 -1 0 1 2 3 4].
1 2 | vector<int> nums(10); iota(begin(nums), end(nums), -5); // [-5 -4 -3 -2 -1 0 1 2 3 4] |
vector<int> nums(10); iota(begin(nums), end(nums), -5); // [-5 -4 -3 -2 -1 0 1 2 3 4]
As the begin() and end() can be used on the arrays, we can apply the increasing sequence on the arrays as well:
1 2 | int nums[10]; iota(begin(nums), end(nums), -5); // [-5 -4 -3 -2 -1 0 1 2 3 4] |
int nums[10]; iota(begin(nums), end(nums), -5); // [-5 -4 -3 -2 -1 0 1 2 3 4]
Not just integers, doubles/floats are also welcome!
1 2 3 | vector<double> nums(10); // [-0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5] iota(begin(nums), end(nums), -0.5); |
vector<double> nums(10); // [-0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5] iota(begin(nums), end(nums), -0.5);
If you want to apply single values to a range of either vector or arrays, you may want to use the std::fill() instead.
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:红枣美味营养可养生但是禁忌亦不少 不同包装的牛奶,你知道该如何选择吗 冬天吃羊肉如何去掉羊膻味及饮食禁忌 适度喝啤酒预防骨质疏松保持关节弹性 关于鸡蛋营养及其食用方法的十大误区 腊肉的风味和特点及腊肉的制作全过程 腊肉的营养价值及腊肉的食用禁忌 煲汤的诀窍及胡萝卜熟吃煮汤更营养 胡萝卜不但可以保护视力还对精子有益 节令食品年糕的营养价值与食用禁忌
- 评论列表
-
- 添加评论