Open In App

How to Store Data Triplet in a Vector in C++?

Last Updated : 17 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a vector, how can we store 3 elements in one cell of vector.

Example

Input: { {2, 31, 102},
{5, 23, 114},
{9, 10, 158} }

Output: 2 31 102 // In first cell of vector
5 23 114 // In second cell of vector
9 10 158 // In second cell of vector

One solution is to create a user defined class or structure. We create a structure with three members, then create a vector of this structure.

C++
// C++ program to store data triplet in a vector
// using user-defined structure.
#include <iostream>
#include <vector>
using namespace std;

// Defining a structure to store triplet values
struct Test{
    int x, y, z; // Triplet values
};

int main(){
  
    // Creating a vector of Test to store triplet structures
    vector<Test> myvec;

    // Inserting elements into the vector
    // The first value is assigned to x, second to y, and third to z
    myvec.push_back({2, 31, 102});
    myvec.push_back({5, 23, 114});
    myvec.push_back({9, 10, 158});

    // Getting the size of the vector
    int s = myvec.size();

    // Loop to access and display the structure members
    for (int i = 0; i < s; i++) {
      
        // Accessing and printing structure members using their names
        cout << myvec[i].x << ", " << myvec[i].y << ", " << myvec[i].z << endl;
    }

    return 0;
}

Output
2, 31, 102
5, 23, 114
9, 10, 158

Another solution is to use pair class in C++ STL. We make a pair with first element as normal element and second element as another pair, therefore storing 3 elements simultaneously.

C++
// C++ program to store data triplet in a vector
// using the pair class.
#include <iostream>
#include <vector>
using namespace std;
int main(){
  
    // Creating a vector of pairs to store triplets.
    // The first element is an integer, and the second element
    // is another pair of two integers.
    vector<pair<int, pair<int, int>>> myvec;

    // Inserting elements into the vector using make_pair().
    myvec.push_back(make_pair(2, make_pair(31, 102)));
    myvec.push_back(make_pair(5, make_pair(23, 114)));
    myvec.push_back(make_pair(9, make_pair(10, 158)));

    // Getting the size of the vector
    int s = myvec.size();

    // Loop to access and display the elements of the pairs
    for (int i = 0; i < s; i++){
      
        // Accessing elements using first for the first value,
        // and second.first and second.second for the inner pair values
        cout << myvec[i].first << ", " << myvec[i].second.first << ", " 
        << myvec[i].second.second << endl;
    }
    return 0;
}

Output
2, 31, 102
5, 23, 114
9, 10, 158

We can also use tuple in C++ to store three values, similar to a triplet. One of the main advantages of using tuples is that we don't have to define a custom structure, and sorting or other operations can be easily handled using the built-in STL functions.

The below example demonstrates how we can store and manipulate triplets using std::tuple.

C++
//C++ program to store and manipulate triplets 
// using std::tuple.
#include <bits/stdc++.h>
using namespace std;

int main(){
  
    // Creating a vector of tuples to store triplets.
    vector<tuple<int, int, int>> myvec;

    // Inserting triplet elements into the vector
    myvec.push_back(make_tuple(2, 31, 102));
    myvec.push_back(make_tuple(5, 23, 114));
    myvec.push_back(make_tuple(9, 10, 158));

    // Accessing and printing the triplet values
    for (auto &t : myvec){
        cout << get<0>(t) << ", " << get<1>(t)
          << ", " << get<2>(t) << endl;
    }

    return 0;
}

Output
2, 31, 102
5, 23, 114
9, 10, 158

Next Article
Practice Tags :

Similar Reads