How to write a get function for a 2D vector

I am trying to get access a specific variable of a 2D vector in another class using "get" function. However, it is vague for me how to ask the code to select the exact cell.

I have:
vector<vector<double>> slopeGrid; // slope of each grid cell
in a class and column/row address in a different class:
I am thinking about:
double getSlopeGrid (vector<vector<double>> &slopeGrid)
{SlopeGrid = this->slopeGrid;)

but not sure if this way I can get access the exact variable in the 2D vector.

Really appreciate your help.
Thanks
Last edited on
This is a general way of declaring, initializing and accessing a 2D vector using C++11 syntax:
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
#include <iostream>
#include <vector>

constexpr auto ROW = 3;
constexpr auto COL = 4;

int main()
{
    std::vector<std::vector<double>> slopeGrid (ROW, std::vector <double> (COL));

    for (double i = 0; i < ROW; i++)
    {
        for (double j = 0; j < COL; j++)
        {
            slopeGrid[i][j] = i + j;
        }
    }
    for (auto& elem_i : slopeGrid)
    {
        for (auto& elem_j : elem_i)
        {
            std::cout << elem_j << " ";
        }
        std::cout << '\n';
    }
}

Output
1
2
3
4
5
6
0 1 2 3
1 2 3 4
2 3 4 5

Process returned 0 (0x0)   execution time : 0.086 s
Press any key to continue.

Thank you very much for your reply. There are 2 issues:
1- My 2D vector located in a different class and I want to write a get function for accessing this PRIVATE variable.
2- Moreover, I need to get access to an exact cell of that 2D vector, for example slopeGrid[2][3]

I have the x and y of the vector as separate double variables and want to ask the code to read in the value of the vector in that address like:

double x = colNR;
double y = rowNR

slope = slopeGrid[x][y];
Thank you very much for your reply. There are 2 issues:

I suggest you post your code showing what you're trying to do.

I have the x and y of the vector as separate double variables and want to ask the code to read in the value of the vector in that address like:
1
2
3
4
double x = colNR;
double y = rowNR

slope = slopeGrid[x][y]; 



Vector/Array indexes must be integral types, not floating point types.


OP: in that case you'd be looking at some notion of friend-ship b/w the 2 classes (possibly with the vector of vectors as a static member). see here for some options:
http://stackoverflow.com/questions/6717163/how-to-access-private-data-members-outside-the-class-without-making-friends
1
2
3
std::vector<std::vector<double>> SomeClass::getSlopeGrid() const { 
  return this->slopeGrid;
}
My 2D vector located in a different class and I want to write a get function for accessing this PRIVATE variable.
closed account (48T7M4Gy)
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
31
32
33
34
#include <iostream>
#include <vector>

typedef std::vector<std::vector<double>> V2;

class Some
{
private:
    V2 vector;

public:
    Some(V2 aVector) { vector = aVector; }
    double getElement(uint, uint);
    V2 getVector();
};

double Some::getElement( uint x, uint y){ return vector[x][y]; }
V2 Some::getVector() { return vector; };


int main()
{
    V2 v  = { {1,2,3}, {4,5,6}, {7,8.765,9} };
    
    // ONE WAY
    Some object(v);
    std::cout << object.getElement(2,1) << '\n';
    
    // ANOTHER WAY - NOT GOOD
    V2 temp = object.getVector();
    std::cout << temp[2][1] << '\n';
    
    return 0;
}


8.765
8.765
Program ended with exit code: 0
Last edited on
OP made another post on the same subject:
http://www.cplusplus.com/forum/beginner/205804/
closed account (48T7M4Gy)
Well picked gunner :)
Topic archived. No new replies allowed.