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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:51:53+00:00 2026-05-27T08:51:53+00:00

The STL provides std::copy but it is tricky to use it with output containers

  • 0

The STL provides std::copy but it is tricky to use it with output containers with fixed sizes as there is no bounds checking on the output iterator

So I invented my own, something like below

template<class InputIterator , class OutputIterator>
void safecopy( InputIterator srcStart , InputIterator srcEnd , 
                    OutputIterator destStart , OutputIterator destEnd )
{
    while ( srcStart != srcEnd && destStart != destEnd )
    {
        *destStart = *srcStart;
        ++srcStart;
        ++destStart;
    }
}

int main()
{
    std::istream_iterator<char> begin(std::cin), end;       
    char buffer[3];

    safecopy( begin, end, buffer, buffer + 3 );     
    return 0;
}

Questions:

  1. Am I reinventing the wheel here ? Is there an stl algorithm to do what I want.
  2. Are there any deficiencies in my safecopy , does it work for everything std::copy works for ?
  • 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-27T08:51:54+00:00Added an answer on May 27, 2026 at 8:51 am

    Let me promote my comment to an answer, so I have a bit more space.

    First off, your implementation looks good.

    Now, why isn’t this in the standard? (The new standard adds std::copy_n, but that does something different, too.*)

    Think about it like this: strncopy isn’t really a “good” idea; it’s just not a terrible idea. Since C doesn’t have any dynamic data structures, a length-checked version is the best you can do.

    But in C++ this doesn’t fit nicely into the general idea of dynamic containers: You would rarely want to overwrite some elements, but rather create all elements, which you do by std::copy plus std::inserter. strncpy is a crutch which requires you to preallocate the destination data structure, but in C++ we can do a lot better than this. With dynamic containers, iterators and inserters, we can copy anything without needing to worry about allocation.

    In other words, any abstract algorithm that you might conceive should have a better, more specific method of obtaining iterators and iterator ranges (think remove/erase); it is rarely the case that the ultimate goal of an algorithm is to only produce an output range that is constrained by some other destination range.

    In summary: Yes, you can do that, but you can probably do better.

    *) Though copy_n plus min of source and destination size could be used to create a bounded copy.

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

Sidebar

Related Questions

The STL commonly defines an output iterator like so: template<class Cont> class insert_iterator :
which STL Algorithm should i use if I want to find something but Don't
Is there a way to call STL libraries from JNI, I believe JNI provides
Why does the C++ STL not provide any tree containers, and what's the best
In the STL almost all containers have an erase function. The question I have
Typically you will find STL code like this: for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); Iter
In the STL library some containers have iterators and it is commonly held that
I have two STL containers that I want to merge, removing any elements that
Recently, I was buzzed by the following problem STL std::string class causes crashes and
Can a STL map be used for keys of varying sizes? I don't have

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.