Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ standard library

The C++ standard library

The C++ Standard Library (STL) is one of the core components of C++, providing rich data structures and algorithms.

is the header file in the C++ standard library for handling strings.

In C++, a string is a sequence composed of characters. The header provides the std::string class, which is a wrapper around C-style strings, providing safer and easier-to-use string manipulation functions.

To use the library in a C++ program, you first need to include this header:

#include 
#include 

Basic Syntax

std::string The basic syntax of the class is as follows:

std::string str2 = "World!"; std::string result = str1 + str2;

Common member functions

std::string The class provides many member functions to manipulate strings; here are some commonly used ones:

Example

Below is an example using simple example of the library, including output results:

Example

#include

#include

int

main

(

)

{

   

// Declare and initialize a string

    std

::

string

greeting

=

"Hello, World!"

;

    std

::

cout

<<

"Greeting: "

<<

greeting

<<

std

::

endl

;

   

// Use size() to get the string length

    std

::

cout

<<

"Length of the greeting: "

<<

greeting.

size

(

)

<<

std

::

endl

;

   

// Use empty() to check whether the string is empty

    std

::

cout

<<

"Is the greeting empty? "

<<

(

greeting.

empty

(

)

?

"Yes"

:

"No"

)

<<

std

::

endl

;

   

// Use operator[] to access a character at a specific position

    std

::

cout

<<

"Character at position 7: "

<<

greeting

[

7

]

<<

std

::

endl

;

   

// Use substr() to get a substring

    std

::

string

sub

=

greeting.

substr

(

7

,

5

)

;

    std

::

cout

<<

"Substring from position 7 with length 5: "

<<

sub

<<

std

::

endl

;

   

// Use find() to find a substring

    std

::

cout

<<

"Position of 'World' in the greeting: "

<<

greeting.

find

(

"World"

)

<<

std

::

endl

;

   

// Use replace() to replace part of the string

   

// Replace 'World' with 'C++'

    std

::

string

modified

=

greeting

;

    std

::

string

::

size_type

pos

=

modified.

find

(

"World"

)

;

   

if

(

pos

!

=

std

::

string

::

npos

)

{

        modified.

replace

(

pos,

5

,

"C++"

)

;

// Starting from position pos, replace 5 characters with "C++"

   

}

    std

::

cout

<<

"Modified greeting: "

<<

modified

<<

std

::

endl

;

   

return

0

;

}

Output result:

Length of the greeting: 13
Is the greeting empty? No
Character at position 7: W
Substring from position 7 with length 5: World
Position of 'World' in the greeting: 7
Modified greeting: Hello, C++!

std::string member function summary table

Here is a summary of common std::string member functions:

Function name Description Example Code
size() Returns the length of the string (number of characters). std::cout << str.size();
length() The difference between size() Same as that, returns the length of the string. std::cout << str.length();
empty() Determines whether the string is empty. std::cout << (str.empty() ? "Yes" : "No");
operator[] Accesses the character at the specified position in the string. std::cout << str[0];
at() Accesses the character at the specified position in the string (with bounds checking). std::cout << str.at(0);
substr() Returns a substring starting from the specified position. std::string sub = str.substr(0, 5);
find() Finds the position of a substring in the string. std::cout << str.find("sub") << std::endl;
rfind() Finds the position of a substring starting from the end of the string. std::cout << str.rfind("sub") << std::endl;
replace() Replaces part of the string. str.replace(pos, length, "new_substring");
append() Appends content at the end of the string. str.append(" more");
insert() Inserts content at the specified position. str.insert(pos, "inserted");
erase() Deletes characters or a substring at the specified position. str.erase(pos, length);
clear() Clears the string. str.clear();
c_str() Returns a C-style string (null-terminated). const char* cstr = str.c_str();
data() Returns a pointer to the character data (C++11 and later). const char* data = str.data();
compare() Compares two strings. int result = str.compare("other");
find_first_of() Finds the position of the first match of any character. size_t pos = str.find_first_of("aeiou");
find_last_of() Finds the position of the last match of any character. size_t pos = str.find_last_of("aeiou");
find_first_not_of() Finds the position of the first non-matching any character. size_t pos = str.find_first_not_of("aeiou");
find_last_not_of() Finds the position of the last non-matching any character. size_t pos = str.find_last_not_of("aeiou");

Example

#include

#include

int

main

(

)

{

    std

::

string

str

=

"Hello, World!"

;

   

   

// size()

    std

::

cout

<<

"Length: "

<<

str.

size

(

)

<<

std

::

endl

;

   

// empty()

    std

::

cout

<<

"Is empty? "

<<

(

str.

empty

(

)

?

"Yes"

:

"No"

)

<<

std

::

endl

;

   

// operator[]

    std

::

cout

<<

"First character: "

<<

str

[

0

]

<<

std

::

endl

;

   

// at()

    std

::

cout

<<

"Character at position 7: "

<<

str.

at

(

7

)

<<

std

::

endl

;

   

// substr()

    std

::

string

sub

=

str.

substr

(

7

,

5

)

;

    std

::

cout

<<

"Substring from position 7 with length 5: "

<<

sub

<<

std

::

endl

;

   

// find()

   

size_t

pos

=

str.

find

(

"World"

)

;

    std

::

cout

<<

"Position of 'World': "

<<

pos

<<

std

::

endl

;

   

// replace()

    str.

replace

(

pos,

5

,

"C++"

)

;

    std

::

cout

<<

"Modified string: "

<<

str

<<

std

::

endl

;

   

// append()

    str.

append

(

" How are you?"

)

;

    std

::

cout

<<

"Appended string: "

<<

str

<<

std

::

endl

;

   

// insert()

    str.

insert

(

7

,

" Beautiful"

)

;

    std

::

cout

<<

"String after insert: "

<<

str

<<

std

::

endl

;

   

// erase()

    str.

erase

(

7

,

10

)

;

    std

::

cout

<<

"String after erase: "

<<

str

<<

std

::

endl

;

   

// clear()

    str.

clear

(

)

;

    std

::

cout

<<

"String after clear: "

<<

(

str.

empty

(

)

?

"Empty"

:

"Not empty"

)

<<

std

::

endl

;

   

// c_str()

    str

=

"Hello, C++!"

;

   

const

char

cstr

=

str.

c_str

(

)

;

    std

::

cout

<<

"C-style string: "

<<

cstr

<<

std

::

endl

;

   

// compare()

   

int

cmp

=

str.

compare

(

"Hello, C++!"

)

;

    std

::

cout

<<

"Comparison result: "

<<

cmp

<<

std

::

endl

;

   

// find_first_of()

   

size_t

pos_first_vowel

=

str.

find_first_of

(

"aeiou"

)

;

    std

::

cout

<<

"First vowel at position: "

<<

pos_first_vowel

<<

std

::

endl

;

   

// find_last_of()

   

size_t

pos_last_vowel

=

str.

find_last_of

(

"aeiou"

)

;

    std

::

cout

<<

"Last vowel at position: "

<<

pos_last_vowel

<<

std

::

endl

;

   

// find_first_not_of()

   

size_t

pos_first_non_vowel

=

str.

find_first_not_of

(

"aeiou"

)

;

    std

::

cout

<<

"First non-vowel at position: "

<<

pos_first_non_vowel

<<

std

::

endl

;

   

// find_last_not_of()

   

size_t

pos_last_non_vowel

=

str.

find_last_not_of

(

"aeiou"

)

;

    std

::

cout

<<

"Last non-vowel at position: "

<<

pos_last_non_vowel

<<

std

::

endl

;

   

return

0

;

}

Output result:

Length: 13
Is empty? No
First character: H
Character at position 7: W
Substring from position 7 with length 5: World
Position of 'World': 7
Modified string: Hello, C++!
Appended string: Hello, C++! How are you?
String after insert: Hello,  BeautifulC++! How are you?
String after erase: Hello, C++! How are you?
String after clear: Empty
C-style string: Hello, C++!
Comparison result: 0
First vowel at position: 1
Last vowel at position: 4
First non-vowel at position: 0
Last non-vowel at position: 10