c++stl notes
- 加了using namespace std 起名時 容易和庫函式衝突(prev, count, next, sort)
- 用printf和scanf輸入輸出比cout和cin要快
- 關於cin.get()
- cin.getline(char*,int) 和getline(cin,line)
- c++動態開闢記憶體
int* number=new int;
int* arr=new int[100];
int* carr=(int *)malloc(100*sizeof(int));
- c++struct
struct node{
int number;
node* next;
};
node* head;
struct node{
int number;
node* next;
node(int _number=0, node* _next=NULL){
number=_number;
next=_next;
}
};
int main(){
node a=node(0);
node* b=new node(1,&a);
}
- c標準庫
**<cstring>** strlen()、strcmp()、strcpy()、memset()
char str[10];
memset(str,-1,sizeof(str)); //可以填0、-1和0x3f3f3f3f
**<cmath>** 三角函式、指數函式
**<cstdlib>** qsort()//C語言快排
rand() <ctime> srand(time(NULL))
malloc() free()
- c++標準庫
**<vector>**
vector<int> list;
int a;
cin>>a;
list.push_back(a);
vector<int>::iterator p1;//迭代器
for(p1=list.begin();p1!=list.end();p1++){ //遍歷
cout<<*p1<<endl;
}
vector 常見操作
list.size();//O(1)
list.clear();//O(n)
list.empty();//O(1)
list.begin();//O(1)
list.end();//O(1) 該元素實際在陣列中不存在
list.erase(p1); //刪除陣列某個迭代器所在位置的數字O(n)
list.push_back(1);//O(1)
list.pop_back();//O(1)
**<string>**
//可以看成特殊的vector
//vector所有的操作,string基本都有,唯一的區別是size的複雜度
string str="Hello";
str.length(); str.size()//O(n)
str.insert(1,"aaa");
str.insert(str.begin(),'a');
str.append(str2);
str.compare(str2);
str==str2; str+=str2; str+='a';
**<algorithm>**
int arr[]{2,3,1,5,4};
vector<int> arr1{2,3,1,5,4};
sort(arr,arr+5); //快排 O(nlogn)
sort(arr2.begin(),arr2.end());
bool cmpInt(int a, int b){
return a>b;
}
sort(arr2.begin(),arr2.end(),cmpInt); //降序排列
min(1,2); max(1,2);
min_element(arr.begin(),arr.end());
max_element(arr.begin(),arr.end());//O(n)
nth_element(arr.begin(),arr.begin()+n,arr.end());//O(n)
swap(arr[0],arr[1]);//O(1)
reverse(arr.begin(),arr.end())//O(n)
int newLength=unique(arr.begin(),arr.end()-arr.begin());//需要在sort之後使用
bool isExist=binary_search(arr.begin(),arr.end(),1);//O(logn)
int firstLoc=lower_bound(arr.begin(),arr.end(),2)-arr.begin();
int lastLoc=upper_bound(arr.begin(),arr.end(),2)-arr.begin(); //O(logn)
//其他標準庫
**<stack>** **<queue>**
stack<int> sta; queue<int> que;
sta.push(1); que.push(1);
int topElement=sta.top(); int frontElement=que.front();
sta.pop(); que.pop();
sta.empty(); que.empty();
sta.size(); que.size();
priority_queue<int> que2; //<functional>配合使用
que2.push(1);
int minElement=que2.top();
que2.pop();
que2.empty();
que2.size();
//棧和佇列的複雜度都為O(1) 優先佇列的複雜度為O(logn)
**<set>**
set<int> st; multiset<int> mst;
st.insert(1); mst.insert(1);
st.find(1); mst.insert(1);
st.erase(1); mst.count(1);//2
//O(logn) 迭代器的++和--能夠在O(logn)的時間裡找到第一個比它大(小)的數
**<map>**
pair<int,int> origin;
origin=make_pair(0,0);
origin.first==origin.second;
origin.swap;//返回swap的pair
pair<string,int> id;
id=make_pair("somebody",110);
map<string,int> studentHeight;
studentHeight["小明"]=170;
studentHeight["小紅"]=150;
studentHeight.insert(id);
studentHeight.erase("小明");
//O(logn)
//<unordered_set>和<unordered_map>這兩種資料結構不允許按大小順序遍歷元素,但能O(1)地訪問和新增一個元素 雜湊
#include<bits/stdc++.h> //一鍵包含所有標頭檔案 visual studio除外
- 細節
1s時限內能做的運算次數大約為1e8,根據複雜度來算是否會超時
g++在輸出double時不能用%lf,要用%f
相關文章
- C++STLC++
- C++STL常見面試題C++面試題
- Mongodb NotesMongoDB
- Typora Notes
- ACM notesACM
- WireGuard Use Notes
- C++STL第二篇(vector的原理用法)C++
- Linux配置notesLinux
- BlockRecover Restrictions and Usage NotesBloCREST
- Make notes for disaster recoveryAST
- Thinking in java notesThinkingJava
- Index of system requirements for Notes, Domino, Domino Administrator, Domino Designer & Notes TravelIndexUIREM
- Notes與Office的介面
- Oracle Training NotesOracleAI
- Recommendation Systems Basic Notes
- Reinforcement Learning Basic Notes
- Redis Reading NotesRedis
- Based UE_Project NotesProject
- C++STL::兩種方式實現STL容器的reference語義C++
- 點乘和叉乘及其物理意義(C++STL實現)點乘C++
- R language notes | pipes: chainingAI
- D3 JS study notesJS
- Notes about Vue Style GuideVueGUIIDE
- Android Weekly Notes Issue #255Android
- Notes for building gimp-printUI
- Notes與Office的介面 (轉)
- Notes of Oracle 11.2 Dataguard setupOracle
- Execl匯入notes JAVA代理Java
- C++STL第四篇(最簡單的棧和佇列)C++佇列
- C++STL第五篇(連結串列List的使用方法)C++
- SAP SD Reference Guide: SAP NotesGUIIDE
- some notes about distributed workflows in GitGit
- Overview of Parameter Reference Notes (Doc ID 68462.1)View
- Lotus notes問題與處理
- 【ZooKeeper Notes 9】ZooKeepr日誌清理
- Fw:Notes程式開發規範
- Flutter Notes | Android 借殼分享微信FlutterAndroid
- Windows Kernel Exploitation Notes(一)——HEVD Stack OverflowWindows