I want to loop through a vector in a sorted way without modifying the underlying vector.
Can std::views and/or std::range be used for this purpose?
I've successfully implemented filtering using views, but I don't know if it is possible to sort using a predicate.
You can find an example to complete here : https://godbolt.org/z/cKer8frvq
#include <iostream>
#include <ranges>
#include <vector>
#include <chrono>
struct Data{
int a;
};
int main() {
std::vector<Data> vec = {{1}, {2}, {3}, {10}, {5}, {6}};
auto sortedView = // <= can we use std::views here ?
for (const auto &sortedData: sortedView) std::cout << std::to_string(sortedData.a) << std::endl; // 1 2 3 5 6 10
for (const auto &data: vec) std::cout << std::to_string(data.a) << std::endl; // 1 2 3 10 5 6
}
You have to modify something to use std::ranges::sort (or std::sort), but it doesn't have to be your actual data.
#include <iostream>
#include <ranges>
#include <numeric>
#include <algorithm>
#include <vector>
#include <chrono>
struct Data{
int a;
friend auto operator<=> (const Data &, const Data &) = default;
};
int main() {
std::vector<Data> vec = {{1}, {2}, {3}, {10}, {5}, {6}};
std::vector<std::size_t> indexes(vec.size());
std::iota(indexes.begin(), indexes.end(), std::size_t{ 0 }); // 0z in C++23
auto proj = [&vec](std::size_t i) -> Data & { return vec[i]; };
std::ranges::sort(indexes, std::less<>{}, proj);
auto sortedView = std::ranges::views::transform(indexes, proj);
for (const auto &sortedData: sortedView) std::cout << sortedData.a << std::endl; // 1 2 3 5 6 10
for (const auto &data: vec) std::cout << data.a << std::endl; // 1 2 3 10 5 6
}
Lately I was using boost-range to create ranges over elements satisfying certain criteria. In all cases I'm using the same kind of filtered range all the time, so that I tried to encapsulate this behaviour in an external function.
This was the point where my problems started. Consider the following example.
#include <boost/range/adaptor/filtered.hpp>
#include <iostream>
#include <vector>
auto myFilter = [](const std::vector<int>& v, int r) {
return v | boost::adaptors::filtered([&r](auto v) { return v%r == 0; });
};
int main(int argc, const char* argv[])
{
using namespace boost::adaptors;
std::vector<int> input{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (auto& element : input | filtered([](auto v) {return v % 2 == 0; } ))
{
std::cout << "Element = " << element << std::endl;
}
std::cout << std::endl;
for (auto& element : myFilter(input,4))
{
std::cout << "Element = " << element << std::endl;
}
return 0;
}
The first for-loop behaves as expected printing 4 and 8. The second for-loop however prints just 4. Why is that?
My second idea was to implement a class having a begin() and end() function. This should be a thin wrapper around a range object.
This was the solution, after fiddling out the type of the range iterator.
struct MyFilter {
MyFilter(const std::vector<int>& c, int r) : c(c), r(r), f([&r](auto v) { return v%r == 0; }) {
}
boost::range_detail::filtered_range<std::function<bool(int)>, std::vector<int>>::iterator begin() {
return rng.begin();
}
boost::range_detail::filtered_range<std::function<bool(int)>, std::vector<int>>::iterator end() {
return rng.end();
}
std::vector<int> c;
int r;
std::function<bool(int)> f;
boost::range_detail::filtered_range < std::function<bool(int)>, std::vector<int>> rng=c | boost::adaptors::filtered(f);
};
Usage should be something like:
for (auto& element : MyFilter(input, 4)) {
std::cout << "Element = " << element << std::endl;
}
Unfortunately, it prints again just the 4. Whichs is quite strange to me??
Now, I got the solution by myself. I have to remove the "&" in my lambda function to make it work!
In:
auto myFilter = [](const std::vector<int>& v, int r) {
return v | boost::adaptors::filtered([&r](auto v) { return v%r == 0; });
};
It returns another range adaptor while r captured by reference becomes a dangling reference. To fix it capture r by value:
auto myFilter = [](const std::vector<int>& v, int r) {
return v | boost::adaptors::filtered([r](auto v) { return v%r == 0; });
}; ^
+--- capture by value
Can someone point me, please, if where is some algorithms within STL to compute difference and intersection per one call in manner of unix comm utility?
int main()
{
//For example we have two sets on input
std::set<int>a = { 1 2 3 4 5 };
std::set<int>b = { 3 4 5 6 7 };
std::call_some_func(a, b, ... );
//So as result we need obtain 3 sets
//x1 = {1, 2} // present in a, but absent in b (difference)
//x2 = {3, 4, 5} // present on both sets (intersection)
//x3 = {6, 7} // present in b, but absent in a
}
My current implementation uses 2 calls of 'std::set_difference' and one call of 'std::set_intersection'.
I think this is probably a reasonably efficient implementation:
Features:
a) operates in linear time.
b) works with all ordered container types for input and all iterator types for output.
c) only requires operator< to be defined on the contained type, as per stl algorithms on sorted ranges.
template<class I1, class I2, class I3, class I4, class ITarget1, class ITarget2, class ITarget3>
auto comm(I1 lfirst, I2 llast, I3 rfirst, I4 rlast, ITarget1 lonly, ITarget2 both, ITarget3 ronly)
{
while (lfirst != llast and rfirst != rlast)
{
auto&& l = *lfirst;
auto&& r = *rfirst;
if (l < r) *lonly++ = *lfirst++;
else if (r < l) *ronly++ = *rfirst++;
else *both++ = (++lfirst, *rfirst++);
}
while (lfirst != llast)
*lonly++ = *lfirst++;
while (rfirst != rlast)
*ronly++ = *rfirst++;
}
example:
#include <tuple>
#include <set>
#include <vector>
#include <unordered_set>
#include <iterator>
#include <iostream>
/// #pre l and r are ordered
template<class I1, class I2, class I3, class I4, class ITarget1, class ITarget2, class ITarget3>
auto comm(I1 lfirst, I2 llast, I3 rfirst, I4 rlast, ITarget1 lonly, ITarget2 both, ITarget3 ronly)
{
while (lfirst != llast and rfirst != rlast)
{
auto&& l = *lfirst;
auto&& r = *rfirst;
if (l < r) *lonly++ = *lfirst++;
else if (r < l) *ronly++ = *rfirst++;
else *both++ = (++lfirst, *rfirst++);
}
while (lfirst != llast)
*lonly++ = *lfirst++;
while (rfirst != rlast)
*ronly++ = *rfirst++;
}
int main()
{
//For example we have two sets on input
std::set<int>a = { 1, 2, 3, 4, 5 };
std::set<int>b = { 3, 4, 5, 6, 7 };
std::vector<int> left;
std::set<int> right;
std::unordered_set<int> both;
comm(begin(a), end(a),
begin(b), end(b),
back_inserter(left),
inserter(both, both.end()),
inserter(right, right.end()));
//So as result we need obtain 3 sets
//x1 = {1, 2} // present in a, but absent in b (difference)
//x2 = {3, 4, 5} // present on both sets (intersection)
//x3 = {6, 7} // present in b, but absent in a
std::copy(begin(left), end(left), std::ostream_iterator<int>(std::cout, ", "));
std::cout << std::endl;
std::copy(begin(both), end(both), std::ostream_iterator<int>(std::cout, ", "));
std::cout << std::endl;
std::copy(begin(right), end(right), std::ostream_iterator<int>(std::cout, ", "));
std::cout << std::endl;
}
example output (note that the 'both' target is an unordered set):
1, 2,
5, 3, 4,
6, 7,
There is no single function to do that, you'd have to call the three functions you mentioned, or write something yourself. That being said, here's my attempt, though I'm not sure it's going to be any faster than the three step method you've already described
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
template <typename T>
void partition_sets(std::set<T> const& a,
std::set<T> const& b,
std::set<T>& difference_a,
std::set<T>& difference_b,
std::set<T>& intersection)
{
std::set_intersection(begin(a), end(a),
begin(b), end(b),
std::inserter(intersection, intersection.begin()));
std::copy_if(begin(a), end(a), std::inserter(difference_a, difference_a.begin()), [&intersection](int i)
{
return intersection.find(i) == intersection.end();
});
std::copy_if(begin(b), end(b), std::inserter(difference_b, difference_b.begin()), [&intersection](int i)
{
return intersection.find(i) == intersection.end();
});
}
Running your example
int main()
{
//For example we have two sets on input
std::set<int> a = { 1, 2, 3, 4, 5 };
std::set<int> b = { 3, 4, 5, 6, 7 };
std::set<int> x1;
std::set<int> x2;
std::set<int> x3;
partition_sets(a, b, x1, x2, x3);
std::cout << "a - b\n\t";
for (int i : x1)
{
std::cout << i << " ";
}
std::cout << "\n";
std::cout << "b - a\n\t";
for (int i : x2)
{
std::cout << i << " ";
}
std::cout << "\n";
std::cout << "intersection\n\t";
for (int i : x3)
{
std::cout << i << " ";
}
}
produces the output
a - b
1 2
b - a
6 7
intersection
3 4 5
Just write a wrapper for the three calls of the algorithms.
For example
#include <iostream>
#include<tuple>
#include <set>
#include <iterator>
#include <algorithm>
template <class T>
auto comm(const std::set<T> &first, const std::set<T> &second)
{
std::tuple<std::set<T>, std::set<T>, std::set<T>> t;
std::set_difference(first.begin(), first.end(),
second.begin(), second.end(),
std::inserter(std::get<0>(t), std::get<0>(t).begin()));
std::set_intersection(first.begin(), first.end(),
second.begin(), second.end(),
std::inserter(std::get<1>(t), std::get<1>(t).begin()));
std::set_difference(second.begin(), second.end(),
first.begin(), first.end(),
std::inserter(std::get<2>(t), std::get<2>(t).begin()));
return t;
}
int main()
{
std::set<int> a = { 1, 2, 3, 4, 5 };
std::set<int> b = { 3, 4, 5, 6, 7 };
auto t = comm(a, b);
for (auto x : std::get<0>(t)) std::cout << x << ' ';
std::cout << std::endl;
for (auto x : std::get<1>(t)) std::cout << x << ' ';
std::cout << std::endl;
for (auto x : std::get<2>(t)) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
The program output is
1 2
3 4 5
6 7
How can you displace the location you iterate through a vector? I've tried something like:
for(auto x : vect+2)
but this doesn't work. I'm sure there's a simple resolve, but I haven't been able to find anything online.
If you want to use the range-based for, you could use Boost.Range to create a range that starts from the third element of your vector (begin() + 2):
for (auto x : boost::make_iterator_range(begin(v) + 2, end(v)))
{
std::cout << x << " ";
}
Here is a simple example:
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/range.hpp>
#include <boost/range/adaptors.hpp>
int main()
{
std::vector<int> v(10);
iota(begin(v), end(v), 1);
for (auto x : boost::make_iterator_range(begin(v) + 2, end(v)))
{
std::cout << x << " ";
}
}
If you want to loop through every second element, instead, you could change your range as follows:
namespace rng = boost::adaptors;
for (auto x : v | rng::strided(2))
{
std::cout << x << " ";
}
Which in a full program would be:
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/range.hpp>
#include <boost/range/adaptors.hpp>
int main()
{
namespace rng = boost::adaptors;
std::vector<int> v(10);
iota(begin(v), end(v), 1);
for (auto x : v | rng::strided(2))
{
std::cout << x << " ";
}
}
Boost.Range is pretty flexible, so you can for instance combine the two adapters above:
for (auto x : boost::make_iterator_range(begin(v) + 2, end(v)) |
rng::strided(3))
{
std::cout << x << " ";
}
If you do not want or cannot use Boost, you could use a classical for loop with iterators:
for (auto i = begin(v) + 2; i != end(v); ++i)
{
std::cout << *i << " ";
}
This is how the whole program would look like:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v(10);
iota(begin(v), end(v), 1);
for (auto i = begin(v) + 2; i != end(v); ++i)
{
std::cout << *i << " ";
}
}
This can be achieved very simply, and has a number of solutions to suit any programming style.
The Classical Approach
int main()
{
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 1);
for (auto i = v.begin() + 2; i != v.end(); ++i)
{
std::cout << *i << " ";
}
}
The Functional Approach
int main()
{
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 1);
std::for_each(v.begin() + 2, v.end(), [](int val)
{
std::cout << val << " ";
}
);
}
You could add a light wrapper to make it work with range-based for:
#include <iostream>
#include <vector>
#include <iterator>
namespace range
{
template <typename C>
struct make_range
{
C t;
make_range(C t, int offset)
: t(std::begin(t) + offset, std::end(t))
{}
auto begin() -> decltype(t.begin())
{
return t.begin();
}
auto end() -> decltype(t.end())
{
return t.end();
}
};
}
int main()
{
std::vector<int> v{1, 2, 3, 4, 5};
for (auto i : range::make_range<decltype(v)>(v, 2))
{
std::cout << i << std::endl;
}
}
Perhaps it would be better to just store the iterators:
namespace range
{
template <typename C>
struct make_range
{
typename C::iterator beg_iter;
typename C::iterator end_iter;
make_range(C& t, int offset)
: beg_iter(std::begin(t) + offset), end_iter(std::end(t))
{}
auto begin() -> decltype(beg_iter)
{
return beg_iter;
}
auto end() -> decltype(end_iter)
{
return end_iter;
}
};
}
Is it possible/achievable to negate a boost filtered adaptor, e.g.
std::vector<int> v = {1, 2, 3, 4, 5};
for(auto i : v | !filtered(is_even))
std::cout << i << std::endl; // prints 1,3,5
instead of doing the negation inside the lambda expression?
Motivation: I work a lot with filtered and lambda functions, however when I use a filter more than once I usually refactor it into a custom filter, e.g.
for(auto i : v | even) // note: my filters are more complex than even.
std::cout << i << std::endl; // prints 2,4
Right now when I need the negation I am building a custom filter for them, e.g.
for(auto i : v | not_even)
std::cout << i << std::endl; // prints 1,2,3
but I would find it nicer to just be able to negate a filter, e.g.
for(auto i : v | !even)
std::cout << i << std::endl; // prints 1,2,3
Here's what I came up with on short notice:
#include <boost/range/adaptors.hpp>
#include <boost/functional.hpp>
#include <iostream>
namespace boost {
namespace range_detail {
template <typename T>
auto operator!(filter_holder<T> const& f) -> decltype(adaptors::filtered(boost::not1(f.val)))
{
return adaptors::filtered(boost::not1(f.val));
}
}
}
int main()
{
using namespace boost::adaptors;
int const v[] = { 1, 2, 3, 4 };
std::function<bool(int)> ll = [](int i){return 0 == (i%2);}; // WORKS
// bool(*ll)(int) = [](int i){return 0 == (i%2);}; // WORKS
// auto ll = [](int i){return 0 == (i%2);}; // not yet
auto even = filtered(ll);
for (auto i : v | !even)
{
std::cout << i << '\n';
}
}
See it live on liveworkspace.org
Note that it currently handles predicates of the form function pointer and std::function<...>, but not naked lambdas yet (on GCC 4.7.2)
This doesn't excactly answer the question, because it doesn't negate the filter, but only the predicate. I'm still posting this, because searching for the solution brought this question up as the first result.
Compared to the other answer, this has the advantage that we don't need to add custom code to namespace boost::range_detail.
C++17 Solution
The function std::not_fn can be used to create a negated predicate.
#include <boost/range/adaptors.hpp>
#include <functional>
#include <iostream>
struct is_even
{
bool operator()( int x ) const { return x % 2 == 0; }
};
int main()
{
using namespace boost::adaptors;
int const v[] = { 1, 2, 3, 4 };
for( auto i : v | filtered( std::not_fn( is_even{} ) ) )
{
std::cout << i << ' ';
}
}
Live Demo
C++11, C++14 Solution
The function std::not1 can be used to create a negated predicate. It has the additional requirement, that the predicate must define a member type, argument_type which has the same type as the predicates operator() parameter.
#include <boost/range/adaptors.hpp>
#include <functional>
#include <iostream>
struct is_even
{
using argument_type = int;
bool operator()( int x ) const { return x % 2 == 0; }
};
int main()
{
using namespace boost::adaptors;
int const v[] = { 1, 2, 3, 4 };
for( auto i : v | filtered( std::not1( is_even{} ) ) )
{
std::cout << i << ' ';
}
}
Live Demo