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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:40:23+00:00 2026-05-31T00:40:23+00:00

I would like to have a vector z, and be able to access it

  • 0

I would like to have a vector z, and be able to access it as z[i] or *pz[i], where *pz[i] points to z[i]. so there is one set of values but two ways to access it.

This is the code that I had:

std::vector<double> z;
std::vector<double*> pz;

for (int i = 0; i < 5; i++) {
  z.push_back(7+0.01*i);
  std::cout << i << "  z = " << z << std::endl;
  pz.push_back(&z.back());
  std::cout << i << "  pz = " << pz << std::endl;
  std::cout << i << "  *pz = " ;
  for (int j = 0; j < pz.size(); j++) {
    std::cout << "  " << *pz[j];
  }
  std::cout << std::endl;
}

z[1]=17.3;
std::cout << "z[1] = " << z[1] << std::endl;
std::cout << "*pz[1] = " << *pz[1] << std::endl;

*pz[2]=34.1;
std::cout << "z[2] = " << z[2] << std::endl;
std::cout << "*pz[2] = " << *pz[2] << std::endl;

the output:

0  z = vector(1) [ 7 ]
0  pz = vector(1) [ 0x1d00b80 ]
0  *pz =   7
1  z = vector(2) [ 7, 7.01 ]
1  pz = vector(2) [ 0x1d00b80, 0x1d00bc8 ]
1  *pz =   1.50254e-316  7.01
2  z = vector(3) [ 7, 7.01, 7.02 ]
2  pz = vector(3) [ 0x1d00b80, 0x1d00bc8, 0x1d00bf0 ]
2  *pz =   1.50254e-316  7.01  7.02
3  z = vector(4) [ 7, 7.01, 7.02, 7.03 ]
3  pz = vector(4) [ 0x1d00b80, 0x1d00bc8, 0x1d00bf0, 0x1d00bf8 ]
3  *pz =   1.50254e-316  7.01  7.02  7.03
4  z = vector(5) [ 7, 7.01, 7.02, 7.03, 7.04 ]
4  pz = vector(5) [ 0x1d00b80, 0x1d00bc8, 0x1d00bf0, 0x1d00bf8, 0x1d00c60 ]
4  *pz =   1.50254e-316  7.01  7.02  7.03  7.04
z[1] = 17.3
*pz[1] = 7.01
z[2] = 7.02
*pz[2] = 34.1

There are 3 problems:

  1. The first element of *pz gets undefined after second push.
  2. When I assign z[1], *pz[1] is not z[1] anymore.
  3. When I assign *pz[2], it’s not assigning to z[2] anymore.

(In my actual program I need to have a vector to access even elements and a vector to access odd elements but I don’t want to make a clone and use more memory.)

  • 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-31T00:40:25+00:00Added an answer on May 31, 2026 at 12:40 am

    Two things:

    1. When you do a push_back() or any operation that might cause the vector to be reallocated, you invalidate all pointers to elements of that vector.
    2. In 64-bit, a pointer will probably be the same size as a double so you won’t save any memory that way.

    If you really want to create a second vector with pointers to elements of the first vector, you need to finish building the first vector first. Otherwise, the push_back() operations will invalidate the pointers.

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

Sidebar

Related Questions

I have a vector of pointers. I would like to call a function for
I have a vector of Rect: vector<Rect> myRecVec; I would like to remove the
What I would like to do (in Clojure): For example, I have a vector
I would like to have a nice template for doing this in development. How
I have a class looking like this: #include <vector> #include record.h #include sortcalls.h template<
I have a problem that I would like have solved via a SQL query.
I would like to have a reference for the pros and cons of using
I would like to have an iframe take as much vertical space as it
I would like to have a VM to look at how applications appear and
We would like to have user defined formulas in our c++ program. e.g. The

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.