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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:48:58+00:00 2026-05-27T09:48:58+00:00

The following is the definition of copy, according to http://www.sgi.com/tech/stl/copy.html . template<class InputIterator, class

  • 0

The following is the definition of copy, according to http://www.sgi.com/tech/stl/copy.html.

template<class InputIterator, class OutputIterator>
  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{
   while (first != last) *result++ = *first++;
   return result;
}

I wrote the following code.

vector<int> v;
set<int> s;

s.insert(7);
s.insert(11);
s.insert(27);

//copy elements from the set into the vector
copy(s.begin(), s.end(), v.begin());

Why does the call to copy above generate a runtime error but not a compile error? I’m assuming it has to do with the fact that the vector is empty, v.begin() == v.end(). But why?

Also, I fixed the code by changing it to the following.

copy(s.begin(), s.end(), back_inserter(v));

Function back_inserter returns an iterator of type back_insert_iterator>. Why does this work? What is it doing?

  • 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-27T09:48:59+00:00Added an answer on May 27, 2026 at 9:48 am

    It fails because you’re not respecting the third precondition listed on this same site:

    There is enough space to hold all of the elements being copied. More
    formally, the requirement is that [result, result + (last – first)) is
    a valid range. [1]

    An empty vector is smaller than a set of 3 elements, so you cannot perform the copy operation (which really should be called an “overwrite” operation). This information is not known at compile-time and therefore there is no compile-time failure. Keep in mind that while vectors and most other C++ STL collections are expandable at runtime, they cannot be expanded through their regular iterators operations (which mostly serve the purpose of enumerating items and specifying ranges).

    The back_inserter function returns a special iterator that will insert elements to end of the collection. This iterator is an output iterator, and has very little to do with the iterators you can obtain through the begin() and end() methods of the vector. You cannot read from an output iterator.

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

Sidebar

Related Questions

I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {}
I have the following class definition and it needs a copy constructor so deep
Using the following Webservice definition using aClientArgs as a complex type: [System.Web.Script.Services.ScriptService] public class
Lets assume following classes definition: public class A { public final static String SOME_VALUE;
Consider the following class definition: class StrToTokens { StrToTokens(const char* str, const char* delimiters
I have the following class definition: public interface IItem{} public class FirstObject<T, U> :
I have a Tree class with the following definition: class Tree { Tree(); private:
I am working with a COM plugin interface that has the following function definition:
Given the following C++ code: struct foo { // Some definition with copy constructor.
I've got a class definition similar to the following: class UUID { public: //

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.