notes on curly braces representing empty vector

This commit is contained in:
Jidong Xiao
2023-09-14 14:36:02 -04:00
parent ab9d28d372
commit c5d3e8fa51

View File

@@ -77,6 +77,19 @@ Here are several different ways to initialize a vector:
```cpp ```cpp
std::vector<int> a; std::vector<int> a;
``` ```
or,
```cpp
std::vector<int> a{};
```
Here, in C++, a pair of curly braces is used to initialize an empty container. Vector is a type of container, and we will learn other containers later this semester. Interesting trick: if you have a function which has a return type of vector, and in your function body, you want to return an empty vector without specifying a vector name, you can do:
```cpp
return {};
```
- This constructs a vector of 100 doubles, each entry storing the value 3.14. New entries can be created using - This constructs a vector of 100 doubles, each entry storing the value 3.14. New entries can be created using
push_back, but these will create entries 100, 101, 102, etc. push_back, but these will create entries 100, 101, 102, etc.
```cpp ```cpp