In C++, a string can be represented either as a string literal with type const char[N] or, more commonly, as an instance of std::string from the standard library. This guide shows how to declare and initialize strings, convert between C-style and std::string, and avoid common pitfalls.
Include the Correct Headers
To use std::string, include the string header. For C string operations, include
#include <string>
#include <iostream>Declare and Initialize a std::string
Declare a string by specifying the type std::string followed by the variable name. You can initialize it with an empty state, a string literal, another string, or multiple copies of a character.
- Default initialization creates an empty string.
- Initialization from a string literal copies the characters up to but not including the null terminator.
- You can copy-initialize with = or direct-initialize with parentheses.
std::string s1; // empty
std::string s2 = "hello"; // copy-initialized from literal
std::string s3("world"); // direct-initialized from literal
std::string s4(3, 'x'); // "xxx"Use std::string Literal for Convenience
By declaring using namespace std::string_literals (or equivalently, using namespace std), you can append s to a string literal and create a std::string directly without explicit std::string construction.
using namespace std::string_literals;
std::string s = "hello"s;C-Style String Declarations
String literals have type const char[N]. You can assign them to a const char* pointer, but prefer std::string for ownership and safety. Arrays declared with fixed sizes store the literal including the null terminator.
const char* ptr = "C-style"; // pointer to literal
const char arr[] = "C-style"; // array copy on the stackConversions and Interoperability
You can convert between std::string and C-style representations using c_str() and, when necessary, data(). Be aware of lifetime: the pointer returned by c_str() remains valid as long as the string is not modified.
std::string a = "example";
const char* c = a.c_str(); // valid until a is modifiedCommon Operations and Best Practices
Use std::string for ownership, concatenation with + or +=, comparison with relational operators, and finding substrings with find. Prefer reserve() when you expect large growth to avoid repeated reallocations. Initialize with the expected size or content when possible.
String Declaration Patterns at a Glance
| Declaration Pattern | Result | Use Case |
|---|---|---|
| std::string s; | Empty string | Default initialization |
| std::string s("text"); | Copy from literal | Direct initialization |
| std::string s = "text"; | Copy-initialized from literal | Copy initialization |
| std::string s(5, 'a'); | "aaaaa" | Repeated character |
| using namespace std::string_literals; std::string s = "text"s; | std::string from literal | Convenience with string literals |
Avoid Common Pitfalls
Do not mix C-style pointers and modifying operations without caution. Calling c_str() and storing the pointer beyond the lifetime of the string leads to undefined behavior. Prefer std::string as the default choice unless interfacing with C APIs or low-level system functions requires a const char*.
Summary
To declare a string in C++, use std::string for safe and flexible text handling. Include