Answers for "C++ Vector clear"

9

how to create a vector in c++

// First include the vector library:
#include <vector>

// The syntax to create a vector looks like this:
std::vector<type> name;

// We can create & initialize "lol" vector with specific values:
std::vector<double> lol = {66.666, -420.69};

// it would look like this: 66.666 | -420.69
Posted by: Guest on February-29-2020
0

C++ Vector clear syntax

vector<T>().swap(x);   // clear x reallocating
Posted by: Guest on April-22-2021
0

C++ Vector clear

// 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();
  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;
}
Posted by: Guest on April-22-2021
0

clear vs erase vector c++

//Syntax
vectorname.clear()
vectorname.erase(startingposition, endingposition)
  
clear() removes all the elements from a vector container, thus making its 
size 0. All the elements of the vector are removed using clear() function. 
  
erase() function, on the other hand, is used to remove specific elements from 
the container or a range of elements from the container, thus reducing its
size by the number of elements removed.
Posted by: Guest on May-10-2021

Browse Popular Code Answers by Language