STL簡記

STL簡記

string
成員函數 作用 示例代碼
size()、length() 字符串長度
=、assign() 賦新值 str.assign(str2,index,len)
+=、append() 添加字符 str.append(str2,index,len)
insert() 插入字符 str.insert(6,str2,[3,4])
erase() 刪除字符 index+len式; str.erase(str.begin()+5,str.end()-9)
empty() 是否空
front()、back() 首尾字符 str.front()='T'
replace() 替換字符 str.replace(b1,e1,str2,[b2,e2])
pop_back() 快速刪除尾字符 str.pop_back()
copy() A的內容複製給B A.copy(B,index,len)
find()、rfind() 查找 pos=str.find(str2,[index,len])
find_first_not_of() pos=str.find_first_not_of("0123456789")
substr() 截取子串 str.substr([b],e)
swap()、getline() ~通用算法函數 swap(s1,s2) getline(cin,str)

getline接收空格、cin.get(str,len,'\n')接收空格和enter

迭代器遍歷

for (string::iterator it=str.begin(); it!=str.end();++i){
    cout<<*it;
}
反向遍歷
for (string::reverse_iterator rit=str.rbegin(); rit!=str.rend();++rit){
	cout<<*rit;
}
vector

size()/assign()/insert()/erase()/empty()/front()/back()/push_back()/pop_back()

迭代器遍歷

for (vector<int>::iterator it=str.begin(); it!=str.end();++i){
    cout<<*it;
}
algorithm
for_each	使大段循環的代碼美觀
void myfunc(int elem){

}
	for_each(myvector.begin(),myvector.end(),myfunc);

find(begin指針/迭代器,end,const型elem)
int *p = find(a,a+4,30);

find_if 加強版查找
bool isOdd(int elem){

}
vector<int>::iterator it=find_if(myvec.begin(),myvec.end(),isOdd);

copy()/swap()/replace()/reverse()

參考教程:http://www.cplusplus.com/reference/string/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章