From 455779fa81db5178e1d21708e4a6d8b288dede5c Mon Sep 17 00:00:00 2001 From: Amith Kumar Date: Sun, 6 Jun 2021 14:03:14 +0530 Subject: [PATCH] Update README.md --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index bbd7003..ae86c21 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ small = min({x, y, z, k}); // life is easy ``` ## JavaScript like Destructuring using Structured Binding in C++ + +### Binding by value: ```cpp pair cur = {1, 2}; auto [x, y] = cur; @@ -36,8 +38,22 @@ auto [x, y] = cur; array arr = {1, 0, -1}; auto [a, b, c] = arr; // a is now 1, b is now 0, c is now -1 +a++; +// a is now 2. arr[0] is unaffected, remains 1. +``` + +### Binding by reference: +```cpp +array arr = {1, 0, -1}; +auto& [a, b, c] = arr; +// a is now 1, b is now 0, c is now -1 +a++; +// a is now 2, arr[0] is also 2. ``` +**Note:** Structured binding cannot be done with vectors since vectors are dynamic. + + ----------------