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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:54:42+00:00 2026-05-31T04:54:42+00:00

Say I have a std::vector called vec with 10 elements and I want to

  • 0

Say I have a std::vector called vec with 10 elements and I want to create a new std::vector containing all elements between (and including) the 2nd and 5th elements of vec. I can see how I might write a for loop to do this, but it looks like STL’s copy() can do this more concisely. But I’m not really getting iterators: I’ve seen how you can use start() and end() to iterate over a vector from its first to last element, but what about the situation above, where I want something slightly different? Thanks.

  • 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-31T04:54:44+00:00Added an answer on May 31, 2026 at 4:54 am

    You don’t need std::copy to create a new vector with a subset of the first one. You can achieve this with the vector‘s constructor and its iterators (doc here):

    std::vector<myType> vec = ...;
    std::vector<myType> other(vec.begin() + 1, vec.begin() + 5);
    

    You have to be sure though, that you don’t exceed the vector‘s limits, or you will get undefined behavior.

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

Sidebar

Related Questions

Let's say I have a class FOO. I want to have a std::vector of
let's say i want to have a member variable for a pointer to std::vector
Let's say I have a container (std::vector) of pointers used by a multi-threaded application.
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
Say, I have a std::vector<SomeClass *> v; in my code and I need to
Let's say I have a: std::vector<std::list<Node> > And let's say Node is: class Node
Here is my issue, lets say I have a std::vector with ints in it.
Say I have the following: std::vector<int> myints; and then I have a function that
Say I have two containers storing pointers to the same objects: std::list<Foo*> fooList; std::vector<Foo*>
Let's say I have a std::vector and I get by some means the address

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.