How to initialize a vector with zeros in C++

2 Answers

0 votes
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v(10, 0);
    
    for (int val : v)
        std::cout << val << " ";
}



/*
run:

0 0 0 0 0 0 0 0 0 0 

*/

 



answered Sep 9, 2022 by avibootz
0 votes
#include <iostream>
#include <vector>

int main()
{
    std::vector<double> v(10, 0);
    
    for (double val : v)
        std::cout << val << " ";
}



/*
run:

0 0 0 0 0 0 0 0 0 0 

*/

 



answered Sep 9, 2022 by avibootz
...