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

The Archive Base Latest Questions

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

How is std::vector implemented, using what data structure? When I write void f(int n)

  • 0

How is std::vector implemented, using what data structure? When I write

void f(int n) {
  std::vector<int> v(n);
  ...
}

Is the vector v allocated on stack?

  • 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-09T12:45:50+00:00Added an answer on June 9, 2026 at 12:45 pm

    The vector object will be allocated on the stack and will internally contain a pointer to beginning of the elements on the heap.

    Elements on the heap give the vector class an ability to grow and shrink on demand.

    While having the vector on the stack gives the object the benefit of being destructed when going out of scope.

    In regards to your [] question, the vector class overloads the [] operator. I would say internally it’s basically doing something like this when you do array[1]:

    return *(_Myfirst+ (n * elementSize))
    

    Where the vector keeps track of the start of it’s internal heap with _Myfirst.

    When your vector starts to fill up, it will allocate more memory for you. A common practice is to double the amount of memory needed each time.

    Note that vector inherits from _Vector_val, which contains the following members:

    pointer _Myfirst;   // pointer to beginning of array
    pointer _Mylast;    // pointer to current end of sequence
    pointer _Myend; // pointer to end of array
    _Alty _Alval;   // allocator object for values
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just realized that I've been using std::vector::data() out of similarity with std::string, but
Is this example code valid? #include<vector> using namespace std; int main() { vector<int> vec(10);
std::vector<const int> vci; vci.push_back(1); vci[0] = 2; With the element type being const int
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
I have a std::vector<int> , and I want to delete the n th element.
Please consider the following example: #include <string> #include <vector> using std::string; using std::vector; template
I've always thought it's the general wisdom that std::vector is implemented as an array,
I would like to write some class which maps int to something using templates.
std::vector<bool> reprVectors::encode(std::vector<float> input){ std::vector<float> distance; for(size_t i=0;i<this->reprVectorsList.size();i++){ distance.push_back(distBtw(input,this->reprVectorsList[i])); } std::vector<float>::iterator it= min_element(distance.begin(),distance.end()); return this->reprVectorsList[it]->code;
std::vector<Foo> vec; Foo foo(...); assert(vec.size() == 0); vec.reserve(100); // I've reserved 100 elems vec[50]

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.