std::string vs C-Style Char Arrays
Compare modern std::string with legacy null-terminated char arrays and decide when to use each.
Two Ways to Hold Text
C++ inherits C-style strings — null-terminated char arrays — and adds std::string from the standard library. Almost always use std::string.
C-Style Strings
A C-style string is a sequence of char ending with a null byte '\0'. Functions like strlen walk the array until they find the null.
const char* greeting = "Hello";
// Memory: H, e, l, l, o, \0 (6 bytes)
#include <cstring>
std::cout << std::strlen(greeting); // 5All lessons in this course
- std::string vs C-Style Char Arrays
- String Operations: concat substr find replace
- String Stream stringstream for Parsing
- String Conversions: to_string and stoi