get value from pointer to bidimensional vector

Hello!

I have this a pointer to a bidimensional vector

 
std::vector< vector< int > > * top_objects;



How can I access to top_objects[0][0] ? This doesn't work because it's a pointer to a vector, not a vector

thanks

marc
top_objects is a pointer to a vector of a vector
*top_objects is a vector of a vector
(*top_objects)[0] is a vector
(*top_objects)[0][0] is a single element in that vector
Last edited on
Hi!

You can use a reference. Here is a sample:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <vector>
#include <iostream>


void fnc( std::vector < std::vector< int > > & p)
{
    for (int y = 0; y < p.size(); y++)        
        for(int x = 0; x < p[y].size(); x++)p[y][x] = 20;
}


int main( )
{
    std::vector < std::vector< int > > array;

    array.resize(3);
    for (int i = 0; i < array.size(); i++)array[i].resize(3);

    fnc(array);

    for (int y = 0; y < array.size(); y++)
    {
        for (int x = 0; x < array[y].size(); x++)   std::cout << array[y][x] << " ";
        std::cout << std::endl;
    }


    return 0;
}
Thanks!

I understand both solutions, but now I'm facing another problem: I am passing by reference that bidimensional vector to the constructor of myClass. I want to keep that reference because I need to make modifications to the bidi-vector during the execution of the program

I tried assigning it to another variable, but it creates a copy instead of modifying the original one. How can I do it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
std::vector < std::vector< int > > ref_to_root_vector;

MyClass::MyClass( std::vector < std::vector< int > > & p){
	ref_to_root_vector = p;
}
void MyClass::update(){
	ref_to_root_vector.resize(3);
	ref_to_root_vector[0].push_back(3);
	...	
}

///////////

int main( ){
    std::vector < std::vector< int > > array;

    MyClass temp = new MyClass(array);
	
    temp.update();

    return 0;
}


Essentially, what I need is to be able to modify "array" from all existing instances of MyClass during runtime

Thanks!

marc
Topic archived. No new replies allowed.