Cpp Standard Library 简明教程

C++ Library - <string>

Introduction

String 是一个类,而字符串中的所有对象都表示字符序列。

String is a class and all objects that in string represent sequences of characters.

Declaration

以下是 std::string 的声明。

Following is the declaration for std::string.

typedef basic_string<char> string;

C++11

typedef basic_string<char> string;

Member types

member type

definition

value_type

char

traits_type

char_traits<char>

allocator_type

allocator<char>

reference

char&

const_reference

const char&

pointer

char*

const_pointer

const char*

iterator

a random access iterator to char (convertible to const_iterator)

const_iterator

a random access iterator to const char

reverse_iterator

reverse_iterator<iterator>

const_reverse_iterator

reverse_iterator<const_iterator>

difference_type

ptrdiff_t

size_type

size_t

Member functions

Sr.No.

Member function & description

1

(constructor)It constructs string object.

2

(destructor)It is a string destructor.

3

[role="bare"]../cpp_standard_library/cpp_string_operator_equal.htmlIt is a string assignment.

Iterators

Sr.No.

Iterator & description

1

beginIt returns iterator to beginning.

2

endIt returns iterator to end.

3

rbeginIt returns reverse iterator to reverse beginning.

4

rendIt returns reverse iterator to reverse end.

5

cbeginIt returns const_iterator to beginning.

6

cendIt returns a const_iterator pointing to the past-the-end character of the string.

7

crbeginIt returns const_reverse_iterator to reverse beginning.

8

crend It returns const_reverse_iterator to reverse end.

Capacity

Sr.No.

Capacity & description

1

sizeIt returns length of string.

2

lengthIt returns length of string.

3

max_sizeIt returns maximum size of string.

4

resizeIt resizes string.

5

capacityIt returns size of allocated storage.

6

reserveIt requests a change in capacity.

7

clearIt clears the string.

8

empty It is used to test if string is empty.

9

shrink_to_fit It is used to shrink to fit.

Capacity

Sr.No.

Element acce & description

1

operator[]It is used to get character of string.

2

atIt is used to get character in string.

3

backIt is used to access last character.

4

frontIt is used to access first character.

Modifiers

Sr.No.

Modifier & description

1

operator+=It appends to string.

2

appendIt appends to string.

3

push_backIt appends a character to string.

4

assignIt is used to assign the content to string.

5

insertIt is used to inset the value to string.

6

eraseIt is used to erase characters from string.

7

replaceIt is used to replace portion of string.

8

swapIt is used to swap string values.

9

pop_backIt is used to delete last character.

String operations

Sr.No.

String operation & description

1

c_strIt is used to get C string equivalent.

2

dataIt is used to get string data.

3

get_allocatorIt is used to get an allocator.

4

copyIt is used to copy sequence of characters from string.

5

findIt is used to find content in string.

6

rfindIt is used to find last occurrence of content in string.

7

find_first_ofIt is used to find character in string.

8

find_last_of It is used to find character in string from the end.

9

find_first_not_ofIt is used to find absence of character in string.

10

find_last_not_of It is used to find non-matching character in string from the end.

11

substrIt is used to generate substring.

12

compareIt is used to compare strings.