1String概念
² string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是用char*表示的。string与char*都可以用来表示字符串,那么二者有什么区别呢。
string和char*的比较
² string是一个类, char*是一个指向字符的指针。
string封装了char*,管理这个字符串,是一个char*型的容器。
² string不用考虑内存释放和越界。
string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。
² string提供了一系列的字符串操作函数(这个等下会详讲)
查找find,拷贝copy,删除erase,替换replace,插入insert
2string的构造函数
² 默认构造函数:
string(); //构造一个空的字符串string s1。
² 拷贝构造函数:
string(const string &str); //构造一个与str一样的string。如strings1(s2)。
² 带参数的构造函数
string(const char *s); //用字符串s初始化
string(int n,char c); //用n个字符c初始化
3string的存取字符操作
² string类的字符操作:
1 2 3 4 | const char &operator[] (int n) const ;
const char &at(int n) const ;
char &operator[] (int n);
char &at(int n);
|
登录后复制
² operator[]和at()均返回当前字符串中第n个字符,但二者是有区别的。
主要区别在于at()在越界时会抛出异常,[]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。
4从string取得const char*的操作
² const char *c_str() const; //返回一个以'\0'结尾的字符串的首地址
5把string拷贝到char*指向的内存空间的操作
² int copy(char *s, int n, int pos=0) const;
把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目。注意要保证s所指向的空间足够大以容纳当前字符串,不然会越界。
6string的长度
int length() const; //返回当前字符串的长度。长度不包括字符串结尾的'\0'。
bool empty() const; //当前字符串是否为空
7string的赋值
1 2 3 4 5 6 | string &operator=( const string&s);
string &assign( const char *s);
string &assign( const char *s, int n);
string &assign( const string&s);
string &assign(int n,char c);
string &assign( const string &s,intstart, int n);
|
登录后复制
8string字符串连接
1 2 3 4 5 6 7 | string &operator+=( const string&s);
string &operator+=( const char *s);
string &append( const char *s);
string &append( const char *s,intn);
string &append( const string&s);
string &append( const string &s,intpos, int n);
string &append(int n, char c);
|
登录后复制
9string的比较
1 2 3 | int compare( const string &s) const ;
int compare( const char *s) const ;
compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。
|
登录后复制
10string的子串
1 | string substr (int pos=0, int n=npos) const ;
|
登录后复制
11string的查找和 替换
查找
1 2 3 4 5 6 7 8 | int find(char c,int pos=0) const ;
int find( const char *s, int pos=0) const ;
int find( const string &s, int pos=0) const ;
find函数如果查找不到,就返回-1
int rfind(char c, int pos=npos) const ;
int rfind( const char *s, int pos=npos) const ;
int rfind( const string &s, intpos=npos) const ;
|
登录后复制
替换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | string &replace(int pos, int n, constchar *s);
string &replace(int pos, int n, conststring &s);
void swap(string &s2);
void main25()
{
strings1 = "wbm hello wbm 111 wbm 222 wbm 333" ;
size_tindex = s1.find( "wbm" , 0);
cout<< "index: " << index;
size_toffindex = s1.find( "wbm" , 0);
while (offindex != string::npos)
{
cout<< "在下标index: " << offindex << "找到wbm\n" ;
offindex= offindex + 1;
offindex= s1.find( "wbm" , offindex);
}
strings2 = "wbm hello wbm 111 wbm 222 wbm 333" ;
s2.replace(0,3, "wbm" );
cout<< s2 << endl;
offindex= s2.find( "wbm" , 0);
while (offindex != string::npos)
{
cout<< "在下标index: " << offindex << "找到wbm\n" ;
s2.replace(offindex,3, "WBM" );
offindex= offindex + 1;
offindex= s1.find( "wbm" , offindex);
}
cout<< "替换以后的s2:" << s2 << endl;
}
|
登录后复制
12String的区间删除和插入
1 2 3 4 5 | string &insert(int pos, const char *s);
string &insert(int pos, const string&s);
string &insert(int pos, int n, charc);
string &erase(int pos=0, intn=npos);
|
登录后复制
13string算法相关
1 2 3 4 5 6 7 8 9 | void main27()
{
strings2 = "AAAbbb" ;
transform(s2.begin(),s2. end (), s2.begin(), toupper);
cout<< s2 << endl;
strings3 = "AAAbbb" ;
transform(s3.begin(),s3. end (), s3.begin(), tolower);
cout<< s3 << endl;
}
|
登录后复制
以上就是c++复习要点总结z之十二——STL string的内容,更多相关内容请关注PHP中文网(www.php.cn)!