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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:55:39+00:00 2026-06-10T23:55:39+00:00

back_inserter and insert_iterator are very handy, but they’re also very inefficient! When you’re appending

  • 0

back_inserter and insert_iterator are very handy, but they’re also very inefficient!

When you’re appending chars, for example, there is a great deal of overhead for every element when you’re copying, when in many situations, there really doesn’t need to be.

Is there a way to make them more efficient?

  • 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-10T23:55:40+00:00Added an answer on June 10, 2026 at 11:55 pm

    Yes, you can define a new version of std::copy which can hijack optimizable calls. 🙂

    Below is an example (or "hack", if you prefer to see the glass half-empty) for Visual C++ and GCC.

    On my personal computer (I use VC++ 2010), the code below makes calls ten times faster!
    A benchmark for GCC’s is also here, showing a 5x difference: old version against new version

    But before you use it:

    Note that this code assumes the container provides a vector-like interface.

    As currently written, this only works for C++11, because it uses the type_traits header’s metaprogramming capabilities to only optimize those situations in which the copy operation would stay exception-safe.

    If you don’t need the exception safety (though you should think twice before actually doing this), or if you have another way of checking for such data types, then you can change

    typename enable_if<..., typename insert_iterator<C> >::type
    

    to:

    insert_iterator<C>
    

    and the rest of the code should work for C++03 as well.

    namespace std
    {
        template<class FwdIt, class C>
        back_insert_iterator<C> copy(
            FwdIt begin, FwdIt end, back_insert_iterator<C> it,
            forward_iterator_tag * =
              static_cast<typename iterator_traits<FwdIt>::iterator_category *>(0))
        {
            struct It : public back_insert_iterator<C>
            {
                using back_insert_iterator<C>::container;
                static C &deref(C &c) { return  c; }
                static C &deref(C *c) { return *c; }
            };
            copy(begin, end, inserter(It::deref(static_cast<It &>(it).container),
                          It::deref(static_cast<It &>(it).container).end()));
            return it;
        }
    
        template<class FwdIt, class C>
        typename enable_if<  // Only do this if it would be exception-safe!
            is_nothrow_copy_constructible<typename C::value_type>::value &&
            is_nothrow_copy_assignable<typename C::value_type>::value,
            insert_iterator<C>
        >::type copy(
            FwdIt const &begin, FwdIt const &end,
            insert_iterator<C> output,
            forward_iterator_tag * =                  // only forward iterators
              static_cast<typename iterator_traits<FwdIt>::iterator_category *>(0))
        {
            struct It : public insert_iterator<C>
            {
                using insert_iterator<C>::container;  // protected -> public
                using insert_iterator<C>::iter;       // protected -> public
                static C &deref(C &c) { return  c; }
                static C &deref(C *c) { return *c; }
            };
            It &it(static_cast<It &>(output));
            typename C::iterator it_iter_end(It::deref(it.container).end());
            {
                // Convert iterators to offsets
                typename C::size_type const iter_end_off =
                    std::distance(It::deref(it.container).begin(), it_iter_end);
                typename iterator_traits<typename C::iterator>::difference_type off
                    = std::distance(It::deref(it.container).begin(), it.iter);
    
                // Resize container
                It::deref(it.container).resize(
                    It::deref(it.container).size() +
                    static_cast<typename C::size_type>(std::distance(begin, end)));
                
                // Renormalize, in case invalidated
                it.iter = It::deref(it.container).begin();
                std::advance(it.iter, off);
                it_iter_end = It::deref(it.container).begin();
                std::advance(it_iter_end, iter_end_off);
            }
            typename C::iterator result
              = copy_backward(it.iter, it_iter_end, It::deref(it.container).end());
            copy_backward(begin, end, result);
            return inserter(It::deref(it.container), result);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Forgive me if this question is very vague Some time back i faced this
Is there a way to automatically insert code into a method? I have the
Is there any way to use DataContext to execute some explicit SQL and return
I know I can do this: std::vector<double> vec; std::back_insert_iterator<std::vector<double> > it( back_inserter(vec) ); it
I'm trying to understand how back_inserter work, and this is its implementation that I
I have created a tickets table that holds ID's for everything so they can
I don't understand this cryptic error message but I get 30 of `'value_type' :
My insert function already handles collisions correctly but i want to be able to
I just asked a question involving std::set but after thinking about it some more,
I am using std::transform with an std::back_inserter to append elements to an std::deque .

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.