Try this exercise. Fill in the missing part by typing it in.
To create a vector in C++, you need to include the <vector>
header file. The Vector Library provides a templated vector
class, which can store elements of any type.
Here's an example that demonstrates how to create a vector, initialize it with elements, and access its elements:
1#include <iostream>
2#include <vector>
3
4int main() {
5 // Creating an empty vector
6 std::vector<int> numbers;
7
8 // Adding elements to the vector
9 numbers.push_back(1);
10 numbers.push_back(2);
11 numbers.push_back(3);
12
13 // Accessing elements in the vector
14 std::cout << "The first element is: " << numbers[0] << std::endl;
15 std::cout << "The second element is: " << numbers[1] << std::endl;
16 std::cout << "The third element is: " << numbers[2] << std::endl;
17
18 return 0;
19}
In this example:
- We start by including the necessary headers
iostream
andvector
. - We create an empty vector of integers using the
std::vector<int>
syntax. - We use the
push_back()
method to add elements to the vector. - We access the elements in the vector using the subscript operator
[]
and print them to the console.
The output of the above code will be:
1The first element is: 1
2The second element is: 2
3The third element is: 3
As you can see, we can create a vector, add elements to it, and access those elements using the subscript operator or other vector methods. Creating and manipulating vectors is a fundamental skill when working with the Vector Library.
To create a vector in C++, you need to include the <vector>
header file. The Vector Library provides a templated vector
class, which can store elements of any type.
Here's an example that demonstrates how to create a vector, initialize it with elements, and access its elements:
1#include <iostream>
2#include <vector>
3
4int main() {
5 // Creating an empty vector
6 std::vector<int> numbers;
7
8 // Adding elements to the vector
9 numbers.push_back(1);
10 numbers.push_back(2);
11 numbers.push_back(3);
12
13 // Accessing elements in the vector
14 std::cout << "The first element is: " << numbers[0] << std::endl;
15 std::cout << "The second element is: " << numbers[1] << std::endl;
16 std::cout << "The third element is: " << numbers[2] << std::endl;
17
18 return 0;
19}
In this example:
- We start by including the necessary headers
iostream
andvector
. The blank sections of the above content are ___. We create an empty vector of integers using thestd::vector<int>
syntax. We use thepush_back()
method to add elements to the vector. We access the elements in the vector using the subscript operator[]
and print them to the console.
The output of the above code will be:
1The first element is: 1
2The second element is: 2
3The third element is: 3
As you can see, we can create a vector, add elements to it, and access those elements using the subscript operator or other vector methods. Creating and manipulating vectors is a fundamental skill when working with the Vector Library.
To create a vector in C++, you need to include the <vector>
header file. The Vector Library provides a templated vector
class, which can store elements of any type.
Here's an example that demonstrates how to create a vector, initialize it with elements, and access its elements:
1#include <iostream>
2#include <vector>
3
4int main() {
5 // Creating an empty vector
6 std::vector<int> numbers;
7
8 // Adding elements to the vector
9 numbers.push_back(1);
10 numbers.push_back(2);
11 numbers.push_back(3);
12
13 // Accessing elements in the vector
14 std::cout << "The first element is: " << numbers[0] << std::endl;
15 std::cout << "The second element is: " << numbers[1] << std::endl;
16 std::cout << "The third element is: " << numbers[2] << std::endl;
17
18 return 0;
19}
In this example:
- We start by including the necessary headers
iostream
andvector
. The blank sections of the above content are ___. We create an empty vector of integers using thestd::vector<int>
syntax. We use thepush_back()
method to add elements to the vector. We access the elements in the vector using the subscript operator[]
and print them to the console.
The output of the above code will be:
1The first element is: 1
2The second element is: 2
3The third element is: 3
As you can see, we can create a vector, add elements to it, and access those elements using the subscript operator or other vector methods. Creating and manipulating vectors is a fundamental skill when working with the Vector Library.
Write the missing line below.