How to cin to vector














































How to cin to vector



To insert elements into the vector from cin you need to use while loop. Below is the example which explains it clearly.

#include <iostream>
#include <vector>

using namespace std;

// Change cin terminator from here
const int cinTerminator = -1;

int main() {

    int num;
    std::vector<int> v;

    cout << "Enter numbers: ";

    //insert number into vector from cin
    while ((cin >> num) && num != cinTerminator) {
        v.push_back(num);
    }

    cout << "Elements into vector: " << endl;
    // print vector
    for(int i : v) {
        cout << i << " ";
    }
    cout << endl;
    return 0;
}

Output:

Enter numbers: 1 2 3 11 22 5 89 4 6 75 9897 45 -1 Elements into vector: 1 2 3 11 22 5 89 4 6 75 9897 45


More Articles of Pankaj Choudhary:

Name Views Likes
Coding Challenges: Fun Projects for Student Programmers 27 0
5 Must-Have Programming Skills for Student 40 0
Hands-On Experience: Internships and Capstone Projects in Data Science Programs 289 0
5 Tips for Effective Self-Study in Programming Education 159 0
5 Essential Study Techniques for Mastering C++ 219 0
What You Need to Know about Programming Languages 164 0
Pitfalls of Learning Programming 178 0
Truminds 5G UPF supports MPTCP Proxy 9968 214
C++ Assignment Help: Where Can You Find? 492 0
Top Benefits of Learning Coding in College or University 430 0
C++ Abstract Class 860 21
Compile C++11 program using g++ 4178 10
C++ Searching an element in a vector 786 11
Priority Queue 1830 12
Check String is palindrome or not 1259 10
Preorder Tree Traversal with recursion 1148 12
Template based linked list 5627 11
Print all processes running as root 866 28
C++ Queue of objects 4944 18
How to cin to vector 17925 10
Python List Built-In Methods 632 11
Python Basic Programs 588 1
C++ Compare version string 3124 12
C++ Vector of objects 6339 20
Windows enable NTP Server 889 17
Tokenized securities on blockchain 749 0
Difference between remove and remove_if in STL 1049 19
CPU usage per thread without top utility 3002 16
Python Selected Interns (Feb 2019 Batch - 1) 1049 24
Complex Number Program 905 10
Functor with arguments 1339 10
Pass function pointer to a function 541 11
C++ Two dimensional vector 947 21
C++ challange for Aug 2019 Interns 6045 4
C program to get memory usage 9805 18
Python challange for Interns (Feb 2019, Batch 2) 879 13
C++ Read file line by line 917 14
Dynamic Programming - Egg Dropping Problem 1763 16
GDB Print complete string 3162 24
UDP Server Program in C 9212 24
C++ Split string by delimiter and store it in vector 3243 12
Python programs list 788 23
Mutable lambda in C++ 4760 11
C++ Sorting of vector 632 10
Overload Assignment Operator 597 10
Blockchain The Benefits and Risks of Smart Contracts 786 0
Inorder Tree Traversal without recursion 1032 14
Lambdas in C++ 792 11
Inorder Tree Traversal with recursion 815 25
C++ Count maximum number of consecutive ones in a vector 1523 10
C++ Parameter Binding 637 11
Python Converting a String to a List of Words 775 19
Python project to update a doc file conver it to the pdf file and send it to multiple email addresses 1833 29
Test article for interns Python Add Two Numbers 689 12
Ring Buffer 5426 11
Python 2.7 Run linux command and get output 1578 38
C++ Predicate 2299 10
Internship Opportunity at cppsecrets for Nov 2020 batch 5064 1
Find N pairs with smallest sums in two vectors 504 11
C++ Set of objects 852 21
C++ Selected Interns (Feb 2019 Batch - 1) 1148 15
Python challange for Sept 2019 Interns 2521 5
Write your own shared pointer 2495 10
Internship Opportunity at cppsecrets for May 2020 batch 10600 4
C++ Check whether two strings are anagram of each other 3385 12
C++11 Range based for loop 805 11
Erase elements from vector 2125 17
Python send mail 807 22
Git create branch 721 21
ssh Bad owner or permissions 847 0
C++11 decltype 526 12
C++ Read file and store it into integer variable 596 12
Python Tcp Server Example Code 829 12
C++ challange for Oct 2019 Interns 5360 0
Linux - Difference between tty and pts 11156 20
sudo error, is mode 0777, should be 0440 16 10
C++ Config Reader 8785 3
Find maximum product of three numbers 525 11
Python Converting a String to a List of Words based on spaces and alphanumeric characters 975 25
Internship Opportunity at cppsecrets.com 4768 0
C++ map of char* and char* 4629 28
C++11 decltype and typeid 2578 16
Fork system call 879 15
Postorder Tree Traversal with recursion 790 21
How to give paid exam at cppsecrets.com? 1138 0
Functor in C++ 613 11
Internship Opportunity at cppsecrets for UPES Hackathon Participants 752 1
C++11 auto keyword 799 21
C++ Boost Serialization: Serializing primitive data types 55 3
C++ Validate parenthesis pattern 572 10
Dynamic Programming - Rod Cutting Problem 1552 20
Linux Print Process Tree 918 27
Stipend update for the C++ interns of Feb 2019 2464 32
Linux Extract an RPM package without installing it 978 16
Preorder Tree Traversal without recursion 782 16
Internship Opportunity at cppsecrets for March 2020 batch 7956 4
Python challange for Feb 2019 Interns 1092 27
UDP Client Program in C 4151 23
Deep Copy 836 11
Find the Nth largest element in a vector 5472 11
10 Most Frequent Laptop Issues and How to Solve them 450 0
Python Selected Interns (Feb 2019 Batch - 2) 974 21
C++ challange for Interns (Feb 2019, Batch 2) 866 24
Select process by PPID 582 17
C++ typeid operator 3751 21
Python UDP client server example code 5581 29
C++ Abstract classes and pure virtual functions 752 23
fatal error: Python.h: No such file or directory 1420 0
Linux Find out how many file descriptors are opened 726 14
C++ Range based for loop 674 18
Binary Tree Traversal 2952 21
Python Command Line Arguments 993 11
How to install Python Requests Library 692 13
Valgrind complete tutorial 1157 10
Function Pointer 620 10
C++ Is paragraph present in the file 678 22
How to cin to list 7570 10
C++11 Auto Example Code 744 14
Shallow Copy 2034 10
C++ File handling 1491 11
C++ challange for Feb 2019 Interns 1064 11
ps man page 728 19
errno.h Error Codes in Linux 840 15
Write copy constructor, assigment operator and desctructor of class having pointer variable 890 10
popen man page 853 15
C++ Read and write binary file 6267 22
C++ Generic file writer class 1188 22
C++ Object Slicing 637 14
OpenSSL generate hash of the file 1960 22
Singleton Class using C++ 11 4568 12
C++ challange for Interns May 2019 1473 21
C++ Pass local variable to Functor 741 10
Nested Template Class 1185 11
Python Sum of all the items in a list 546 11
How to give internship exam at cppsecrets.com 2550 1
C++ Selected Interns (Feb 2019 Batch - 2) 997 19
Run unix command and get output 3199 28
Python Top MOST famous Python libraries and framework 805 29
Linux - find all the shell/terminals are running 851 29
Python challange for May 2019 Interns 1178 15
Linux Select Process By PPID 817 20
C++ File read and write 587 12
C++ Best way to trim std::string 5383 28
Build-in Functors 556 10
C++ Template with default arguments 738 20
How To Move Forward After Losing Your Job Due To Covid 318 0
Truminds 5G UPF now runs in 6WINDGate as a plugin 12656 2312

Comments