To create an empty vector in C++, you can use the std::vector
class from the Standard Template Library (STL).
In this example,
header at the beginning of the program to use the std::vector
class.std::vector
declaration to define a vector that will hold integers.empty_vector
and initialize it using the default constructor, which creates an empty vector.size()
member function to confirm that it is empty. The size()
function returns the number of elements in the vector.std::cout
to output the size of the vector to the console.#include <vector>
#include <iostream>
int main() {
std::vector<int> empty_vector;
std::cout << "Size of empty vector: " << empty_vector.size() << std::endl;
return 0;
}
Size of empty vector: 0
In this example,
header at the beginning of the program to use the std::vector
class, and the
header to use the std::string
class.std::vector
declaration to define a vector that will hold strings.empty_string_vector
and initialize it using the default constructor, which creates an empty vector.size()
member function to confirm that it is empty. The size()
function returns the number of elements in the vector.std::cout
to output the size of the vector to the console.#include <vector>
#include <string>
#include <iostream>
int main() {
std::vector<std::string> empty_string_vector;
std::cout << "Size of empty string vector: " << empty_string_vector.size() << std::endl;
return 0;
}
Size of empty string vector: 0
In this tutorial, we learned How to create an Empty Vector in C++ language with well detailed examples.