[go: up one dir, main page]

0% found this document useful (0 votes)
12 views2 pages

Characteristics of String in C++

The document outlines the characteristics of std::string, highlighting its dynamic size, built-in member functions, and operator overloading for intuitive string manipulation. It also emphasizes automatic memory management and compatibility with C-style strings. An example is provided to demonstrate string concatenation, substring extraction, and searching within a string.

Uploaded by

landeparth93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Characteristics of String in C++

The document outlines the characteristics of std::string, highlighting its dynamic size, built-in member functions, and operator overloading for intuitive string manipulation. It also emphasizes automatic memory management and compatibility with C-style strings. An example is provided to demonstrate string concatenation, substring extraction, and searching within a string.

Uploaded by

landeparth93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Characteristics of std::string

1. Dynamic Size: Unlike C-style strings (character arrays), std::string can grow or
shrink dynamically as you add or remove characters.
2. Member Functions: The std::string class comes with many built-in methods,
including:
o length() or size(): Returns the number of characters in the string.
o append(): Adds characters to the end of the string.
o substr(): Extracts a substring.
o find(): Searches for a specific character or substring.
o insert(): Inserts characters at a specified position.
o erase(): Removes characters from the string.
o c_str(): Returns a pointer to a C-style string (null-terminated).
3. Operator Overloading: You can use operators to concatenate strings, compare them,
and more, which makes string manipulation intuitive.

cpp
Copy code
std::string str1 = "Hello, ";
std::string str2 = "World!";
std::string result = str1 + str2; // Concatenation

4. Memory Management: The std::string class automatically handles memory


allocation and deallocation, reducing the chances of memory leaks and buffer
overflows.
5. Compatibility with C-style Strings: You can convert between std::string and C-
style strings easily using the c_str() method for compatibility with functions that
require C-style strings.

Example of Using std::string

Here’s a simple example demonstrating some features of the std::string class:

cpp
Copy code
#include <iostream>
#include <string>

int main() {
std::string greeting = "Hello";
greeting += ", World!"; // Concatenation

std::cout << greeting << std::endl; // Output: Hello, World!

// Finding a substring
size_t pos = greeting.find("World");
if (pos != std::string::npos) {
std::cout << "'World' found at position: " << pos << std::endl;
}

// Substring
std::string sub = greeting.substr(7, 5); // Extract "World"
std::cout << "Substring: " << sub << std::endl;
return 0;
}

You might also like