From 9e4c8cce85d27211fb07898d4077de4b5dc5c003 Mon Sep 17 00:00:00 2001 From: Sebastien Gilles <sebastien.gilles@inria.fr> Date: Mon, 20 May 2019 09:45:48 +0200 Subject: [PATCH] Add explanation about c_str(); also commit without cells executed for the notebook. --- 1-ProceduralProgramming/3-Types.ipynb | 190 ++++++++------------------ 1 file changed, 57 insertions(+), 133 deletions(-) diff --git a/1-ProceduralProgramming/3-Types.ipynb b/1-ProceduralProgramming/3-Types.ipynb index cf85b4b..1d5a2f1 100644 --- a/1-ProceduralProgramming/3-Types.ipynb +++ b/1-ProceduralProgramming/3-Types.ipynb @@ -541,26 +541,14 @@ "\n", "In modern C++, rather than bothering with character tables\n", "which come from the C language, it's easier to use the type `std::string`, provided\n", - "through the standard language library:" + "through the standard language library, that provides a much simpler syntax:" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "String 'bonjour' is 7 characters long.\n", - "String 'coucou' is 6 characters long.\n", - "String 'bonjour' is 7 characters long.\n", - "String 'dynamic std::string' is 19 characters long.\n", - "String 'std::string is dynamical and flexible' is 37 characters long.\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "#include <iostream>\n", "#include <cstring> // For strlen\n", @@ -590,6 +578,35 @@ "}" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If needed (for instance to interact with a C library) you may access to the underlying table with `c_str()`:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#include <string>\n", + "\n", + "{\n", + " std::string cplusplus_string(\"C++ string!\");\n", + " \n", + " const char* c_string = cplusplus_string.c_str(); // notice the `const`\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `const` here is important: you may access the content but should not modify it; this functionality is provided for read-only access." + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -608,17 +625,9 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Area = 3.14159265358979\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "#include <iostream>\n", "#include <iomanip> // For std::setprecision\n", @@ -641,17 +650,9 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Area = 3.14159274101257\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "#include <iostream>\n", "#include <iomanip> // For std::setprecision\n", @@ -680,7 +681,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -703,24 +704,9 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 is prime.\n", - "3 is prime.\n", - "5 is prime.\n", - "7 is prime.\n", - "11 is prime.\n", - "13 is prime.\n", - "17 is prime.\n", - "19 is prime.\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "#include <vector>\n", "#include <iostream>\n", @@ -747,26 +733,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 is prime.\n", - "3 is prime.\n", - "5 is prime.\n", - "7 is prime.\n", - "11 is prime.\n", - "13 is prime.\n", - "17 is prime.\n", - "19 is prime.\n" - ] - } - ], + "outputs": [], "source": [ "#include <vector>\n", "#include <iostream>\n", @@ -793,24 +764,9 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 is prime.\n", - "3 is prime.\n", - "5 is prime.\n", - "7 is prime.\n", - "11 is prime.\n", - "13 is prime.\n", - "17 is prime.\n", - "19 is prime.\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "#include <vector>\n", "#include <iostream>\n", @@ -836,24 +792,9 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 is prime.\n", - "3 is prime.\n", - "5 is prime.\n", - "7 is prime.\n", - "11 is prime.\n", - "13 is prime.\n", - "17 is prime.\n", - "19 is prime.\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "#include <vector>\n", "#include <iostream>\n", @@ -881,18 +822,9 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "j and k are of different type.\n", - "i and k are of the same type.\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "#include <algorithm>\n", "#include <iostream>\n", @@ -928,17 +860,9 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "j and k are of the same type.\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "#include <algorithm>\n", "#include <iostream>\n", -- GitLab