开发者

template syntax for containers

开发者 https://www.devze.com 2023-02-24 02:03 出处:网络
This is what I have now: template<template<typename T> class container, typename T> inline bool contains( const container<T> &cont, const T &element )

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开发者_StackOverflow社区 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>&)'


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


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消