From c5d3e8fa51377b14373c5ed202c0a00bd4b34ef7 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 14 Sep 2023 14:36:02 -0400 Subject: [PATCH] notes on curly braces representing empty vector --- lectures/02_strings_vectors/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lectures/02_strings_vectors/README.md b/lectures/02_strings_vectors/README.md index a6b000b..eafa052 100644 --- a/lectures/02_strings_vectors/README.md +++ b/lectures/02_strings_vectors/README.md @@ -77,6 +77,19 @@ Here are several different ways to initialize a vector: ```cpp std::vector a; ``` + +or, + +```cpp +std::vector 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 push_back, but these will create entries 100, 101, 102, etc. ```cpp