I want to get the number of people whose name starts with 'T':
#include <iostream>
#include <string>
#include <range\v3\all.hpp>
using namespace ranges;
int main()
{
const auto names = std::vector<std::string> {"Tony", "Peter"};
std::cout << size(names | view::filter([](const auto& s) {return s[0] == 'T';}));
}
But I got huge compile error:
λ clang -std=c++14 test.cpp
test.cpp:11:18: error: no matching function for call to object of type 'const ranges::v3::adl_size_detail::size_fn'
std::cout << size(names | view::filter([](const auto& s) {return s[0] == 'T';}));
^~~~
K:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\range/v3/size.hpp:90:32: note: candidate template
ignored: substitution failure [with Rng =
ranges::v3::remove_if_view<ranges::v3::iterator_range<std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > > >,
std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > > > >, ranges::v3::logical_negate<(lambda at test.cpp:11:44)> >]: no matching function
for call to 'size'
constexpr auto operator()(Rng &&rng) const ->
^
K:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\range/v3/utility/iterator.hpp:405:32: note: candidate
function template not viable: requires 2 arguments, but 1 was provided
iterator_size_t<I> operator()(I begin, S end) const
By the way, I use the clang 3.7 in the Visual Studio 2015 Update 1. So, what's wrong?
The library used in the code examples is not really the C++20 ranges, it's the ranges-v3 open-source library from Eric Niebler, which is the basis of the proposal to add ranges to the C++. It's a header-only library compatible with C++11/14/17.
Range v3 is a generic library that augments the existing standard library with facilities for working with ranges. A range can be loosely thought of a pair of iterators, although they need not be implemented that way.
You're better off with the count_if
algorithm.
auto cnt = ranges::count_if( names, [](const auto& s) {return s[0] == 'T';} )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With