1

This is what I have now:

template<template<typename T> class container, typename T>
inline bool contains( const container<T> &cont, const T &element )
{
    if( cont.size() == 0 )
        return false;

    return( std::find(cont.begin(), cont.end(), element) != cont.end() );
}

An I'd like to call it like so:

std::vector<string> stringvector;
contains( stringvector, "somestring" );

I believe this should be possible, but everything I've tried throws up a different error. Any help is appreciated. Thanks!

UPDATE: Thanks for all the answers already, I was looking too far, but I'm still having problems:

template<class container_type, typename T>
inline bool contains(const container_type& c, const T& e)
{
    return !(c.size() && std::find(c.begin(),c.end(),e)!=c.end());
}

int main(int argc, char *argv[])
{
    vector<string> stringvector;
    stringvector.push_back("hello");
    cout << contains( stringvector, string("hello") );
    return 0;
}

fails to compile, even without the explicit `string´ constructor:

error: no matching function for call to 'find(std::vector<std::basic_string<char> >::const_iterator, std::vector<std::basic_string<char> >::const_iterator, const std::basic_string<char>&)'
rubenvb
  • 74,642
  • 33
  • 187
  • 332

4 Answers4

5

STL containers take two arguments, one for the contained type and another for the allocator that describes how to obtain memory.

Try this:

template<template<typename T, typename Alloc> class container, typename T, typename Alloc>
inline bool contains( const container<T, Alloc > &cont, const T &element )

However, this is really a good example of why you should avoid parameterizing algorithms on containers at all. It's best to follow the pattern of <algorithm> and only specify iterator type parameters.

template< typename Iter, typename T >
inline bool contains( Iter first, Iter last, const T &element )
    { return std::find( first, last, element ) != last; }

If you must ask for a container, don't bother to specify what the container looks like. Just assume it has the interface you want. This is the easiest thing for you, and the most flexible thing for the user.

template< typename Cont, typename T >
inline bool contains( Cont const &cont, T const &element )
    { return std::find( cont.begin(), cont.end(), element ) != cont.end(); }
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Isn't the second template argument provided with a default value (ie the `std::allocator`? I've never had to specify an allocator anywhere? – rubenvb Apr 11 '11 at 20:05
  • Wow, with your code I get `no matching function for call to 'find(std::vector >::const_iterator, std::vector >::const_iterator, const std::basic_string&)'`. Maybe it's getting too late here... – rubenvb Apr 11 '11 at 20:06
  • your last code sample has undeclared `last` ;-) I believe you meant to change it to cont.end() – Michael Krelin - hacker Apr 11 '11 at 20:09
  • @rubenvb: Yes, there is a default value, but that does not affect which parameter specifications are matched when passing a template template parameter. – Potatoswatter Apr 11 '11 at 20:10
  • Your updated code causes the same error message :( "no matching function call". – rubenvb Apr 11 '11 at 20:13
  • @rubenvb: Here's a working context; I didn't change anything in the first snippet: http://ideone.com/nmcOL - did you `#include `? – Potatoswatter Apr 11 '11 at 20:15
  • @Potatoswatter: wow, `#include `. Now why didn't I think of that... Time to wrap up for tonight I think, I'm getting tired :) – rubenvb Apr 11 '11 at 20:20
  • @rubenvb: Note that above only works for vector, deque, list, stack and queue. Look at [this answer](http://stackoverflow.com/questions/5440089/how-to-pattern-match-a-template/5440268#5440268) of mine for a nice container_traits example. :) – Xeo Apr 11 '11 at 20:23
  • This is subtly wrong... the standard allows for implementors to provide any number of arguments as long as they get default values. What it means is that you can use `std::vector` or `std::vector`, but that the implementation is not required to provide a template with exactly two parameters. A compliant implementation could provide `std::vector`, for optimization purposes. STL containers are tricky to this respect... – David Rodríguez - dribeas Apr 11 '11 at 22:27
  • @David: Do you have a reference for that? It sounds plausible, but I have to wonder. (The code listings are a grey area.) Anyway, it's a bad assumption because restricting an interface to the standard containers is counterproductive when the object is flexibility. (Also, many like to treat `string` as a container, and it does take 3 parameters.) – Potatoswatter Apr 11 '11 at 22:53
  • @Potatoswatter: I had read it a couple of times, and I even recall that once I was pointed to some sentence in the standard, but after looking for it I did not find. Then I googled, and I googled more... finally, it seems that I was incorrect and that implementations **cannot** add extra arguments with default values. Funny enough, I found the [answer](http://stackoverflow.com/questions/1469743/standard-library-containers-with-additional-optional-template-parameters) here in SO. Thanks for doubting, I have learnt something new. – David Rodríguez - dribeas Apr 12 '11 at 11:13
  • @David: Wow, thanks for the research! Of course, there are plenty of other ways to pass more parameters, and knowingly breaking debatably-compliant code would still have been within the letter but against the spirit of the law… – Potatoswatter Apr 12 '11 at 12:23
3

Why not just

template<class container_type,typename T>
inline bool contains(const container_type& c,const T& e) {
    return !(c.size() && std::find(c.begin(),c.end(),e)!=c.end());
}
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
2

Try:

template <class T> 
bool contains(const T & cont, const typename T::value_type & elem)
{
   return( std::find(cont.begin(), cont.end(), elem) != cont.end() );
}

Most standard Containers have a value_type typedef which simplifies this.

Chintan
  • 374
  • 2
  • 9
0

It's simpler than that... just consider the container and the contained element type as template parameters...

#include <vector>
#include <list>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>

template<typename C, typename E>
bool contains(const C& c, const E& e)
{
    return std::find(c.begin(), c.end(), e) != c.end();
}

int main()
{
    std::vector<std::string> V;
    std::list<std::string> L;
    std::set<std::string> S;

    V.push_back("Foo"); L.push_back("Foo"); S.insert("Foo");
    V.push_back("Bar"); L.push_back("Bar"); S.insert("Bar");

    std::cout << contains(V, "Foo") << std::endl;
    std::cout << contains(L, "Foo") << std::endl;
    std::cout << contains(S, "Foo") << std::endl;

    std::cout << contains(V, "Baz") << std::endl;
    std::cout << contains(L, "Baz") << std::endl;
    std::cout << contains(S, "Baz") << std::endl;

    return 0;
}

Note however that for example in the code above I'm using contains also with std::set, where std::find is not a smart way to search things (std::set::find can do better than O(n)).

6502
  • 112,025
  • 15
  • 165
  • 265
  • I think I remember a `std::set::find` calling `lower_bound`, which has the better complexity. (GCC 4.5) – rubenvb Apr 11 '11 at 20:21
  • Exactly my point; but `std::find` is just given an iterator so it must walk it instead. I was just saying that `contains` is going to be highly inefficient in that case. – 6502 Apr 11 '11 at 20:24