Take user input into vector in C++ - CodeSpeedy (2024)

In this tutorial, we will get an idea about how to take user input into vector in C++. But before knowing this, we expect that you have some basic idea about the array. As we know that vectors are dynamic arrays which means they can grow and shrink their size according to use. So here we will see the different ways of taking input from the user into vector. We will also learn to take input in a 2D vector. But first, we will learn to take input into a 1D vector from the user.

Different ways of taking input into 1D vector in C++

Example code 1 :

The basic way is if the user will give the size of the vector then we can take input into vector simply using for loop. See the code below to understand it better.

Input: 5 2 9 4 7 2
#include<bits/stdc++.h>using namespace std;//main functionint main() { int n; cin>>n; // taking size of vector from user int a; vector<int> v; // we can also mention size here like // vector<int> v(n); for(int i=0 ; i<n ; i++) { cin>>a; v.push_back(a); } for(auto &p : v) { cout<<p<<" "; } cout<<endl; return 0;}
Output:2 9 4 7 2

Example code 2 :

If the user will not enter the size of the vector but the user wants to enter vector elements as much as they want. So here we will learn how to code it.

Input : 3 9 1 72 3 5 2

#include<bits/stdc++.h>using namespace std;//main functionint main() { int a; vector<int> v; // user can add element as much as they want while(cin>>a) { v.push_back(a); } for(auto &p : v) { cout<<p<<" "; } cout<<endl; return 0;}
Output: 3 9 1 72 3 5 2

Take user input into 2D vector in C++

In a 2D vector, we require the size of row and column from the user but the column size can be different for each row of vector which was not possible in the case of an array. So it has its own benefit in the case when we want to enter different numbers of values in a different row. So let’s learn it using this example code written below.

Input:2 2 2 4 5 2 5 6 6 4
#include<bits/stdc++.h>using namespace std;//main functionint main() { vector<vector<int>> v; int row, column, a; cin>>row; // taking input into 2D vector from user for(int i=0 ; i<row ; i++) { cin>>column; vector<int> p; for(int j=0 ; j<column ; j++) { cin>>a; p.push_back(a); } v.push_back(p); } // displaying 2D vector value for(auto q: v) { for(auto it: q) { cout<<it<<" "; } cout<<endl; } return 0;
Output: 2 4 2 5 6 6 4

Also read:How to break out of nested loops in C++

'); } if (paragraph_for_ad.length > 15) { let adparagraph = paragraph_for_ad[10]; adparagraph.insertAdjacentHTML('afterEnd', '

'); } if (paragraph_for_ad.length > 30) { let adparagraph = paragraph_for_ad[25]; adparagraph.insertAdjacentHTML('afterEnd', '

'); } } else if(window.innerWidth > 1360) { if (paragraph_for_ad.length > 15) { let adparagraph = paragraph_for_ad[10]; // 71161633/article_incontent_1/article_incontent_1 adparagraph.insertAdjacentHTML('afterEnd', '

'); } if (paragraph_for_ad.length > 22) { let adparagraph = paragraph_for_ad[18]; // 71161633/article_incontent_2/article_incontent_2 adparagraph.insertAdjacentHTML('afterEnd', '

'); } if (paragraph_for_ad.length > 30) { let adparagraph = paragraph_for_ad[29]; // 71161633/article_incontent_1/article_incontent_1 adparagraph.insertAdjacentHTML('afterEnd', '

Take user input into vector in C++ - CodeSpeedy (2024)

FAQs

How to take input in vector array in cpp? ›

push_back(): With the help of this function, we can insert an element from the backside of the vector. pop_back(): With the help of this function, we can remove or delete the element from the backside of the vector. insert(): With this function, we can insert the element in the vector at any specified position.

How do you input a vector without size? ›

Initializing a Vector in C++

Unlike static containers like an array, a vector does not need a size to be initialized with. You can initialize a vector without defining the size of the container since it can increase as well as decrease its size dynamically.

How do you write a function that returns a vector in C++? ›

Returning a vector from a function in C++ involves declaring the function's return type as std::vector<T> (where T is the type of elements in the vector) and returning the vector using the return statement. Here's an example: Returning a Vector from a Function: #include <iostream>

Are vectors passed by reference C++? ›

An object of type std::vector can be passed to a function just like any other object. That means if we pass a std::vector by value, an expensive copy will be made. Therefore, we typically pass std::vector by (const) reference to avoid such copies.

What is vector in C++ with example? ›

In C++, vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically. That is, we can change the size of the vector during the execution of a program as per our requirements. Vectors are part of the C++ Standard Template Library.

How do you add to a 2D vector in C++? ›

Adding Elements to a 2-D Vector

The C++ STL's push_back() method can be used to insert elements into a vector. The insertion operation in a vector of vectors is shown in the example below. The push_back() function is used in the code to construct a 2D vector displayed alongside the matrix.

How do you write a vector answer? ›

The vector form is used to represent a line or a plane in a three-dimensional space. The point P(x, y, z) in a cartesian plane can be represented as a vector line →OP=x→i+y→j+z→k O → P = x i → + y j → + z k → .

How does vector size () work? ›

size() count each element of a vector object whenever it's called and return this value, or does the vector object have a (size_t?) member that holds the number of elements currently in the vector, and the value of this member gets returned by .

What is vector back () in C++? ›

vector::back() returns a reference that points to the last element of the vector.

What is V back () in vector C++? ›

The .back() returns a reference to the last element of the vector: // The last element of a vector. std::cout << order.

What C++ library is vector in? ›

Vectors in C++ are a simple and effective way of storing data and keeping it organized. Vectors, or std::vector , are a template class in the STL (Standard Template Library).

How to declare a vector in C++ class? ›

Every new vector must be declared starting with the vector keyword. This is followed by angle brackets which contain the the type of data the vector can accept like strings, integers, and so on. Lastly, the vector name - we can call this whatever we want.

What does & symbol mean in C++? ›

When programmers use the & operator as a binary operator, it performs a bitwise AND operation between corresponding bits of two operands. It means the & operator compares every bit of the operands one by one (the first bit of the first operand with the first bit of the second).

How to accept array input in cpp? ›

How to Take Input in Array in C++?
  1. Arrays in C++ are derived data types that can contain multiple elements of the same data type. ...
  2. But instead of accessing a single element to take input in an array, we use a loop to iterate over the array elements and take input from the user for each element.
Oct 5, 2023

How to take input in dynamic array in cpp? ›

Dynamic arrays in C++ are declared using the new keyword. We use square brackets to specify the number of items to be stored in the dynamic array. Once done with the array, we can free up the memory using the delete operator. Use the delete operator with [] to free the memory of all array elements.

How to take input in C using array? ›

Input data into the array in C

If the array size is four, we iterate from index=0 to index=3 in the array. We have written a printf statement(to display the output) in the For loop. So the message Enter number will print four times, and then we have written scanf statement(to input the values).

Top Articles
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 6250

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.