The <string> header in C++ is a fundamental part of the Standard Library, providing facilities for working with strings. This tutorial will cover the key features and functionalities of the std::string class, including its constructors, methods, and best practices for string manipulation.
std::string is a sequence container that represents arrays of characters and is part of the Standard Template Library (STL). It provides a high-level interface for manipulating strings, offering both performance and ease of use. The std::string class is defined in the <string> header.
#include <iostream>
#include <string>
int main() {
std::string str; // Creates an empty string
return 0;
}
std::string str1 = "Hello";
std::string str2(str1); // Copies the contents of str1 to str2
std::string createString() {
return std::string("Move this string");
}
int main() {
std::string str3 = createString(); // Move semantics are used here
return 0;
}
std::string str4("Hello, World!"); // Initializes with a C-style string literal
std::string str5(str1, 7, 5); // Creates "World" from str1 starting at index 7 and length 5
operator[]: Provides access to characters by index.at(): Similar to operator[], but throws an exception if the index is out of range.std::string str6 = "Hello";
char c1 = str6[0]; // 'H'
char c2 = str6.at(0); // 'H'
append(): Appends a string to the end.push_back(): Adds a single character at the end.str6.append(" World!"); // "Hello World!"
str6.push_back('!'); // "Hello World!!"
pop_back(): Removes the last character.clear(): Erases all characters in the string.str6.pop_back(); // "Hello World!"
str6.clear(); // ""
compare(): Compares two strings lexicographically.std::string str7 = "Apple";
int result = str7.compare("Banana"); // Negative value, since 'A' < 'B'
find(): Searches for a substring.replace(): Replaces a portion of the string.std::string str8 = "Hello, World!";
size_t pos = str8.find("World"); // 7
str8.replace(pos, 5, "Universe"); // "Hello, Universe!"
substr(): Extracts a substring.std::string subStr = str8.substr(0, 5); // "Hello"
std::string Over C-style Strings: std::string provides automatic memory management and is safer to use.at() over operator[] when bounds checking is necessary.std::string_view for Read-Only Access: When you need a lightweight, non-owning view of a string.#include <iostream>
#include <string>
void processString(std::string str) {
// Process the string
std::cout << "Processing: " << str << std::endl;
}
int main() {
std::string input = "Hello, World!";
processString(input); // Pass by value to allow modifications
const std::string_view view = input; // Use string_view for read-only access
std::cout << "Viewing: " << view << std::endl;
return 0;
}
In this example, processString takes a copy of the string, allowing it to modify the input without affecting the original. The std::string_view is used for read-only access, which is more efficient than copying the entire string.
The <string> header in C++ provides a powerful and flexible way to handle strings. By understanding its constructors, methods, and best practices, you can write efficient and safe C++ code that manipulates strings effectively.