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

  • Home
  • SEARCH
  • 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 9033781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:18:59+00:00 2026-06-16T08:18:59+00:00

I’m wondering why the STL doesn’t overload their algorithm functions such that I can

  • 0

I’m wondering why the STL doesn’t overload their algorithm functions such that I can call them by simply providing a container and not taking the more verbose way to pass begin + end iterators. I of course understand why we also want to use an iterator pair for processing subsequences of a container / array, however, almost all calls to these methods are using a whole container:

std::for_each(myVector.begin(), myVector.end(), doSomething);

I’d find it more convenient, readable and maintainable to just write

std::for_each(myVector, doSomething);

Is there a reason STL doesn’t provide these overloads? [EDIT: I don’t mean to replace the interface with this restricted one but to also provide a container-based iterface!] Do they introduce ambiguity? I’m thinking about something like this:

template<typename _Container, typename _Funct>
inline _Funct for_each(_Container c, _Funct f) {
    return for_each(begin(c), end(c), f);
}

Am I missing something?

  • 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-06-16T08:19:01+00:00Added an answer on June 16, 2026 at 8:19 am

    They do introduce ambiguity for many algorithms. A lot of <algorithm> looks like

    template<class iterator>
    void do_something(iterator, iterator);
    
    template<class iterator, class funct>
    void do_something(iterator, iterator, funct);
    

    If you add additional overloads

    template<class container, class funct>
    void do_something(container, funct);
    

    the compiler will have some trouble figuring out what do_something(x, y) means. If x and y are of the same type, it will match both iterator = type and container = type, funct = type.*)

    C++11 tried to solve this with “concepts” that could recognize the difference between a container and an iterator. However, these “concepts” turned out to be too complicated to make it into the standard, so neither did these overloads.

    *) compilers disagree here, the Comeau compiler claims that it is ambiguous, g++ 4.5 and MSVC 10 calls the first function.


    After an extremely long discussion in the comments, here is one example where it doesn’t work as expected – using a container adapter that can also double as a predicate.

    #include <iostream>
    #include <vector>
    
    template<class iterator>
    void test(iterator, iterator)
    {
       std::cout << "test iterator\n";
    }
    
    template<class iterator, class predicate>
    void test(iterator, iterator, predicate)
    {
       std::cout << "test iterator, predicate\n";
    }
    
    template<class container, class predicate>
    void test(const container& cont, predicate compare)
    {
       std::cout << "test container, predicate\n";
    
       test(cont.begin(), cont.end(), compare);
    }
    
    template<class container>
    class adapter
    {
    public:
       typedef typename container::iterator   iterator;
    
       adapter(container* cont) : cont(cont)
       { }
    
       iterator begin() const
       { return cont->begin(); }
    
       iterator end() const
       { return cont->end(); }
    
       bool operator()(const iterator& one, const iterator& two)
       { return *one < *two; }
    
    private:
       container* cont;
    };
    
    int main()
    {
       std::vector<int>   v;
    
       adapter<std::vector<int>>   a(&v);
    
       test(a, a);
    
    }
    

    Output:

    test iterator

    http://ideone.com/wps2tZ

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.