473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

resizing vector via a pointer to the vector

Hi,
Can anyone please tell me if the following is possible, and
if so, what is the correct syntax?

I'd like to resize a vector (and access other vector member functions)
via a pointer to that vector, e.g:
vector<int> x(10);
vector<int>* p = &x;
*p.resize( 5 );
cout << *p[0] << endl;
But the compiler throws an error: "request for member `resize' in
`p', which is of non-aggregate type `vector<int, allocator<int> > *'"

How do I dereference p to obtain an aggregate type?

Here's my complete code:
#include<vector>
#include<iostream>
using namespace std;
int main(){

// some data vectors:
vector<int> x( 10, 1 );
vector<float> y( 10, 2.);
vector<double> z( 10, 3.);

// try to resize via a pointer to the vector:
// (doesn't work)
vector<int>* p = &x;
*(p).resize( 5 );
cout << *(p)[0] << endl;

// a vector of pointers to the data vectors:
int Nentries=3;
vector< void* > entries(Nentries);
entries[0] = &x;
entries[1] = &y;
entries[2] = &z;

// try to resize each of the data vectors via the pointers:
// (doesn't work)
for ( int entry=0; entry<Nentries; entry++ ) *entries[entry].resize( 5 );

}

Any suggestions?
Thanks very much,
Sean
Jul 19 '05 #1
2 8670
"Sean Dettrick" <sd*******@hotmail.com> wrote...
Hi,
Can anyone please tell me if the following is possible, and
if so, what is the correct syntax?

I'd like to resize a vector (and access other vector member functions)
via a pointer to that vector, e.g:
vector<int> x(10);
vector<int>* p = &x;
*p.resize( 5 );
cout << *p[0] << endl;
But the compiler throws an error: "request for member `resize' in
`p', which is of non-aggregate type `vector<int, allocator<int> > *'"

How do I dereference p to obtain an aggregate type?


(*p).resize(5);

or

p->resize(5);

the same goes for the operator []. If you want to reach
the zeroth element of the vector pointed to by 'p', you
need to put *p in parentheses:

(*p)[0]

otherwise you're indexing the vector from the pointer and
then trying to dereference the vector (doesn't compile):

*(p[0])

Learn the precedence of operators.

Victor
Jul 19 '05 #2
Thanks very much!
Sean
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lasse Skyum | last post by:
Is it true that when std::vector resizes for more capacity it copies all the elements to a bigger array and then destroys all the elements from the old one? If so, why doesn't it just use...
46
by: Blue Ocean | last post by:
How do I do it? I'm doing a practice problem which includes me implementing a set of ints as a class. I don't want to use vector because that would be cheating. ;) I don't want to spend...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
5
by: sandwich_eater | last post by:
I get a segmentation fault in my program when resizing a vector. This seems strange because it is the second resize that causes the error. std::vector<double> a; std::vector<double> b; ...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
9
by: dli07 | last post by:
Hello, I'm trying to convert a piece of code that creates a dynamic vertical resizing bar in a table from internet explorer to firefox. It's based on a post from...
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.