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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:56:45+00:00 2026-06-09T14:56:45+00:00

A std::vector contains a buffer of continuous memory internally for a given type, with

  • 0

A std::vector contains a buffer of continuous memory internally for a given type, with the exception of bools. Is there anyway of constructing a vector by specifying this buffer such that no coping of data is required?

I have a C api which gives me a buffer of data of a certain type. I would like to be able to manipulate this data via the functionality associated with std::vector, such as std::vector<>::iterator, begin(), end() etc.

Maybe you have a better suggestion as to how I might work with these buffers, as they are huge and I don’t wish to copy them.

The api allocates the memory and provides a function which I call to tell it to release it again.

  • 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-09T14:56:46+00:00Added an answer on June 9, 2026 at 2:56 pm

    Why dont you just wrap the buffer in a simple class containing the functions you want to be able to use.
    Something like this will probably suffice, using the fact that pointers are iterators.

    template<typename T>
    struct RawBuffer<T>
    {
      RawBuffer( T* in_buffer, size_t in_n ) : buffer(in_buffer), n(in_n) {}
      T* buffer;
      size_t n;
      T* begin() { return buffer; }
      T* end() { return buffer+n; }
      const T* begin() const { return buffer; }
      cont T* end() const { return buffer+n; }
      T& operator[](size_t i) { return buffer[i]; }
      const T& operator[](size_t i) const { return buffer[i]; }
    };
    

    Now you can use it kinda like a vector:

    RawBuffer<MyObject> values( generate_objects(n), n );
    
    //Set an entry
    values[1] = MyObject()
    
    //Or use an entry
    values[1].do_something();
    
    //Lets use some std functions on the object.
    std::for_each( values.begin(), values.end(), my_object_fn );
    
    //Or create a real vector from it
    std::vector<MyObject> values_copy( values.begin(), values.end() );
    

    If you also want to manage the memory that the buffer contains then you’ll need to add a destrtuctor and remove the default copy constructor and assignment operator.

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

Sidebar

Related Questions

So I have a container(any kind, probably std::map or std::vector) which contains objects of
I'd like to create a std::map that contains a std::vector of iterators into itself,
I have a C++ std::vector denoted as: std::vector<GameObject*> vectorToSort; Each object in vectorToSort contains
I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
#include <iostream> using namespace std; // This first class contains a vector and a
std::vector<const int> vci; vci.push_back(1); vci[0] = 2; With the element type being const int
In C++, given vector<T> src, dst , both already sorted, is there a more
Is there any built in function which tells me that my vector contains a
#include <vector> #include <iostream> #include <memory> int main() { // create the vector std::vector<
What is the fastest way (if there is any other) to convert a std::vector

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.