Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4324694
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:10:01+00:00 2026-05-21T09:10:01+00:00

This is what I have now: template<template<typename T> class container, typename T> inline bool

  • 0

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>&)'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:10:02+00:00Added an answer on May 21, 2026 at 9:10 am

    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(); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template based class [Allotter.h & Allotter.cpp]: template <typename allotType> class Allotter
I have this class: template<typename T> class Parser { public: Parser() : count(0) {}
I have template class Reader template<typename T> class Reader{ typedef T type; }; Special
If I have a template base class with a template method : template <typename
I have written a list-like template class sll (Single Linked List). Now, I am
I have a template class where typename is a pointer to a function: CHook<NTSTATUS(__stdcall*)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,HANDLE,PCLIENT_ID,PCONTEXT,PUSER_STACK,BOOLEAN)>
I have a template class, the only template parameter is typename. template<typename T> class
I have a class looking like this: #include <vector> #include record.h #include sortcalls.h template<
I have a problem with variadic template templates: template <typename T> class A {
I have a template class which implements function: template<typename T> class Matrix { ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.