C++
C++ . , . -. basic_string. basic_string: string, , wstring, , . string. C++ <string>.
basic_string . , STL . .
basic_string char_traits, , . , , char, wchar_t, basic_string , .
basic_string .
template ,
class Allocator = allocator> class basic_string
CharType - , Attr - , , Allocator . basic_string .
explicit basic_string{const Allocator
&a = Allocator());
basic_string(size_type len, CharType ch,
const Allocator &a = Allocator());
basic_string(const CharType *str,
const Allocator &a = Allocator());
basic_string(const CharType *str, size_type len,
const Allocator &a = Allocator)));
basic_string(const basic_string &str, size_type indx = 0,
size_type len=npos, const Allocator &a = Allocator());
template basic_string(InIter start,
InIter end, const Allocator &a = Allocator());
. , len ch. , , str. , str, ( ) , , len . basic_string, , indx , len . , , , start end.
basic_string :
==, <, <=, !=, > >=.
+, , - << >>, .
+ , -. , .
+ + - - +
+ .
basic_string npos, -1. .
CharType , . , typedef- , . , basic_string.
size_type |
, size_t |
reference |
const- |
const_reference |
const- |
iterator |
|
const_iterator |
const- |
reverse_iterator |
|
const_reverse_iterator |
const- |
value_type |
, |
allocator_type |
|
pointer |
|
const_pointer |
const- |
traits_type |
typedef- char_traits |
difference_type |
, |
-, basic_string. char-( ), string, , wstring( , basic_string).
- |
|
|
reference operator[](size_type indx) const; const_reference operator[](size_type indx) const; |
, indx |
string &operator=(const string &str); string &operator=(const CharType *str); string &operator=(CharType ch); |
. *this |
string &operator+=(const string &str); string &operator+=(const CharType *str); string &operator+=(CharType ch); |
. *this |
|
|
iterator begin(); const_iterator begin() const; |
|
iterator end(); const_iterator end() const; |
, |
reverse_iterator rbegin(); const_reverse_iterator rbegin() const; |
, |
reverse_iterator rend(); const_reverse_iterator rend() const; |
, |
|
string &append(const string &str); |
str . *this |
string &append(const string &str, size_type indx, size_type len); |
str . indx 1 . *this |
string &append(const CharType *str); |
s tr . *this |
string &append(const CharType *str, size_type num); |
num str . *this |
string &append(size_type len, char CharType ch); |
len , ch, . *this |
template string &append(InIter start, InIter end); |
, start end, . *this |
|
string &assign(const string &str); |
str . *this |
string &assign(const string &str, size_type indx, size_type len); |
str . indx len . *this |
string &assign(const CharType *str); |
str . *this |
string &assign(const CharType *str, size_type len); |
len str. *this |
string &assign(size_type len, CharType ch); |
len , ch. *this |
template string &assign{InIter start, InIter end); |
, start end. *this |
|
size_type find(const string &str, size_type indx = 0) const; |
str . indx. , npos |
size_type find(const CharType *str, size_type indx = 0) const; |
str . indx. , npos |
size_type find(const CharType *str, size_type indx, size_type len) const; |
len str . indx. , npos |
size_type find(CharType ch, size_type indx = 0) const; |
ch . indx. , npos |
size_type find_first_of(const string &str, size_type indx = 0) const; |
, str. indx. , npos |
size_type find_first_of(const CharType *str, size_type indx = 0) const; |
, str. indx. , npos |
size_type find_first_of(const CharType *str, size_type indx, size_type len) const; |
, len str. indx. , s |
size_type find_first_of(CharType ch, size_type indx = 0) const; |
ch . indx. , ns |
size_type find_first_not_of(const string &str, size_type indx = 0) const; |
, str. indx. , npos |
size_type find_first_not_of(const CharType *str, size_type indx = 0) const; |
, str. indx. , npos |
size_type find_first_not_of(const CharType *str, size_type indx, size_type len) const; |
, len str. indx. , npos |
size_type find_first_not_of(CharType ch, size_type indx = 0) const; |
, ch. indx. (), npos |
size_type find_last_of(const string &str, size_type indx = npos) const; |
, str. indx. , npos |
size_type find_last_of(const CharType *str, size_type indx = npos) const; |
, str. indx. , npos |
size_type find_last_of(const CharType *str, size_type indx, size_type len) const; |
, len str. indx. , npos |
size_type find_last_of{CharType ch, size_type indx = npos) const; |
ch . indx. , npos |
size_type find_last_not_of(const string &str, size_type indx = , npos) const; |
, str. indx. , npos |
size_type find_last_not_of(const CharType *str, size_type indx = npos) const; |
, str. indx. , npos |
size_type find_last_not_of(const CharType *str, size_type indx, size_type len) const; |
, len str. indx. , npos |
size_type find_last_not_of(CharType ch, size_type indx = npos) const; |
, ch. indx. , npos |
size_type rfind(const string &str, size_type indx = npos) const; |
str . , indx. , npos |
size_type rfind(const CharType *str, size_type indx = npos) const; |
str . , indx. , npos |
size_type rfind(const CharType *str, size_type indx, size_type len) const; |
len str . , indx. , npos |
size_type rfind(CharType ch, size_type indx = npos) const; |
ch . , indx. , npos |
|
int compare(const string &str) const; |
str . : , *this < str; , *this == str; , *this > str |
int compare(size_type indx, size_type len, const string &str) const; |
str . indx len . : , *this < str; , *this == str; , *this > str |
int compare(size_type indx, size_type len, const string &str, size_type indx2, size_type len2) const; |
str . indx len . str indx2 len . : , *this < str; , *this == str; , *this > str |
int compare(const char CharType *str) const; |
str . : , *this < str; , *this == str; , * this > str |
int compare(size_type indx, size_type len, const char CharType *str, size_type len2 = npos) const; |
str . indx len . str len2 . : , *this < str; , *this == str; , *this > str |
|
iterator insert(iterator i, const CharType &ch); |
ch , i. |
string &insert(size_type indx, const string &str); |
str , indx. *this |
string &insert(size_type indx1, const string &str, size_type indx2, size_type len); |
str , indx1. , indx2, len . *this |
string &insert(size_type indx, const CharType *str); |
str , indx. *this |
string &insert(size_type indx, const CharType *str, size_type len); |
len str , indx. *this |
string &insert(size_type indx, size_type len, CharType ch); |
len ch , indx. *this |
void insert(iterator i, size_type len, const CharType &ch); |
len ch , i |
template void insert(iterator i, InIter start, InIter end); |
, start end, , i. |
|
string &replace(size_type indx, size_type len, const string &str); |
len , indx, str. *this |
string &replace(size_type indx1, size_type len1, const string &str, size_type indx2, size_type len2); |
len1 , indxl, ( 1en2) str, indx2. *this |
string &replace(size_type indx, size_type len, const CharType *str); |
len , indx, str. *this |
string &replace(size_type indx1, size_type len1, const CharType *str, size_type len2); |
len1 , indxl, ( len2) str, indx2. *this |
string &replace(size_type indx, size_type len1, size_type len2, CharType ch); |
len1 , indx, ( 12), ch. *this |
string &replace(iterator start, iterator end, const string &str); |
, start end, str. *this |
string &replace(iterator start, iterator end, const CharType *str); |
, start end, str. *this |
string &replace(iterator start, iterator end, const CharType *str, size_type len); |
, start end, len str. *this |
string &replace(iterator start, iterator end, size_type len, CharType ch); |
, start end, ( len), ch. *this |
template string &replace(iterator startl, iterator endl, InIter start2, InIter end2); |
, start1 end1, , start2 end2. *this |
|
iterator erase(iterator i); |
, i. , |
iterator erase(iterator start, iterator end); |
, start end. , |
string &erase(size_type indx =0, size_type len = npos); |
indx, len . *this |
|
size_type copy(CharType *str, size_type len, size_type indx = 0) const; |
indx, len , str. |
|
reference at(size_type indx); const_reference at(size_type indx) const; |
, indx |
const CharType *c_str() const; |
-(.. ) |
size_type capacity() const; |
- , , |
const CharType *data() const; |
|
bool empty(} const; |
true, , false |
allocator_type get_allocator() const; |
|
size_type length() const; |
|
size_type max_size() const; |
, |
void reserve(size_type num = 0); |
num |
void resize(size_type num); void resize(size_type num, CharType ch); |
, , . ( ) , , ch |
size_type size() const; |
|
string substr(size_type indx = 0, size_type Jen = npos) const; |
( ), len , indx |
void swap(string &str); |
str |
, - , C++ . , string ( ), ( ), , . , , .
// .
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "abcdefghijklmnopqrstuvwxyz";
string str2;
string str3(str1);
str2 = str1.substr(10, 5);
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
cout << "str3: " << str3 << endl;
str1.replace(5, 10, "");
cout << "str1.replace(5, 10, \"\"):" << str1 << endl;
str1 = "one";
str2 = "two";
str3 = "three";
cout << "str1.compare(str2): ";
cout << str1.compare(str2) << endl;
if (str1 < str2) cout << "str1 str2\n";
string str4 = str1 + " " + str2 + " " + str3;
cout << "str4: " << str4 << endl;
int i = str4.find("wo");
cout << "str4.substr(i): " << str4.substr(i);
return 0;
}
.
Strl: abcdefghijklmnopqrstuvwxyz
str2: klmno
str3: abcdefghijklmnopqrstuvwxyz
str1.replace(5, 10, ""): abcdepqrstuvwxyz
str1.compare(str2): -1
str1 str2
str4: one two three
str4.substr(i): wo three
. , + , < - . -, strcat() strcmp(). ++ string -, - , .
|