Data access inside std :: vector container

I have three std :: vector.

typedef std::pair< double,double > A;
typedef std::vector< A > B;
typedef std::vector< B > C;

I know how to access an element inside B, for example

B b;
b.at(0).first;
b.at(0).second;
And
C c;

How can I access element b using container variable c? Thanks

+3
source share
3 answers

C.at(0).at(0)will get access to the first item B. Because it Cis a container B.

+2
source

(I know that you (or someone) updated the question to fix the flaw typedef- just leaving it as relevant to the original question if it helps someone else).

You need to understand the difference between a type and an object / variable / instance of this type. For example, doublethis is a type, and if you say:

double x;

x double, .

, , . , , , - .

std::pair<> std::vector<> , : std::vector<int> std::vector int , :

std::vector<int> vi;

, :

std::pair< double,double > A;
std::vector< A > B;
std::vector< B > C;

A std::pair<double,double>. std::vector<> , - std::vector<> . , :

typedef std::pair< double,double > A;
std::vector< A > B;

typedef , std::pair<double, double>, B :

std::vector< std::pair<double, double> > B;

, . :

typedef std::pair< double,double > A;
typedef std::vector< A > B;
std::vector< B > C;

C std::vector<std::vector<std::pair<double,double>>>.

, C size() i j, , doubles, :

C[i][j].first;
C[i][j].second;

i, j , :

try
{
    std::cout << C.at(i).at(j).first << ' ' << C.at(i).at(j).second << '\n';
}
catch (const std::exception& e)
{
    std::cerr << "caught an exception " << e.what() << '\n';
}
+2

C.at(i) is the link * i * th B in C.

C.at(i).at(j) is * j * th Link to the item in the link * i * th B in C.

You must refer to the specification std::vector.

+1
source

All Articles