Declaration and Initialization:
#include <iostream>
#include <string>
int main() {
// Declaration and initialization
std::string helloString = "Hello, std::string!";
// Printing the string
std::cout << helloString << std::endl;
return 0;
}
std::string
is part of the C++ standard library's <string>
header. Its declaration is clean and straightforward, and it automatically manages memory, freeing you from the burden of manual memory allocation.
Dynamic Resizing:
#include <iostream>
#include <string>
int main() {
// Dynamic resizing
std::string dynamicString = "Dynamic ";
dynamicString += "Resizing!";
// Printing the result
std::cout << dynamicString << std::endl;
return 0;
}
One of the standout features of std::string
is its ability to dynamically resize itself as you perform operations like concatenation, eliminating the need for explicit memory management.
Length Retrieval with length
and size
:
#include <iostream>
#include <string>
int main() {
std::string myString = "Length and Size";
// Retrieving length
std::cout << "Length: " << myString.length() << std::endl;
// Retrieving size
std::cout << "Size: " << myString.size() << std::endl;
return 0;
}
Both length()
and size()
member functions provide the length of the string, giving you flexibility in choosing the method you find more readable.
Accessing Characters:
#include <iostream>
#include <string>
int main() {
std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
// Accessing individual characters
char thirdChar = alphabet[2]; // 'c'
char lastChar = alphabet.back(); // 'z'
// Printing the result
std::cout << "Third Character: " << thirdChar << std::endl;
std::cout << "Last Character: " << lastChar << std::endl;
return 0;
}
Accessing individual character in a std::string
is intuitive using array like syntax or dedicated member functions like back()
.
Substrings with substr
:
#include <iostream>
#include <string>
int main() {
std::string longString = "Extracting Substrings";
// Extracting a substring
std::string substring = longString.substr(12, 5); // "Substring"
// Printing the result
std::cout << "Extracted Substring: " << substring << std::endl;
return 0;
}
The substr
function allows you to extract a substring from a std::string
with ease, specifying the starting index and length.
String Comparison with Operators:
#include <iostream>
#include <string>
int main() {
std::string string1 = "apple";
std::string string2 = "banana";
// String comparison
bool isEqual = (string1 == string2);
// Printing the result
std::cout << "Strings are equal: " << std::boolalpha << isEqual << std::endl;
return 0;
}
std::string
supports standard comparison operators (==
, !=
, <
, <=
, >
, >=
), providing a natural way to compare strings.
The Elegance of Modern String Handling
1. String Concatenation:
#include <iostream>
#include <string>
int main() {
std::string part1 = "Hello, ";
std::string part2 = "C++!";
// Concatenation
std::string greeting = part1 + part2;
// Printing the result
std::cout << greeting << std::endl;
return 0;
}
2. Input and Output Operations:
#include <iostream>
#include <string>
int main() {
std::string part1 = "Hello, ";
std::string part2 = "C++!";
// Concatenation
std::string greeting = part1 + part2;
// Printing the result
std::cout << greeting << std::endl;
return 0;
}
3. C++11 Raw String Literals:
#include <iostream>
#include <string>
int main() {
// C++11 raw string literals
std::string rawString = R"(This is a raw string literal
It can span multiple lines
without escape characters)";
// Printing the result
std::cout << rawString << std::endl;
return 0;
}