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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:37:55+00:00 2026-06-12T15:37:55+00:00

Given myvector.start() and myvector.end() I want to create a read-only subset of myvector without

  • 0

Given myvector.start() and myvector.end() I want to create a read-only subset of myvector without copying the data.

Is this possible, and how?

#include <iostream>
#include <vector>

using namespace std;

template <class T> void print_vector(const vector<T> &v) {
    for(size_t i = 0; i < v.size(); ++i) std::cout << v[i] << " ";
    std::cout << std::endl;
}

int main() {

First I create the vector.

    vector<double> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    print_vector(data); // 1 2 3 4 5 6 7 8 9 10

Then I want a subset. But I think this makes a copy.

    // Does this make a copy or not? I don't want to make a copy.
    const vector<double> subset1(data.begin() + 3, data.end() - 3);
    print_vector(subset1); // 4 5 6 7

How about this approach?

    // Another approach. Questions:
    // - Would something like this be a good alternative if I really don't want a copy?
    // - How would the performance of this be compared to a normal STL container?
    // - Is there an existing implementation of a container that does something like this, maybe a boost class?
    class SubsetType {
    public:
        SubsetType(const vector<double>::iterator &start, const vector<double>::iterator &end) { begin_ = start; end_ = end; }

        vector<double>::iterator begin() { return begin_; }
        vector<double>::iterator end() { return end_; }

        double operator[](vector<double>::size_type i) { return *(begin_ + i); }

        vector<double>::size_type size() { return end_ - begin_; }

    private:
        vector<double>::iterator begin_, end_;
    };

    SubsetType subset2(data.begin() + 3, data.end() - 3);
    for(size_t i = 0; i < subset2.size(); ++i) std::cout << subset2[i] << " ";
    std::cout << std::endl; // 4 5 6 7

Or is the solution to declare all functions like f(const vector::iterator &start, const vector::iterator &en). The STL algorithms do this, right? (but generic)

Exit

    std::cout << "Bye!" << std::endl;
    return 0;
}
  • 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-12T15:37:57+00:00Added an answer on June 12, 2026 at 3:37 pm

    Just use iterators (const ones where appropriate).

    If you’re really allergic to passing begin/end pairs around everywhere, consider using Boost.Range or something similar: it lets you bundle the [start,end) pair into a single object.

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

Sidebar

Related Questions

Possible Duplicate: Initializing private static members This is really driving me crazy, I want
Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;
Given a certain date, I want to set the value of a cell with
Given a MySQL table of real estate data, I would like to generate a
Given that an input String may be specified as follows: read(xpath(‘...’)) or xpath(‘...’) or
I am using vector<char> to send and receive data via socket. In this vector
Given a abstract factory implementation: public class FooFactory : IFooFactory { public IFoo Create(object
Given this: $ids = ''; I just realized that this: $single = $ids ==
Given this example in Python sample = '5PB37L2CH5DUDWN2SUOYE6LJPYCJBFM5N2FGVEHF7HD224UR52KB====' a = base64.b32decode(sample) b = base64.b32encode(a)
Given this webusercontrol: <%@ Control Language=C# AutoEventWireup=true CodeFile=Comments.ascx.cs Inherits=Controls_Fresh_Comments %> Hello World! <asp:PlaceHolder runat=server

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.