Answers for "create empty vector in c++"

C++
1

declare vectors c++

vector<int> vec;
//Creates an empty (size 0) vector
 

vector<int> vec(4);
//Creates a vector with 4 elements.

/*Each element is initialised to zero.
If this were a vector of strings, each
string would be empty. */

vector<int> vec(4, 42);

/*Creates a vector with 4 elements.
Each element is initialised to 42. */


vector<int> vec(4, 42);
vector<int> vec2(vec);

/*The second line creates a new vector, copying each element from the
vec into vec2. */
Posted by: Guest on May-25-2020
0

Empty the vector

//C++ STL program to demonstrate example of
//vector::empty() function

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;

    //printing the size of the vector
    cout << "Total number of elements: " << v1.size() << endl;
    //checking whether vector is empty or not
    if (v1.empty())
        cout << "vector is empty." << endl;
    else
        cout << "vector is not empty." << endl;

    //pushing elements
    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);

    //printing the size of the vector
    cout << "Total number of elements: " << v1.size() << endl;
    //checking whether vector is empty or not
    if (v1.empty())
        cout << "vector is empty." << endl;
    else
        cout << "vector is not empty." << endl;

    return 0;
}
Posted by: Guest on September-06-2021
-1

declare vector of size n in c++

#include <vector>

auto n = 20
// create a vector with n=20 integer elements
std::vector<int> arr(n);
Posted by: Guest on September-13-2020

Browse Popular Code Answers by Language