STL String常用函数用法总结

string是STL中的字符串,虽然有点慢,但很多时候都很有用…

一、string的构造函数

//生成空串
default (1)	      string();

//生成字符串为str的复制品
copy (2)	      string (const string& str);
//将字符串str中从下标pos开始、长度为len的部分作为字符串初值
substring (3)     string (const string& str, size_t pos, size_t len = npos);
//生成内容和C的char*类型一样的字符串
from c-string (4) string (const char* s);
//以C的char*类型s的前char_len个字符串作为字符串s的初值
from buffer (5)	  string (const char* s, size_t n);

//生成n个c字符的字符串
fill (6)	      string (size_t n, char c); 

此外还有:

//生成一个相应位置的长度为len的子串
string substr (size_t pos = 0, size_t len = npos) const;  

二、string的大小和容量

size
length  :Return length of string 返回string对象的长度(字符个数)

max_size:Return maximum size of string 返回string对象最大的字符容量

resize  :Resize string 调整字符串的容量大小
capacity:Return size of allocated storage (public member function ) 返回已经分配的内存空间
clear   :Clear string (public member function ) 清空字符串
empty   :Test if string is empty (public member function ) 判断字符串是否为空
#include 
#include 
using namespace std;

int main() {              
	string s("123456789"); 
	cout << "size = " << s.size() << endl;
	cout << "length = " << s.length() << endl;
	cout << "max_size = " << s.max_size() << endl;
	cout << "capacity = " << s.capacity() << endl;
	s.resize(14);
	cout << "size = " << s.size() << endl;
	cout << "length = " << s.length() << endl;
	cout << "max_size = " << s.max_size() << endl;
	cout << "capacity = " << s.capacity() << endl;	
	return 0;
}

STL String常用函数用法总结_第1张图片

三、string的字符串比较

用c的char*时只能用strcmp,麻烦,而string支持>, >=, <, <=, ==, !=的比较,甚至支持string和char*的比较,这种比较是根据字典序的,从前往后。一般这样比较就可以了。

四、string的字符访问

operator[]
    Get character of string (public member function )
at
    Get character in string (public member function )
back
    Access last character (public member function )
front
    Access first character (public member function )

可以用下标和at,当然,最常用的还是下标。

void test6()
{
    string s1("abcdef"); // 调用一次构造函数

    // 方法一: 下标法

    for( int i = 0; i < s1.size() ; i++ ) 
        cout<<s1[i]; 
    cout<<endl;

    // 方法二:正向迭代器

    string::iterator iter = s1.begin();
    for( ; iter < s1.end() ; iter++) 
        cout<<*iter; 
    cout<<endl;

    // 方法三:反向迭代器
    string::reverse_iterator riter = s1.rbegin();
    for( ; riter < s1.rend() ; riter++) 
        cout<<*riter; 
    cout<<endl;
} 

STL String常用函数用法总结_第2张图片

五、string的修改

operator+=
    Append to string (public member function )                 添加字符串
append
    Append to string (public member function )                 添加字符串
push_back
    Append character to string (public member function )       添加字符到字符串尾部

pop_back 
    Delete last character (public member function )            删除尾部字符

最常用的还是+=,还有+,以及append。

六、string的删除erase

1. iterator erase(iterator p);   //删除字符串中p所指的字符                 
//删除字符串中迭代器区间[first,last)上所有字符
2. iterator erase(iterator first, iterator last); 
//删除字符串中从索引位置pos开始的len个字符 
3. string& erase(size_t pos = 0, size_t len = npos);  

七、string的查找:find

//在当前字符串的pos索引位置开始,查找子串s,返回找到的位置索引, -1表示查找不到子串
1. size_t find (constchar* s, size_t pos = 0) const;
2. npos
    Maximum value for size_t 无法找到子串时返回的标志

你可能感兴趣的:(STL,======算法======)