constructors
string();
string( size_type length, char ch );// length个ch
string( const char *str );// 以str为初始值,长度任意
string( const char *str, size_type length );// 以str为初始值,长度为length
string( string &str, size_type index, size_type length );/ 以str的index开始,长度为length的子串
string( input_iterator start, input_iterator end );// 以start到end之间的元素为初始值
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1;
cout << "s1:" << s1 << endl;
string s2(5, 'a');
cout << "s2:" << s2 << endl;
string s3("abc");
cout << "s3:" << s3 << endl;
string s4("abc", 2);
cout << "s4:" << s4 << endl;
string s5("hello world", 6, 5);
cout << "s5:" << s5 << endl;
vector<char> v(3, 'a');
string s6(v.begin(), v.end());
cout << "s6:" << s6 << endl;
return 0;
}
operators
/*
* 用 ==, >, <, >=, <=, != 进行字符串的比较
* 用 +, += 连接多个字符串
* 用 [] 获取字符串指定位置的字符
*/
==
>
<
>=
<=
!=
+
+=
[]
append
basic_string &append( const basic_string &str );// 在字符串末尾添加str
basic_string &append( const char *str );// 在字符串末尾添加str
basic_string &append( const basic_string &str, size_type index, size_type len );// 在字符串末尾添加str的以index开始,长度为len的子串
basic_string &append( const char *str, size_type num ); // 在字符串末尾添加num个
basic_string &append( size_type num, char ch ); // 在字符串末尾添加num个ch
basic_string &append( input_iterator start, input_iterator end );// 在字符串末尾添加start到end之间的元素
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s = "hello ";
cout << s.append("world") << endl;
cout << s.append("hello world", 6, 5) << endl;
cout << s.append("world", 3) << endl;
cout << s.append(5, 'a') << endl;
std::vector<char> v(3, 'b');
cout << s.append(v.begin(), v.end()) << endl;
return 0;
}
assign
basic_string &assign( const basic_string &str );// 用str给字符串赋值
basic_string &assign( const char *str );// 用str给字符串赋值
basic_string &assign( const char *str, size_type num );//用str开始的num个字符给字符串赋值
basic_string &assign( const basic_string &str, size_type index, size_type len ); // 用str,以index开始,长度为len的子串给字符串赋值
basic_string &assign( size_type num, char ch );// 用num个ch给字符串赋值
at
reference at( size_type index );// 返回一个引用,指向index位置的字符,等同操作符[]
begin
iterator begin(); // 返回一个迭代器,指向字符串的第一个元素
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s = "hello";
string::iterator it;
for(it = s.begin(); it != s.end(); it++)
{
cout << *it << endl;
}
return 0;
}
c_str
const char *c_str();//返回一个指向正规C字符串的指针, 内容与本字符串相同
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s = "hello";
//char *p = s.c_str(); //error: invalid conversion from ‘const char*’ to ‘char*’
const char *p = s.c_str();
cout << *(p+1) << endl;
return 0;
}
// 结果:e
capacity
size_type capacity();// 返回在重新申请更多空间之前,字符串可以容纳的字符数,至少和size()一样大
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s("hello", 10);
cout << "+" << s << endl;// s第6到10的字符的具体内容是不能确定的。
cout << s.size() << endl;
cout << s.capacity() << endl;
s.reserve(20);// 预留20个字符的空间
cout << s.size() << endl;
cout << s.capacity() << endl;
}
compare
/*
* 比较方法:依次根据每个位置字符的ASCII码进行比较
* 返回值 情况
* 小于零 this < str
* 零 this == str
* 大于零 this > str
*/
int compare( const basic_string &str );
int compare( const char *str );
int compare( size_type index, size_type length, const basic_string &str );// 以自己index开始,长度为length的子串,与str进行比较
int compare( size_type index, size_type length, const basic_string &str, size_type index2, size_type length2 );
int compare( size_type index, size_type length, const char *str, size_type length2 );
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s("hello", 10);
cout << s.compare("hella") << endl;
cout << s.compare("hellp") << endl;
cout << s.compare(3, 2, "lo") << endl;
cout << s.compare(3, 2, "follow", 3, 2) << endl;
cout << s.compare(3, 2, "loudly", 2) << endl;
cout << s.compare(3, 2, "loudly", 4) << endl;
cout << s.compare(3, 2, "loudly", 0) << endl;
}
copy
size_type copy( char *str, size_type num, size_type index ); // copy()函数拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s = "hello world";
char buf[5];
int num = s.copy(buf, 10, 6);
cout << num << endl;
cout << buf << endl;
}
// 结果: 5 world
data
const char *data(); // 返回指向自己的第一个字符的指针.
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s = "hello world";
const char *p = s.data();
cout << *(p+1) << endl;
}// 结果为e
empty
const char *data(); // 返回指向自己的第一个字符的指针.
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
string s2 = "";
string s3;
cout << s1.empty() << endl;
cout << s2.empty() << endl;
cout << s3.empty() << endl;
}
// 结果: 0 1 1
end
iterator end(); //返回一个迭代器,指向字符串的末尾(最后一个字符的下一个位置).
erase
iterator erase( iterator pos );// 删除pos指向的字符,返回下一个字符的迭代器
iterator erase( iterator start, iterator end );// 删除从start到end的所有字符,返回一个迭代器,指向被删除的最后一个字符的下一个位置
basic_string &erase( size_type index = 0, size_type num = npos );// 删除从index开始的num个字符,返回*this
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
s1.erase(3, 2);
cout << s1 << endl;
string s2 = "hello world";
s2.erase(3);
cout << s2 << endl;
string s3 = "hello world";
s3.erase();
cout << s3 << endl;
string s4 = "hello world";
string::iterator it;
for(it = s4.begin(); it != s4.end(); )
{
if(*it == 'l')
{
it = s4.erase(it);
}
else
{
it++;
}
}
cout << s4 << endl;
string s5 = "hello world";
s5.erase(s5.begin() + 1, s5.end() - 1);
cout << s5 << endl;
return 0;
}
find
size_type find( const basic_string &str, size_type index );// 返回str在字符串中第一次出现的位置(从index开始查找),如果没找到则返回string::npos;
size_type find( const char *str, size_type index );
size_type find( const char *str, size_type index, size_type length );
size_type find( char ch, size_type index );// 返回字符ch在字符串中第一次出现的位置(从index开始查找),如果没找到则返回string::npos;
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
cout << s1.find("lo", 0) << endl;
cout << s1.find("lo", 5) << endl; // string::npos
cout << s1.find("o") << endl;
return 0;
}
find_first_of
size_type find_first_of( const basic_string &str, size_type index = 0 );// 查找字符串中第一个与str中某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到则返回string::npos
size_type find_first_of( const char *str, size_type index = 0 );
size_type find_first_of( const char *str, size_type index, size_type num );// 查找字符串中第一个与str中某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到则返回string::npos
size_type find_first_of( char ch, size_type index = 0 );// 查找字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
cout << s1.find_first_of("efgh") << endl;
cout << s1.find_first_of("efgh", 1) << endl;
cout << s1.find_first_of('o', 6) << endl;
return 0;
}
// 结果: 0 1 7
find_fisrt_not_of
size_type find_first_not_of( const basic_string &str, size_type index = 0 );// 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_type find_first_not_of( const char *str, size_type index = 0 );
size_type find_first_not_of( const char *str, size_type index, size_type num );// 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符。如果没找到就返回string::nops
size_type find_first_not_of( char ch, size_type index = 0 );// 在字符串中查找第一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
find_last_of
size_type find_last_of( const basic_string &str, size_type index = npos );// 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_type find_last_of( const char *str, size_type index = npos );
size_type find_last_of( const char *str, size_type index, size_type num );// 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::nops
size_type find_last_of( char ch, size_type index = npos );// 字符串中查找最后一个与ch匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
cout << s1.find_last_of("lo") << endl;
cout << s1.find_last_of("lo", 4) << endl;
cout << s1.find_last_of("lo", 4, 2) << endl;
cout << s1.find_last_of('o', 6) << endl;
return 0;
}
// 结果: 9 4 4 4
find_last_not_of
size_type find_last_not_of( const basic_string &str, size_type index = npos );// 在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_type find_last_not_of( const char *str, size_type index = npos); // 在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符如果没找到就返回string::nops
size_type find_last_not_of( const char *str, size_type index, size_type num );
size_type find_last_not_of( char ch, size_type index = npos );// 在字符串中查找最后一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
get_allocator
allocator_type get_allocator();// 字符串的配置器
insert
iterator insert( iterator i, const char &ch ); // 在迭代器i表示的位置前面插入一个字符ch
basic_string &insert( size_type index, const basic_string &str );// 在字符串的位置index插入字符串str
basic_string &insert( size_type index, const char *str );
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );// 在字符串的位置index插入字符串str的子串(从index2开始,长num个字符)
basic_string &insert( size_type index, const char *str, size_type num );// 在字符串的位置index插入字符串str的num个字符
basic_string &insert( size_type index, size_type num, char ch );// 在字符串的位置index插入num个字符ch的拷贝
void insert( iterator i, size_type num, const char &ch );// 在迭代器i表示的位置前面插入num个字符ch的拷贝
void insert( iterator i, iterator start, iterator end );// 在迭代器i表示的位置前面插入一段字符,从start开始,以end结束
length
size_type length();// 函数返回字符串的长度. 这个数字应该和size()返回的数字相同
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
cout << s1.length() << endl;
cout << s1.size() << endl;
return 0;
}
// 结果: 11 11
max_size
size_type max_size(); // 返回字符串能保存的最大字符数
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
cout << s1.max_size() << endl;
cout << s1.size() << endl;
return 0;
}
rbegin
const reverse_iterator rbegin();// 一个逆向迭代器,指向字符串的最后一个字符
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
string::reverse_iterator it;
for(it = s1.rbegin(); it != s1.rend(); it++)
{
cout << *it;
}
return 0;
}
// 结果: dlrow olleh
rend
const reverse_iterator rend(); // 返回一个逆向迭代器,指向字符串的开头(第一个字符的前一个位置)。
replace
basic_string &replace( size_type index, size_type num, const basic_string &str );// 用str中的num个字符替换本字符串中的字符,从index开始
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
size_type num2 );// 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
basic_string &replace( size_type index, size_type num, const char *str );// 用str中的num个字符(从index开始)替换本字符串中的字符
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );// 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );// 用num2个ch字符替换本字符串中的字符,从index开始
basic_string &replace( iterator start, iterator end, const basic_string &str );// 用str中的字符替换本字符串中的字符,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, const char *str );
basic_string &replace( iterator start, iterator end, const char *str, size_type num ); // 用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, size_type num, char ch );// 用num个ch字符替换本字符串中的内容,迭代器start和end指示范围
reserve
void reserve( size_type num ); // 设置本字符串的capacity 以保留num个字符空间
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.reserve(20);
cout << s1.size() << endl;
cout << s1.capacity() << endl;
}
// 结果: 11 11 11 20
resize
void resize( size_type num );
void resize( size_type num, char ch ); // 改变本字符串的大小到num, 新空间的内容不确定。也可以指定用ch填充
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
s1.resize(5);
cout << s1 << endl;
s1.resize(10, 'a');
cout << s1 << endl;
return 0;
}
rfind
size_type rfind( const basic_string &str, size_type index ); // 返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回string::npos
size_type rfind( const char *str, size_type index );
size_type rfind( const char *str, size_type index, size_type num );// 返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符。如果没找到就返回string::npos
size_type rfind( char ch, size_type index );// 返回最后一个与ch匹配的字符,从index开始查找。如果没找到就返回string::npos
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
cout << s1.find("lo") << endl;
cout << s1.find("lo", 4) << endl;
cout << s1.find("lo", 4, 2) << endl;
cout << s1.find('o', 6) << endl;
cout << s1.rfind("lo") << endl;
cout << s1.rfind("lo", 4) << endl;
cout << s1.rfind("lo", 4, 2) << endl;
cout << s1.rfind('o', 6) << endl;
cout << s1.find_last_of("lo") << endl;
cout << s1.find_last_of("lo", 4) << endl;
cout << s1.find_last_of("lo", 4, 2) << endl;
cout << s1.find_last_of('o', 6) << endl;
return 0;
}
size
size_type size(); // 返回字符串中现在拥有的字符数
substr
basic_string substr( size_type index, size_type num = npos ); // 返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
string s2 = s1.substr(6);
cout << s2 << endl;
string s3 = s1.substr(6, 2);
cout << s3 << endl;
cout << s1 << endl;
return 0;
}
swap
void swap( basic_string &str ); // str和本字符串交换
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string s1 = "hello world";
string s2 = "welcome to china";
s1.swap(s2);
cout << s1 << endl;
cout << s2 << endl;
return 0;
}