Module: Dynamic arrays: vector


Problem

1/8

Vector: Beginning

Theory Click to read/hide

Vectors in C++ (vector)
One kind of dynamic array in C++ is vector (vector)
 
Vector (vector) — it is a data structure that is already a model of a dynamic array.

Ordinary arrays in C++ do not have any special functions and methods for working with them. Vectors in C++ are data structures that contain more additional functions for working with elements.
 
Creating a vector
#include <vector> ... int main() { // declaration of integer vector v for 10 elements   vector <int> v(10);     // same with zero initial values ​​(vector v1)   vector <int> v1(10, 0); ...  
Vector padding
Method 1
We allocate memory for the n-th number of elements and fill them in by reading them from the keyboard.
  intn; cin>> n; vector a(n); for (int i = 0; i < n; i++) cin>> a[i];
Method 2
The second method is needed when the number of elements is unknown. First, an empty vector is created, and then, using the push_back() method, a new element is added to the end of the vector.
  intn; cin>> n; vectora; for (int i = 0; i < n; i++) { intb; cin>> b; a.push_back(b); }
Vector size
int b = a.size();

Problem

Create a vector and fill it with positive elements only.


Input
The first line is the number of elements in the array. The second line contains the elements of the array.
 
Output
Output only positive elements from the sequence.

 
Examples
# Input Output
1 4
2 -4 0 100
2 100