hi all,
i have a bit of a problem with pointers to vector. I have a map - 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
6 1146
hi all,
i have a bit of a problem with pointers to vector. I have a map - 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
Actually, you would code: -
int main()
-
{
-
map<int , vector<int> * > children;
-
vector<int>* v = children[5];
-
int answer = (*v)[3];
-
}
-
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.
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.
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;
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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================================...
|
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...
|
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...
|
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;
...
}
|
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...
|
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...
|
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"...
|
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;...
|
by: shuisheng |
last post by:
Dear All,
I have a question. Assume
#include <vector>
using namespace std;
class A
{
private:
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
| |