Ok , so this is a short query.
I have a Set container say a ;
c=5 , d= 42 are integers.
I want to insert all integers from 5 to 42 in the set without using the for loop.
How can i do this??
Finally a should look like {5,6,7.......,42}
Ok , so this is a short query.
I have a Set container say a ;
c=5 , d= 42 are integers.
I want to insert all integers from 5 to 42 in the set without using the for loop.
How can i do this??
Finally a should look like {5,6,7.......,42}
Something like this:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
template<class OutputIterator, class T>
OutputIterator iota_rng(OutputIterator first, T low, T high)
{
return std::generate_n(first, high - low, [&, value = low]() mutable { return value++; });
}
int main()
{
std::set<int> s;
iota_rng(std::inserter(s, s.begin()), 5, 43);
for (auto elem : s) {
std::cout << elem << ", ";
}
}
In the range-v3 library (proposed as a technical specification) you can write it even more concisely as:
#include <range/v3/all.hpp>
#include <iostream>
#include <set>
int main()
{
std::set<int> s = ranges::view::iota(5, 43);
for (auto elem : s) {
std::cout << elem << ", ";
}
}