473,386 Members | 1,610 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,386 software developers and data experts.

vector of pointers

84
Suppose I have a vector defined as
Expand|Select|Wrap|Line Numbers
  1. vector <elem*> v;
  2.  
where elem is some struct defined beforehand.
Now if I type :
Expand|Select|Wrap|Line Numbers
  1. v[0]
  2.  
I get a pointer to elem, ie elem* where elem* is the first element of v.

My question is what do I have to type in order to access elem, ie the value elem* points to?
Jul 29 '07 #1
3 1284
JosAH
11,448 Expert 8TB
Suppose I have a vector defined as
Expand|Select|Wrap|Line Numbers
  1. vector <elem*> v;
  2.  
where elem is some struct defined beforehand.
Now if I type :
Expand|Select|Wrap|Line Numbers
  1. v[0]
  2.  
I get a pointer to elem, ie elem* where elem* is the first element of v.

My question is what do I have to type in order to access elem, ie the value elem* points to?
I'd say all you have to do is this:

Expand|Select|Wrap|Line Numbers
  1. T foo= v[0]->first_of_elem_struct;
  2. U bar= v[0]->second_of_elem_struct;
  3. // etc.etc.
  4.  
kind regards,

Jos
Jul 29 '07 #2
sanctus
84
I'd say all you have to do is this:

Expand|Select|Wrap|Line Numbers
  1. T foo= v[0]->first_of_elem_struct;
  2. U bar= v[0]->second_of_elem_struct;
  3. // etc.etc.
  4.  
kind regards,

Jos
Sorry but I don't understand what you mean. Are T foo and U bar variables to be defined? Or does foo mean something (as I have seen it already quite often on some sites while looking for something else)?
Jul 29 '07 #3
JosAH
11,448 Expert 8TB
Sorry but I don't understand what you mean. Are T foo and U bar variables to be defined? Or does foo mean something (as I have seen it already quite often on some sites while looking for something else)?
No, it doesn't mean much but because I don't know what your first and second
etc. elements of your structs are I simply put 'T foo' as the first element and
'U bar' as the second element.

e.g. if the first element of your struct is an int, named 'index', the example
should read:

Expand|Select|Wrap|Line Numbers
  1. int index= v[0]->index;
  2.  
kind regards,

Jos
Jul 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
13
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
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;
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
8
by: jagguy | last post by:
I am a little confused with the basic concept of vector of pointers. The vector is easy enough. Say you want a vector of pointers to int. The integers are not created outside the vector so all...
5
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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.