您的位置:首页 > 其它

STL:string常用函数方法使用介绍

2018-03-27 21:54 846 查看
0.append

用来在字符串的结尾增加字符串.也可以使用’+’运算符直接增加字符串.

string str = "abcde";
str.append("ab");
cout << str << endl;
//abcdeab
string str = "abcde";
str += "ab";
cout << str << endl;
//abcdeab


1.assign

assign方法用来重新给字符串赋值.

string str = "abcde";
str.assign(2,'a');
cout << str << endl;
//aa
//取指定字符串下标为2开始,2个位置的字符.
string str = "abcde";
str.assign("asdasd",2,2);
cout << str << endl;
//da
//取指定字符串的前四个字符.
string str = "abcde";
str.assign("asdasd",4);
cout << str << endl;
//asda


2.at

获取指定下标的字符.如果越界.则发出abort信号,中止程序.和[]一样.不过’[]’越界直接提示越界.

string str = "abcde";
cout << str.at(1) << endl;
//b
string str = "abcde";
cout << str[1] << endl;
//b


<
4000
strong>3.back[/b]

返回字符串的最后一个字符.如果字符串为空,则提示迭代器偏移量越界.访问了非法内容.

string str = "abcde";
cout << str.back() << endl;
//e


4.front

返回字符串的第一个字符.如果字符串为空,则提示迭代器偏移量越界.访问了非法内容.

string str = "abcde";
cout << str.front() << endl;
//a


5.begin

返回了字符串的首迭代器,迭代器可以理解为指针,也就是指向第一个字符的指针.但是迭代器和指针却不能化为一谈.只能说用法类似.

string str = "abcde";
cout << *str.begin() << endl;
//a


6.end

返回了字符串的尾迭代器,尾迭代器指向字符串最后一个字符的后一个位置.

string str = "abcde";
cout << *--str.end() << endl;
//e


7.rbegin

逆向返回指向第一个字符的迭代器.

string str = "abcde";
cout << *str.rbegin() << endl;
//e


8.rend

逆向返回指向最后一个字符的下一个字符的迭代器.

string str = "abcde";
cout << *--str.rend() << endl;
//a


9.cbegin

返回指向字符串第一个字符的常量迭代器.不能修改迭代器所指向内容的值.

string str = "abcde";
//错误,返回的是常量迭代器.不能修改迭代器所指向内容的值.
//*str.cbegin() = 'w';


10.cend

返回指向字符串最后一个字符后一个字符的常量迭代器.不能修改迭代器所指向内容的值.

string str = "abcde";
//错误,返回的是常量迭代器.不能修改迭代器所指向内容的值.
//*--str.cend() = 'w';


11.clear

清空字符串的内容.

12.compare

比较字符串的大小.可以取出子串进行比较.

string str = "abcde";
cout << str.compare("qezx") << endl;
//-1
//比较的是"bc"和"ez"两个子串的大小.
string str = "abcde";
cout << str.compare(1,2,"qezx",1,2) << endl;
//-1


13.c_str

把string类型转化为const char*类型.转化为了C风格的字符串,以’\0’结尾.

string str = "abcde";
char buff[16];
strcpy(buff,str.c_str());
cout << buff << endl;
//abcde.


14.data

与c_str()类似,但是返回的数组不以空字符终止.

15.empty

判断字符串是否为空.

16.erase

删除字符串中指定的元素.

string str = "abcde";
str.erase(1,2);
cout << str << endl;
//ade
string str = "abcde";
str.erase(1);
cout << str << endl;
//a
string str = "abcde";
str.erase(str.begin(),str.begin()+2);
cout << str << endl;
//cde


17.find

返回从指定位置,顺序查找第一次出现指定字符串的下标.

string str = "abacde";
cout << str.find('a') << endl;
//0
string str = "abacde";
cout << str.find('a',2) << endl;
//2


18.find_first_not_of

返回从指定位置,顺序查找第一个不是指定字符串的下标.

string str = "abacde";
cout << str.find_first_not_of('a') << endl;
//1


19.find_first_of

返回字符串中第一个含有指定字符串中字符的下标.

string str = "abacde";
cout << str.find_first_of("wex") << endl;
//5
``


20.find_last_not_of

逆向查找,返回字符串中第一个不是指定字符串中任意字符的下标.

string str = "abacde";
cout << str.find_last_not_of("wex") << endl;
//4


21.find_last_of

逆向查找,返回字符串中第一个是指定字符串中任意字符的下标.

string str = "abacde";
cout << str.find_last_of("wex") << endl;
//5


22.insert

在指定位置插入字符或字符串.

string str = "abacde";
str.insert(2,"asd");
cout << str << endl;
//abasdacde


23.length

返回字符串的长度.

string str = "abacde";
cout << str.length() << endl;
//6


24.npos

返回字符串最后一个字符的下一个字符的位置.

string str = "abacde";
cout << (int)str.npos << endl;
//-1


25.max_size

返回字符串的最大长度.

string str = "abacde";
cout << str.max_size() << endl;


26.capacity

返回当前字符串的容量.

string str = "abacde";
cout << str.capacity() << endl;
//15


27.pop_back

删除最后一个字符.

string str = "abacde";
str.pop_back();
cout << str << endl;
//abacd


28.push_back

在字符串末尾添加一个字符.

string str = "abacde";
str.pop_back('q');
cout << str << endl;
//abacdeq


29.replace

replace用来替换指定位置的字符串.

string str = "abcde";
str.replace(0,2,"asd");
cout << str << endl;
//asdcde
string str = "abcde";
str.replace(str.begin(),str.begin()+1,"zx");
cout << str << endl;
//zxbcde


30.reserve

调整字符串的内存大小,从而改变了容量的大小.

string str = "abcde";
str.reserve(20);
cout << str << endl;
cout << str.capacity() << endl;
//abcde
//31


31.resize

resize和reserve都可以改变容量的大小,但是resize可以控制字符的个数.减少则删除元素,增加则自动填充指定字符,如果没有指定,则添加’\0’.

string str = "abcde";
str.resize(2);
cout << str << endl;
cout << str.capacity() << endl;
//ab
//15
string str = "abcde";
str.resize(10,'z');
cout << str << endl;
//abcdezzzzz


32.size

返回字符串的长度,相当于C风格字符串的strlen方法.

string str = "abcde";
cout << str.size() << endl;
//5


33.substr

获取字符串的子串.

string str = "abcde";
cout << str.substr(0) << endl;
cout << str.substr(2, 2) << endl;
//abcde
//cd


34.swap

用来和另外一个字符串进行交换.

string str = "abcde";
string temp = "as";
str.swap(temp);
cout << str  << endl;
cout << temp << endl;
//as
//abcde
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  STL