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:
// 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.
*(_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].
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:
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!
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.
C/C++ Programming
- Understanding std::transform_reduce in Modern C++
- Implement a Lock Acquire and Release in C++
- Detecting Compile-time vs Runtime in C++: if consteval vs std::is_constant_evaluated()
- C++ Forward References: The Key to Perfect Forwarding
- Understanding dynamic_cast in C++: Safe Downcasting Explained
- C vs C++: Understanding the restrict Keyword and its Role in Optimization
- C++ Lvalue, Rvalue and Rvalue References
- C++ assert vs static_assert
- Why auto_ptr is Deprecated in C++?
- C++ What is the consteval? How is it different to const and constexpr?
- Tutorial on C++ std::move (Transfer Ownership)
- const vs constexpr in C++
- Tutorial on C++ Ranges
- Tutorial on C++ Smart Pointers
- Tutorial on C++ Future, Async and Promise
- The Memory Manager in C/C++: Heap vs Stack
- The String Memory Comparision Function memcmp() in C/C++
- Modern C++ Language Features
- Comparisions of push_back() and emplace_back() in C++ std::vector
- C++ Coding Reference: is_sorted_until() and is_sorted()
- C++ Coding Reference: iota() Setting Incrementing Values to Arrays or Vectors
- C++ Coding Reference: next_permutation() and prev_permutation()
- C++ Coding Reference: count() and count_if()
- C++ Code Reference: std::accumulate() and Examples
- C++ Coding Reference: sort() and stable_sort()
- The Next Permutation Algorithm in C++ std::next_permutation()
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Javascript: From Promises to Async/Await
Next Post: How to Get List of IP addresses in BASH using hostname and tr commands?