Last active
March 29, 2019 18:56
-
-
Save bwedding/73df100e07d317a01fc579efdb15866d to your computer and use it in GitHub Desktop.
C++ stepping iota template for ints and floats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use of lambda with std::accumulate to only add numbers that are n | |
#include <numeric> | |
#include <iterator> | |
#include <iostream> | |
#include <algorithm> | |
#include <iostream> | |
#include <list> | |
#include <numeric> | |
#include <random> | |
#include <vector> | |
#include <numeric> | |
template<typename ForwardIterator, typename T> | |
void step_iota(ForwardIterator first, ForwardIterator last, T value, T step) | |
{ | |
while (first != last) | |
*first++ = value, value += step; | |
} | |
int main() | |
{ | |
std::vector<float> vec(100); | |
step_iota(std::begin(vec), std::next(std::begin(vec), 100), 0.0f, 0.5f); | |
for (const auto& elem : vec) | |
{ | |
std::cout << elem << ' '; | |
} | |
std::vector<float> intvec(100); | |
step_iota(std::begin(intvec), std::next(std::begin(intvec), 100), 0, 5); | |
for (const auto& elem : intvec) | |
{ | |
std::cout << elem << ' '; | |
} | |
std::list<int> l(10); | |
std::iota(l.begin(), l.end(), -4); | |
std::vector<std::list<int>::iterator> v(l.size()); | |
std::iota(v.begin(), v.end(), l.begin()); | |
std::shuffle(v.begin(), v.end(), std::mt19937{ std::random_device{}() }); | |
std::cout << "\nContents of the list: "; | |
for (auto n : l) std::cout << n << ' '; | |
std::cout << '\n'; | |
std::cout << "\nContents of the list, shuffled: "; | |
for (auto i : v) std::cout << *i << ' '; | |
std::cout << '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment