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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:28:07+00:00 2026-06-07T23:28:07+00:00

Is there a std::copy -like algorithm that accepts four iterators, denoting two ranges? Basically,

  • 0

Is there a std::copy-like algorithm that accepts four iterators, denoting two ranges?

Basically, it should stop copying as soon as either range is exhausted:

template<typename Iter>
void copy_range(Iter begin1, Iter end1, Iter begin2, Iter end2)
{
    for (; (begin1 != end1) && (begin2 != end2); ++begin1, ++begin2)
    {
         *begin2 = *begin1;
    }
}
  • 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-07T23:28:09+00:00Added an answer on June 7, 2026 at 11:28 pm

    No there is no such thing sadly. The closest thing is std::copy_n.

    And of course the algorithm you just wrote.

    Depending on the type of iterator used (random or not), using this is more efficient (because only one check needs to be made for each iteration) than your algorithm:

    std::copy_n(begin1,
                std::min(std::distance(begin1, end1), std::distance(begin2, end2)),
                begin2);
    

    Another alternative is a checked output iterator, something along the lines of this (rough sketch, not checked code):

    template<class Iter>
    class CheckedOutputIter {
    public:
        // exception used for breaking loops
        class Sentinel { }
    
        CheckedOutputIter()
            : begin(), end() { }
    
        CheckedOutputIter(Iter begin, Iter end)
            : begin(begin), end(end) { }
    
        CheckedOutputIter& operator++() {
            // increment pas end?
            if (begin == end) {
                throw Sentinel();
            }
    
            ++begin;
            return *this;
        }
    
        CheckedOutputIter operator++(int) {
            // increment past end?
            if (begin == end) {
                throw Sentinel();
            }
    
            CheckedOutputIter tmp(*this);
    
            ++begin;
    
            return tmp;
        }
    
        typename iterator_traits<Iter>::value_type operator*() {
            return *begin;
        }
    
    
    private:
        Iter begin, end;
    };
    

    Usage:

    try {
        std::copy(begin1, end1, CheckedOutputIter(begin2, end2));
    } catch(const CheckedOutputIter::Sentinel&) { }
    

    This has roughly the same performance as your solution, but it is more widely usable.

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

Sidebar

Related Questions

In C++98, I can copy ranges with the std::copy algorithm. std::copy(source.begin(), source.end(), destination.begin()); Is
I have an std::vector -like class that is compiled with Visual C++ 2008. There's
I would like to implement a copy of std::stack< boost::shared_ptr<T> > . Is there
And I know there's std::cin , but that requires the user to enter a
Is there an alternative version of std::find_if that returns an iterator over all found
Is there an alternative container that has the same functionality as std::map but it
I would like to copy the content of one std::map into another. Can I
I looked into the GCC STL (4.6.1) and saw that std::copy() uses an optimized
I'd like to copy values that match a predicate (equal ints) from a map<string,int>
having something like this: void print_signs() { const char* chars[] = {abcdefghijklmnopqrstuvwxyz0123456789}; std::copy(chars,chars +

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.