Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active December 16, 2020 14:45
Show Gist options
  • Save rpivo/9cb1f91cc8238d5dd8844974d53571a3 to your computer and use it in GitHub Desktop.
Save rpivo/9cb1f91cc8238d5dd8844974d53571a3 to your computer and use it in GitHub Desktop.
Strings in C++

Strings in C++

In JavaScript, strings are a primitive type. In C++, they're a class that can be constructed with different char types depending on the kind of string you want.

#include <string>

string foo = "bar";

In order to use strings, you have to import the string library from std.

Given that string is a class, there are a lot of member functions available to be used right out of the box.

In C, you might use a character array to create a string. It's a statically sized array containing each char you want to represent in your string plus a null character (\0):

char foo[4] = {'b', 'a', 'r', '\0'};

You can also do this in C++, and it's more performant than using a string class, but using a string class has some advantages like mutability, dynamic memory allocation, and built-in helper functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment