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


In this tutorial, we will be discussing a program to understand how to store a Data triplet in a vector in C++.

To store three elements in a single cell of a vector we will creating a user defined structure and then make a vector from that user defined structure.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
struct Test{
   int x, y, z;
};
int main(){
   //creating a vector of user defined structure
   vector<Test> myvec;
   //inserting values
   myvec.push_back({2, 31, 102});
   myvec.push_back({5, 23, 114});
   myvec.push_back({9, 10, 158});
   int s = myvec.size();
   for (int i=0;i<s;i++){
      cout << myvec[i].x << ", " << myvec[i].y << ", " << myvec[i].z << endl;
   }
   return 0;
}

Output

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

Updated on: 02-Mar-2020

384 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements