473,386 Members | 1,758 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 problem

hi all,

i have a bit of a problem with pointers to vector. I have a map

Expand|Select|Wrap|Line Numbers
  1. map<int , vector<int> * > children;
now I can access all the vectors trough commands like children[i]->begin().

the question is how do I gain random acces to an element of a vector to edit the value?

I have tried *(children[i])[j] = 5; but this gives me a error C2100: illegal indirection

I have tried other options but these are just wrong. I know I can evade the problem by using iterators, but I use rather large vectors so this is not optimal.

cheers,
stefaan
Jul 15 '07 #1
6 1146
JosAH
11,448 Expert 8TB
hi all,

i have a bit of a problem with pointers to vector. I have a map

Expand|Select|Wrap|Line Numbers
  1. map<int , vector<int> * > children;
now I can access all the vectors trough commands like children[i]->begin().

the question is how do I gain random acces to an element of a vector to edit the value?

I have tried *(children[i])[j] = 5; but this gives me a error C2100: illegal indirection

I have tried other options but these are just wrong. I know I can evade the problem by using iterators, but I use rather large vectors so this is not optimal.

cheers,
stefaan
Go slowly here: children[i] is a pointer to vector, so *(children[i]) is the vector
itself and (*(children[i]))[5] is the fifth child. When in doubt parenthesize.

kind regards,

Jos
Jul 15 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Actually, you would code:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. map<int , vector<int> * > children;
  4.  vector<int>* v  = children[5];
  5.  int answer = (*v)[3];
  6. }
  7.  
The key 5 will return a value (pair::second), which is a pointer to a vector<int>. So, *v is the vector, so (*v)[3] is the 4th int in that vector.

It's not going to work like a 2D array but you can bury this logic in a function that is used throughout your code.

Be careful using pointers in STL conatiners. You should use a handle instead.
Jul 15 '07 #3
Just a question, but isn't it usually better to use -> instead of (* ) when you have a pointer to an object? Such as:

children[i]->[j] = 5;

Or won't that work with []? I haven't tried it.
Jul 15 '07 #4
Darryl
86
Just a question, but isn't it usually better to use -> instead of (* ) when you have a pointer to an object? Such as:

children[i]->[j] = 5;

Or won't that work with []? I haven't tried it.
Won't work, at best you'd have to

children[i]->operator[](j) = 5; //quite ugly

but better, you can use at()

children[i]->at(j) = 5;
Jul 16 '07 #5
thanks all,

(*(children[i]))[5] = 0 does the trick perfectly. Apparently the brackets do matter :)

I don't know handles for pointers, will look in to it later.

I know -> is the general way to go with pointers but ->[5] doesn't work.

the reason at is no good is becaus it just returns the value at say 5. I wanted to edit the value at 5. sorry if this wasn't clear.

anyway thanks all,

stefaan
Jul 16 '07 #6
Darryl
86
thanks all,



the reason at is no good is becaus it just returns the value at say 5. I wanted to edit the value at 5. sorry if this wasn't clear.


stefaan
Actually it returns a reference so children[i]->at(5) = 0 would edit the value at 5.

additionally at() reads cleaner than the pointer dereference and at() has bounds checking which may be desirable if speed is not a concern
Jul 16 '07 #7

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

Similar topics

7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
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: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
13
by: Steve | last post by:
I have defined the following private object: std::vector<Banana> bananas; in my header file. I have also added a method called FillVector(), which sets the size of the vector and fills it with...
18
by: imutate | last post by:
I have an integer variable and I am testing it as follows #define NULL_VAL -1 ... if (x.id != NULL_VAL) { std::cout << x.id << std::endl; ... }
0
by: acosgaya | last post by:
hi, I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors. I am trying to partition...
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: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
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: shuisheng | last post by:
Dear All, I have a question. Assume #include <vector> using namespace std; class A { private:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.