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 5938881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:44:53+00:00 2026-05-22T15:44:53+00:00

Inspired by contains() , I want do declare contains(), fitting misc containers. // for

  • 0

Inspired by contains(), I want do declare contains(), fitting misc containers.

// for std::vector, std::list, ..           (1)
template<typename C, typename T>
bool contains(const C& container, const T& val) 
{
    return ::std::find(container.begin(), container.end(), val) != container.end();
}

// partial specialization for std::map       (2)
template<typename K, typename V>
bool contains(const ::std::map<K, V>& container, const K& key)
{
   // std::map.find() is better than std::find used in (1)
    return container.find(key) != container.end(); 
}

Following question, I want to add the functions to namespace the arguments belongs too.

Questions:

  1. Types of val from (1) and key from (2) are unknown. Does it mean I don’t need to put the functions to any namespace or do I need to put them in std namespace, the containers belong to?

  2. Is it possible to improve (2) it will preferred by compiler than (1) for std::map, std::set, boost::unordered_map, boost::unordered_set?

  3. boost::algorithm::contains(). Do I need choose another name for my wrapper?

  • 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-22T15:44:53+00:00Added an answer on May 22, 2026 at 3:44 pm

    (1) Don’t add anything to the std namespace

    (2) As Space_C0wb0y points out, (2) is not a partial specialization, but an overload. I’m not sure if this is standard behaviour, but on VS2008 this is resolved correctly (the overloaded version is used for map). In either case, I think a slighly better version would be:

    template<typename C>
    bool contains(const C & mapContainer, const typename C::key_type & key)
    {
      // std::map.find() is better than std::find used in (1)
      return mapContainer.find(key) != mapContainer.end(); 
    }
    

    This is a partial specialization (T is specialized to C::key_type) and would work for all types that has the key_type typedef (e.g. std::map, boost::unordered_map etc.)

    (3) Put in separate namespace (see @Tony’s answer)

    Sidenote: I’m not sure I think these functions should have the same name. If I saw a function contains() accepting a map + another argument I could think that (1) the function checks if the map contains an entry for the given value (e.g. some key has the value provided) or (2) the function checks if there is such an entry in the map (e.g. the provided value is a pair). Instead I would call the function contains_key() for maps.

    Edit:
    After a bit of checking I suspect that the code above isn’t compliant as it uses “template typedefs”. This is not a standard C++ feature, but it seems that Visual Studio supports it in some cases. There’s probably a workaround for this, but not one I’m clever enough to come up with now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inspired by: NServiceBus.Configure.With().Log4Net(a => a.YourProperty = value); I want to use something similar as
Inspired by std::string in a multi-threaded program and another answer (seen somewhere), that STL
Inspired by this CodingHorror article, Protecting Your Cookies: HttpOnly How do you set this
Inspired by Raymond Chen's post , say you have a 4x4 two dimensional array,
Inspired by the MVC storefront the latest project I'm working on is using extension
Inspired by another question asking about the missing Zip function: Why is there no
Inspired by this question , I wanted to try my hand at the latest
Inspired by this question and answer , how do I create a generic permutations
Inspired by the question What’s the simplest way to call Http GET url using
Inspired by this question I began wondering why the following examples are all illegal

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.