chsvlib
chsv helper source code
chsvmemex\generating_input_iterator.cpp

Demonstrates usage of the generating_input_iterator adapter.

#include <vector>
#include <functional>
#include <iterator>
using namespace Chusov::Memory;
std::vector<unsigned> fill_generate(std::size_t count)
{
std::vector<unsigned> v;
unsigned val = 0;
std::copy_n(generating_input_iterator<std::function<unsigned()>>([&val]() mutable {return ++val;}), count, std::back_inserter(v));
return v;
}
std::vector<unsigned> fill_generate_cpp17(std::size_t count)
{
std::vector<unsigned> v;
unsigned val = 10;
std::copy_n(generating_input_iterator([&val]() mutable {return val--;}), count, std::back_inserter(v)); //C++17 due to the deduced type of the lambda
return v;
}
#include <iostream>
int main(int argc, char** argv)
{
auto v = fill_generate(10);
for (const auto& elem:v)
std::cout << elem << ' ';
std::cout << '\n';
v = fill_generate_cpp17(10);
for (const auto& elem:v)
std::cout << elem << ' ';
std::cout << '\n';
return 0;
}
/*
Output:
>1 2 3 4 5 6 7 8 9 10
>10 9 8 7 6 5 4 3 2 1
>
*/
Advanced functionality and C++ objects for working with memory.
OutputIterator copy_n(InputIterator input, std::size_t size, OutputIterator output) noexcept(std::is_nothrow_copy_assignable< typename std::iterator_traits< OutputIterator >::value_type >::value)
Copy-assigns a given number of elements starting from one referred to by an input iterator to a place...
Definition: chsvmemex.h:2291
A namespace of functions and components used to work with memory.
Definition: chsvmem.h:71