delete from front in vector c++
// Deleting first element
vector_name.erase(vector_name.begin());
// Deleting xth element from start
vector_name.erase(vector_name.begin()+(x-1));
// Deleting from the last
vector_name.pop_back();
delete from front in vector c++
// Deleting first element
vector_name.erase(vector_name.begin());
// Deleting xth element from start
vector_name.erase(vector_name.begin()+(x-1));
// Deleting from the last
vector_name.pop_back();
vector c++ empty it
// clearing vectors
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
myvector.push_back (100);
myvector.push_back (200);
myvector.push_back (300);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); i++)
std::cout << ' ' << myvector[i];
std::cout << 'n';
myvector.clear(); // making myverctor.empty() == 1
myvector.push_back (1101);
myvector.push_back (2202);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); i++)
std::cout << ' ' << myvector[i];
std::cout << 'n';
return 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;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us