Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::find() with easier interface

42 views
Skip to first unread message

Ralf Goertz

unread,
May 4, 2016, 5:53:07 AM5/4/16
to
Hi,

I find it quite tedious to always have to write

auto it=std::find(v.begin(),v.end(),value);

where "v" is a suitable container of elements of the same type as
"value". I almost never want so search in a proper subrange. So now
that we have stuff like

for (auto i:v) …

to indicate that we want "i" to iterate over all elements of "v" I came
to wonder why there is no

auto it=std::find(v,value); [1]

or at least

auto it=std::find(v.begin(),value); [2]

Looking at the substr() and erase() member function of string it seems
there are function prototypes which help avoid unnecessary typing. I
know that variant [2] would be difficult given that it would be hard to
know what v.end() is from within the function. But why not provide [1]
in a way similar to this?:

#include <algorithm>
#include <vector>


template <class C> typename C::iterator find (const C &c, const typename C::value_type & val) {
return std::find(c.begin(),c.end(),val);
}


int main() {
std::vector<int> v({47,11});
auto it=find(v,47);
}

However, this gives an error:

find_test.cc: In instantiation of ‘typename C::iterator find(const C&, const typename C::value_type&) [with C = std::vec
tor<int>; typename C::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename C::value_type = int]’:
find_test.cc:12:22: required from here
find_test.cc:6:43: error: could not convert ‘std::find<__gnu_cxx::__normal_iterator<const int*, std::vector<int> >, int>
((& c)->std::vector<_Tp, _Alloc>::begin<int, std::allocator<int> >(), (& c)->std::vector<_Tp, _Alloc>::end<int, std::all
ocator<int> >(), (* & val))’ from ‘__gnu_cxx::__normal_iterator<const int*, std::vector<int> >’ to ‘std::vector<int>::it
erator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}’
return std::find(c.begin(),c.end(),val);
^
Omitting the "const" in front of "C &c" makes it compile. But then I
can't search in const vectors. Is there a way that works?

Ralf

Ralf Goertz

unread,
May 4, 2016, 7:00:55 AM5/4/16
to
Am 4 May 2016 10:21:21 GMT
schrieb r...@zedat.fu-berlin.de (Stefan Ram):

> Ralf Goertz <m...@myprovider.invalid> writes:
> >#include <algorithm>
> >#include <vector>
> >template <class C> typename C::iterator find (const C &c, const
> >typename C::value_type & val) {
> > return std::find(c.begin(),c.end(),val);
> >}
> >int main() {
> > std::vector<int> v({47,11});
> > auto it=find(v,47);
> >}
>
> I did not bother to really understand the error message,
> and I don't know whether it's the following that you want,
> but this compiles here (-std=c++17):

<snip>

> #include <utility>
>
> template< typename T >
> typename T::iterator find
> ( T & c,
> typename T::value_type const & val )
> { return ::std::find( begin( c ), end( c ), val ); }

Okay, thanks. This does the trick even with "const T & c". But why don't
we need namespace qualifier std:: in front of "begin" and "end"?
According to http://www.cplusplus.com/reference/iterator/begin/ that's
where these functions live. And what do you need <utility> for. Here it
works without it (std=c++11)

Richard

unread,
May 4, 2016, 12:26:21 PM5/4/16
to
[Please do not mail me a copy of your followup]

Ralf Goertz <m...@myprovider.invalid> spake the secret code
<20160504115...@delli.home.local> thusly:

>I find it quite tedious to always have to write
>
>auto it=std::find(v.begin(),v.end(),value);

It's coming.

<http://ericniebler.com/2014/10/11/n4128-ranges-for-the-standard-library/>
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Ralf Goertz

unread,
May 6, 2016, 4:13:38 AM5/6/16
to
Am Wed, 4 May 2016 16:26:09 +0000 (UTC)
schrieb legaliz...@mail.xmission.com (Richard):

> [Please do not mail me a copy of your followup]
>
> Ralf Goertz <m...@myprovider.invalid> spake the secret code
> <20160504115...@delli.home.local> thusly:
>
> >I find it quite tedious to always have to write
> >
> >auto it=std::find(v.begin(),v.end(),value);
>
> It's coming.
>
> <http://ericniebler.com/2014/10/11/n4128-ranges-for-the-standard-library/>

That's exactly what I was hoping for, thanks.

Richard

unread,
May 6, 2016, 12:14:23 PM5/6/16
to
[Please do not mail me a copy of your followup]

Ralf Goertz <m...@myprovider.invalid> spake the secret code
<20160506101...@delli.home.local> thusly:
Depending on your compiler you can use the library today:
<https://github.com/ericniebler/range-v3>

It needs more SFINAE support than VS 2015 Update 2 is currently
offering, but I'm sure MS is working on improving that.
0 new messages