今天總結一下string中的庫函數
STL之string常用庫函數筆記
- string的構造函數形式
- string的大小和容量
- string的字符串比較
- string的插入
- string的拼接
- string的遍歷
- string的刪除
- string的字符替換
- string大小寫轉換
- string的查找
- string的排序
- string的分割
1.string的構造函數形式
-
string s (str) :生成字符串s爲str的複製品
-
string s(str, str_begin, str_len):將字符串str中從下標str_begin開始、長度爲strlen的部分作爲字符串s的初值。
#include <bits/stdc++.h> using namespace std; int main() { string a("0123456789"); string b(a,0,5); string c(a,1,8); cout<<a<<endl<<b<<endl<<c<<endl; return 0; } /* 0123456789 01234 12345678 Process returned 0 (0x0) execution time : 0.120 s Press any key to continue.*/
2.string的大小和容量
- size()和length():返回string對象的字符個數,他們執行效果相同。
- max_size():返回string對象最多包含的字符數,超出會拋出length_error異常
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a("0123456789");
cout<<a.length()<<endl;
cout<<a.size()<<endl;
cout<<a.max_size()<<endl;
return 0;
}
/*
10
10
2147483647
Process returned 0 (0x0) execution time : 0.112 s
Press any key to continue.*/
3.string的字符串比較
- 比較操作符: >, >=, <, <=, ==, !=
這些操作符根據“當前字符特性”將字符按字典順序進行逐一比較,字典排序靠前的字符小,比較的順序是從前向後比較,遇到不相等的字符就按這個位置上的兩個字符的比較結果確定兩個字符串的大小(前面減後面) - 成員函數compare()
支持多參數處理,支持用索引值和長度定位子串進行比較,前面減去後面的ASCII碼,>0返回1,<0返回-1,相同返回0;
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a("0123456789");
string b("01245");
string c("acv");
cout<<a.compare(c)<<endl;
cout<<b.compare(c)<<endl;
cout<<a.compare(b)<<endl;
if(a<b)
cout<<"a<b"<<endl;
return 0;
}
//-1
-1
-1
a<b
Process returned 0 (0x0) execution time : 0.069 s
Press any key to continue.
4.string的插入
- string的插入:push_back() 和 insert();
// 尾插一個字符
s1.push_back('a');
s1.push_back('b');
s1.push_back('c');
cout<<"s1:"<<s1<<endl; // s1:abc
// insert(pos,char):在制定的位置pos前插入字符char
s1.insert(s1.begin(),'1');
cout<<"s1:"<<s1<<endl; // s1:1abc
5.string的拼接
- string拼接字符串:append() 、 +;
//方法一:append()
string s1("abc");
s1.append("def");
cout<<"s1:"<<s1<<endl; // s1:abcdef
// 方法二:+ 操作符
string s2 = "abc";
/*s2 += "def";*/
string s3 = "def";
s2 += s3.c_str();
cout<<"s2:"<<s2<<endl; // s2:abcdef
6.string的遍歷:藉助迭代器 或者 下標法
- 正向迭代器 str.begin()、str.end()
- 反向迭代器 str.rbegin()、str.rend()
- 下標
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1("abcdef");
// 下標
for(int i=0;i<s1.length();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;
return 0;
}
//
abcdef
abcdef
fedcba
Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
7.string的刪除
- iterator erase(iterator p):刪除字符串中p所指的字符
- iterator erase(iterator first, iterator last):刪除字符串中迭代器區間 [first, last) 上所有字符
- string.erase(size_t pos, size_t len):刪除字符串中從索引位置 pos 開始的 len 個字符
- void clear():刪除字符串中所有字符
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1("abcdef");
s1.erase(s1.begin()+1);
cout<<s1<<endl;
string s2("I Love China");
s2.erase(s2.begin()+1,s2.end()-1);
cout<<s2<<endl;
string s3("I Love China");
s3.erase(1,7);
cout<<s3<<endl;
return 0;
}
//
> cd "f:\Yqifei_javacode\" ; if ($?) { g++ qi.cpp -o qi } ; if ($?) { .\qi }
acdef
Ia
Ihina
PS F:\Yqifei_javacode>
8.string的字符替換
- string& replace(size_t pos, size_t n, const char s):將當前字符串從pos索引開始的n個字符,替換成字符串s
- string& replace(size_t pos, size_t n, size_t n1, char c):將當前字符串從pos索引開始的n個字符,替換成n1個字符c
- string& replace(iterator i1, iterator i2, const char s):將當前字符串[i1,i2)區間中的字符串替換爲字符串s
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1("hello,world!");
string s2("hello,world!");
string s3("hello,world!");
s1.replace(6,6,"girl");
s2.replace(0,12,"boy,girl");
cout<<s1<<endl;
cout<<s2<<endl;
return 0;
}
//
hello,girl
boy,girl
9.string大小寫轉換:tolower() 和 toupper() 或者 STL中的 transform 算法
- tolower(char) 和 toupper(char) :將字符進行大小寫轉換
- transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func):transform [_First, _Last) with _Func
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1("hello,world!");
for(int i=0;i<s1.size();i++){
s1[i]=toupper(s1[i]);
}
cout<<s1<<endl;
string s2("hello,world!"),s3;
transform(s2.begin(),s2.end(),s2.begin(),::toupper);
cout<<s2<<endl;
return 0;
}
//
HELLO,WORLD!
HELLO,WORLD!
10.string的查找:find
- find (const char* s, size_t pos):在當前字符串的pos索引位置開始,查找子串s,返回找到的位置索引
- find (char c, size_t pos):在當前字符串的pos索引位置開始,查找字符c,返回找到的位置索引
string s("dog bird chicken bird cat");
//字符串查找-----找到後返回首字母在字符串中的下標
// 1. 查找一個字符串
cout << s.find("chicken") << endl; // 結果是:9
// 2. 從下標爲6開始找字符'i',返回找到的第一個i的下標
cout << s.find('i', 6) << endl; // 結果是:11
11.string的排序
-
sort(iterator iter1, iterator iter2):對[iter1, iter2)進行排序
ps:for(auto x:a) cout<<x<<" "這是基於範圍的for循環,只有C++11版本以上才支持。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a("sdfergrggqeiufn");
sort(a.begin(),a.end());
for(auto x:a)
cout<<x<<" ";
cout<<endl;
return 0;
}
//
d e e f f g g g i n q r r s u
Process returned 0 (0x0) execution time : 0.020 s
Press any key to continue.
12.substr(start, length)方法:返回一個從指定位置開始,並具有指定長度的子字符串。
參數:
- start:必選。所需的子字符串的起始位置。字符串中第一個字符的索引爲 0。
- length:可選項。返回的子字符串中包含的字符數。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s = "12345";
string s1 = s.substr(3);
string s2 = s.substr(2, 3);
cout << s1 << ' ' << s2;
return 0;
}
//
45 345
Process returned 0 (0x0) execution time : 0.020 s
Press any key to continue.