rubenvb
rubenvb

Reputation: 76519

template syntax for containers

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>&)'

Upvotes: 1

Views: 1207

Answers (4)

Chintan
Chintan

Reputation: 372

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.

Upvotes: 2

6502
6502

Reputation: 114481

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)).

Upvotes: 0

Potatoswatter
Potatoswatter

Reputation: 137810

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(); }

Upvotes: 5

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143081

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());
}

Upvotes: 3

Related Questions