diff --git a/5-UsefulConceptsAndSTL/3-Containers.ipynb b/5-UsefulConceptsAndSTL/3-Containers.ipynb index e359685baf9a1828669694a7dbc8fc00df856ab4..c95b2c5812591576cc3ef679f0a675e1b10e3a77 100644 --- a/5-UsefulConceptsAndSTL/3-Containers.ipynb +++ b/5-UsefulConceptsAndSTL/3-Containers.ipynb @@ -713,10 +713,32 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "## Access a container element - Python like syntax" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#include <iostream>\n", + "#include <vector>\n", "\n", - "\n", - "\n", - "\n", + "std::vector<int> v {1, // can be accessed with begin()[0] or end()[-4]\n", + " 2, // can be accessed with begin()[1] or end()[-3]\n", + " 3, // can be accessed with begin()[2] or end()[-2]\n", + " 4 // can be accessed with begin()[3] or end()[-1]\n", + " };\n", + "std::cout << v.end()[-2] << \" - \" << v.begin()[1] << std::endl;\n", + "// Displays '3 - 2'\n", + "// But also some weird pointer value (something like @0x7ffff91e0de0) with Xeus-cling. Just forget about it, it looks like a Xeus-cling bug." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "## Other containers\n", "\n", "`std::vector` is not the only possible choice; I will present very briefly the other possibilities here:\n",