網路高頻面試題
string | 構造 | 新增 | 刪除 | 替換 | 查詢(find,rfind) | 比較 | 大小 | 遍歷 |
普遍 | string s; | += | s.clear(); |
| find("a b c"); | compare(s) | size(); | for(0 - length()) |
string s("a b c"); | append(n,'c'); | s.pop_back() | find_first(last)_of("a b") | compare("a b") | ||||
string s(s1); | push_back() | find_first(last)_not_of("a b") | ||||||
location |
| insert(index,'c'); |
| find('c'(,index)); |
| |||
pos,n | string s(s1,pos,n); |
| erase(pos(,n)); | replace(pos,n,(a,)"a b c"); |
| compare(pos,n,s); | ||
first,last | string s(s1,first,last); |
| erase(first,last); | replace(first,last,"a b c"); |
|
|
【注意】
注1):last大多為該元素的前一個,而非本元素。即[first,last);
注2):().end不是最後一個元素,而是\0
注3):cout << s[1] << endl;沒毛病;但是cout << s.begin() << endl;有錯,要加*。
注4):c中那樣的(a[i]='1';)賦值方法是錯的。下標只能用來獲取已經存在的元素。
注5):注意begin()與front()異同
【其他函式】
empty()
resize()
reserve()
swap()
reverse (first,last);
sprintf、strcpy 及 memcpy
區別在於 實現功能 以及 操作物件 不同。
操作物件 | 目的 | |
strcpy | 字串 | 拷貝字串 |
sprintf | 任意基本型別的資料 | 基本資料型別向字串的轉換 |
memcpy | 任意資料型別 | 記憶體拷貝 |
利弊_單論拷貝字串:
strcpy 是最合適的選擇:效率高且呼叫方便。
sprintf 要額外指定格式符並且進行格式轉化,麻煩且效率不高。
memcpy 雖然高效,但是需要額外提供拷貝的記憶體長度這一引數,易錯且使用不便;(可以用來實現對結構或者陣列的拷貝,目的是高效或者使用方便)
摘自:https://blog.csdn.net/grl18840839630/article/details/79672837?ops_request_misc=&request_id=&biz_id=102&utm_term=strcpy%20sprintf%20memcpy%E7%94%A8%E6%B3%95&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-5-79672837.pc_search_result_control_group&spm=1018.2226.3001.4187
size, length, capacity
size()=length()——string的有效字元個數
capactiy()——string的容量
如果 value值為空,則capactiy()==0;否則,capacity() 初始值為32,根據string 儲存的量的變化而變化。初始值=32,步長=32;
vector | 構造 | 賦值 | 新增 | 刪除 | 大小 | 遍歷 |
普遍 | vector a(n(,num)); | push_back(); | a.clear(); | size(); | for(0 - length()) | |
vector<int>a(b); | assign(n,elem); | a.pop_back() | ||||
location | insert(position,(n,)elem); | erase(location); | ||||
first,last | vector a(b,b+n); | assign(b.first,b.last) | insert(position,b.first,b.last) | erase(first,last); | ||
pos,n | 為什麼vector不用這招了呢? |
deque | 構造 | 賦值 | 新增 | 刪除 | 大小 |
普遍 | deque<T> c; | push_back(elem) | pop_back() | size(); | |
deque<T> c(c2); | push_front(elem) | pop_front() | |||
deque<T> c(n,elem); | assign(n,elem); | clear(); | |||
location | insert(position,(n,)elem);(n個) | erase(location); | |||
first,last | deque<T> c(first,last); | assign(first,last); | insert(position,b.first,b.last); | erase(first,last); |