容器的模板语法

发布于 2024-11-01 07:02:22 字数 1225 浏览 1 评论 0原文

这就是我现在所拥有的:

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

我想这样称呼它:

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

我相信这应该是可能的,但我尝试过的所有内容都会引发不同的错误。任何帮助表示赞赏。谢谢!

更新:感谢所有的答案,我看得太远了,但我仍然遇到问题:

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

即使没有显式的“字符串”构造函数,也无法编译:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

握住你手 2024-11-08 07:02:22

STL 容器采用两个参数,一个用于包含的类型,另一个用于描述如何获取内存的分配器。

尝试一下:

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

然而,这确实是一个很好的例子,说明了为什么您应该完全避免在容器上参数化算法。最好遵循 模式并仅指定迭代器类型参数。

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

如果您必须要一个容器,请不必费心指定容器的外观。假设它有您想要的界面。这对你来说是最简单的事情,对用户来说也是最灵活的事情。

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

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(); }
九八野马 2024-11-08 07:02:22

为什么不只是

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

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());
}
少年亿悲伤 2024-11-08 07:02:22

尝试:

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

大多数标准容器都有一个 value_type typedef 来简化这一过程。

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.

栖竹 2024-11-08 07:02:22

它比这更简单...只需将容器和包含的元素类型视为模板参数...

#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;
}

但请注意,例如在上面的代码中,我也将 containsstd 一起使用: :set,其中 std::find 不是一种聪明的搜索方式(std::set::find 可以比 O(n) 做得更好)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文