1. 程式人生 > 其它 >C++基礎知識string,set,佇列,棧

C++基礎知識string,set,佇列,棧

因為要找實習,所以回顧了c++程式設計的基礎知識點,在這裡順便記一下。

string

1 cin輸入字串時遇到空格和回車會結束;

getline(cin,str)函式可以輸入帶空格的字串 ,以回車為中止

2string轉int:

常用方式1

string numstr = str.substr(開始位置,長度); //擷取部分字串
int num = stoi(numstr); //string轉int

常用方式2:

#include <sstream>
//getline以;作為分隔符一部分一部分的讀取
while(getline(cin,str,';')){}
//擷取str,從第二個字元一直到最後
string part2 = str.substr(1) //將字串string型別part2轉換為int型num stringstream ss; ss << part2; ss >> num;

set

set裡面的值都是唯一的,沒有重複的;且會從小到大排序
參考連結:https://www.cnblogs.com/caiyishuai/p/8646345.html
#include<set>
set<int> arr;
arr.insert(x);
set<int>::iterator it;
for(it = arr.begin();it!=arr.end();it++){}

佇列

#include <queue>
queue<pair<int,int>> q;
q.empty()
q.size()
q.pop()
q.push()
q.front()
q.back()

#include <stack>
stack<pair<int,int>> s;
s.empty()
s.size()
s.pop()
s.push()
s.top()

棧和佇列有很多函式都是一樣的。