How to remove all elements from a vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
 
void printVector(std::vector<int> const &v) {
    for (auto const &n: v) {
        std::cout << n << " ";
    }
}
  
int main ()
{
    std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  
    printVector(v);
    
    v.clear();
    
    v.push_back(734);
    v.push_back(915);

    std::cout << std::endl;
    printVector(v);
  
    return 0;
}
  
  
  
/*
run:
  
1 2 3 4 5 6 7 8 9 
734 915 
   
*/

 



answered Apr 9, 2020 by avibootz

Related questions

1 answer 136 views
1 answer 172 views
1 answer 190 views
1 answer 173 views
1 answer 225 views
2 answers 222 views
...