- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
Advertisements