STL

vector<pair<int, pair<int, int>>> a; please explain this in detail
Most likely it's a part of your code you should burn and re-write.

Start from the outside and work your way in.
So first we have a vector, and each element in the vector is a pair.
Each pair pairs together an int with a pair of ints.

Here's an example of how you would initialize such a monstrosity.

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
// Example program
#include <iostream>
#include <vector>
#include <utility> // std::pair

int main()
{
    using std::vector;
    using std::pair;
    
    // initialize with two elements in the vector
    vector<pair<int, pair<int, int>>> a = {
      pair<int, pair<int, int>>(3, pair<int, int>(4, 5)),  
      pair<int, pair<int, int>>(6, pair<int, int>(7, 8)),  
    };
    
    // push_back another element
    a.push_back( pair<int, pair<int, int>>(9, pair<int, int>(10, 11)) );    

     // alternatively,
    vector<pair<int, pair<int, int>>> b = {
      {3, {4, 5}},
      {6, {7, 8}},
      {9, {10, 11}}
    };   
}
Last edited on
Start in the inside and move out. The type pair<int, int> is a pair of integers. Maybe blood pressure (systolic, diastolic). Maybe (origination node number, destination node number). Maybe (address number, number of people in the house). All we know is it is a pair of numbers. Let's call this type P_INT.

The type pair<int, pair<int, int>> is a pair where the first value in the pair is an int and the second value is a P_INT. So, we can have a pair of, for instance, (test subject number, blood pressure). Let's call this type P_INT_PAIR.

vector<pair<int, pair<int, int>>> is a vector of P_INT_PAIR elements. So, whatever P_INT_PAIR represents, this is a bunch of them. The variable a is such a vector.

So, whatever the meaning of the individual integers, a is a vector of pairs in which the first value is an int and the second value is another pair of ints.
Funny how I said the opposite of you (outside-to-in vs inside-to-out).

Also, here's just some more code to show printing and some manipulation of such a structure.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Example program
#include <iostream>
#include <vector>
#include <utility> // std::pair


/// Printing:

template <typename T, typename U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& pair)
{
    return os << "(" << pair.first << ", " << pair.second << ")";
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vector)
{
    if (vector.size() == 0)
    {
        return os << "{ }";   
    }
    
    os << "{ ";
    for (size_t i = 0; i < vector.size()-1; i++)
    {
        os << vector[i] << ", ";
    }
    os << vector.back() << " }";
    return os;
}


/// Program:

int main()
{
    using std::vector;
    using std::pair;
        
   // per doug4's suggetion, could represent
   // { id, {systolic, diastolic} }
      
    vector<pair<int, pair<int, int>>> patients = {
      {1001, {120, 80}},
      {1002, {110, 70}},
      {1003, {150, 60}}  
    };
    
    /// Print all:
    std::cout << patients << std::endl;
    std::cout << std::endl;
    
    /// Print each patient:
    for (size_t i = 0; i < patients.size(); i++)
    {
        int id = patients[i].first;
        int systolic  = patients[i].second.first;
        int diastolic = patients[i].second.second;
        
        std::cout << "Patient " << id << ":\n";
        std::cout << "  (" << systolic << ", " << diastolic << ")\n";
    } 
}


/// Printing all:
{ (1001, (120, 80)), (1002, (110, 70)), (1003, (150, 60)) }

/// Print each patient:
Patient 1001:
  (120, 80)
Patient 1002:
  (110, 70)
Patient 1003:
  (150, 60)


_________________________________

But: It depends on the exact application, but personally, I would make a struct call Patient, and make it have an id and std::pair for blood pressure.


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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Example program
#include <iostream>
#include <vector>
#include <utility> // std::pair

/// Printing:

template <typename T, typename U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& pair)
{
    return os << "(" << pair.first << ", " << pair.second << ")";
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vector)
{
    if (vector.size() == 0)
    {
        return os << "{ }";   
    }
    
    os << "{ ";
    for (size_t i = 0; i < vector.size()-1; i++)
    {
        os << vector[i] << ", ";
    }
    os << vector.back() << " }";
    return os;
}

/// Program:

struct Patient {
    Patient(int id, const std::pair<int, int>& blood_pressure)
    : id(id), blood_pressure(blood_pressure) {}

    int id;
    std::pair<int, int> blood_pressure;
};

std::ostream& operator<<(std::ostream& os, const Patient& patient)
{
    os << "Patient " << patient.id << ": " << patient.blood_pressure;
    return os;
}

int main()
{

   // per doug4's suggetion, could represent
   // { id, {systolic, diastolic} }
      
    std::vector<Patient> patients = {
      {1001, {120, 80}},
      {1002, {110, 70}},
      {1003, {150, 60}}  
    };
    
    /// Print all:
    std::cout << patients << std::endl;
}


But hey, maybe that's not more clear for your application. Do what you think is best.
Last edited on
But: It depends on the exact application, but personally, I would make a struct call Patient, and make it have an id and std::pair for blood pressure.


Just to point out what may be obvious: I have no idea what the data from the OP is referring to. I was making up meanings for the various int values to give some kind of filter through which the reader might be able to understand how the data values relate to one another.

I also agree that in my hypothetical model, creating a class Patient (and possibly class BloodPressure) would be far superior to a pair<int, pair<int, int>>. I was merely trying to explain how to interpret the single line of code from the OP.

Funny how I said the opposite of you (outside-to-in vs inside-to-out).

Yeah, you got your response in while I was working on mine. There was no intention of trying to contradict you--I hadn't seen your post yest--but we attacked the problem from opposite points of view. Sometimes a bottom-up approach is warranted; sometimes a top-down approach is. It all depends on the problem being solved.
I didn't say you were contradicting me, just thought it was funny. :) I know OP didn't say what the actual application, but I liked your example of blood pressure.

Edit: This misunderstanding may have been from when I said "you" -- after my "but", I was talking to the OP, but I can see how you might have thought I was talking to you. Hope that clears things up.
Last edited on
Topic archived. No new replies allowed.