Skip to content

Latest commit

 

History

History
170 lines (132 loc) · 4.61 KB

std_string.md

File metadata and controls

170 lines (132 loc) · 4.61 KB

The std::string class in C++ is part of the Standard Library and provides a high-level, convenient way to handle strings. It's defined in the <string> header.

Declaration and Initialization

You can declare and initialize std::string in several ways:

std::string str1;               // Empty string
std::string str2 = "Hello";     // Initialization with a C-style string
std::string str3("World");      // Initialization using constructor
std::string str4 = str2;        // Copy initialization
std::string str5(5, 'a');       // Initialize with 5 'a' characters ("aaaaa")

Concatenation

You can concatenate strings using the + and += operators or the append() method:

str1 = str2 + " " + str3;  // "Hello World"
str1 += "!";
str1.append(" How are you?");

Length and Capacity

You can get the length of the string using length() or size():

size_t len = str1.length();

You can also check the capacity and resize the string:

size_t cap = str1.capacity();
str1.resize(50);

Accessing Characters

You can access individual characters using the [] operator or the at() method:

char firstChar = str1[0];
char secondChar = str1.at(1);

Substrings and Finding

To get a substring, you can use the substr() method:

std::string sub = str1.substr(0, 5);  // Gets first 5 characters

To find a substring or a character, you can use find():

size_t pos = str1.find("World");  // Returns position or std::string::npos if not found

Comparison

You can compare strings using relational operators (==, !=, <, >, etc.) or the compare() method:

if (str1 == str2) {
    // Strings are equal
}

Conversion

To convert a std::string to a C-style string, you can use the c_str() method:

const char* cstr = str1.c_str();

To convert numbers to strings or vice versa, you can use std::to_string() and std::stoi():

std::string numStr = std::to_string(42);
int num = std::stoi("42");

String to Number Conversions

Integer (std::stoi, std::stol, std::stoll): Convert to int, long, or long long.

int i = std::stoi("42");
long l = std::stol("42");
long long ll = std::stoll("42");

Floating-Point (std::stof, std::stod, std::stold): Convert to float, double, or long double.

float f = std::stof("42.42");
double d = std::stod("42.42");
long double ld = std::stold("42.42");

Unsigned Integer (std::stoul, std::stoull): Convert to unsigned long or unsigned long long.

unsigned long ul = std::stoul("42");
unsigned long long ull = std::stoull("42");

Iterating

You can iterate through a string using iterators or range-based for loops:

for (char c : str1) {
    // Do something with c
}

Memory Management

std::string manages memory automatically, resizing as needed.

Immutability

Unlike strings in some other languages, std::string objects are mutable. You can change them after they are created.

Miscellaneous Methods

Constructors

  • string() : Default constructor
  • string(const string& str): Copy constructor
  • string(const char* s): Construct from a C-string
  • string(size_t n, char c): Construct by repeating a character n times

Element Access

  • operator[]: Access element
  • at(): Access element, throws out_of_range exception for invalid index
  • front(): Access the first character
  • back(): Access the last character

Modifiers

  • append(): Append to string
  • push_back(): Append a character to the end
  • insert(): Insert into string
  • erase(): Erase a part of string
  • replace(): Replace a part of string
  • swap(): Swap content with another string
  • pop_back(): Removes the last character
  • clear(): Clears the content

Capacity

  • size() / length(): Return length
  • empty(): Check if string is empty
  • resize(): Change size
  • reserve(): Reserve storage
  • capacity(): Get capacity
  • shrink_to_fit(): Shrink to fit

String operations

  • c_str(): Get C string equivalent
  • data(): Get array of characters
  • copy(): Copy substring to C-array
  • find(): Find substring
  • rfind(): Reverse find
  • find_first_of(): Find character from set
  • find_last_of(): Find last character from set
  • find_first_not_of(): Find character not in set
  • find_last_not_of(): Find last character not in set
  • substr(): Generate a substring
  • compare(): Compare strings

Operators

  • operator+: Concatenate strings
  • operator+=: Append to string
  • operator== / operator!=: Compare equality/inequality
  • operator< / operator<= / operator> / operator>=: Lexicographical compare