資料結構+演算法=程式
--Anonymous
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <ctime>
#include <cstring>
#include <set>
#include <map>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <utility>
#include <numeric>
#include <climits>
using namespace std;
int main(){
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
return 0;
}
你要哪個
雖然第二堂課講過了但是可以再講一些觀念
宣告
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a;
vector<int> b(10);
vector<int> c(n);
vector<int> d(n,10);
vector<int> e={1,2,3};
vector<int> f=d;
vector<int> g(d.begin()+2,d.end());
vector<vector<int> > two;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> v(n);
v[0]=1;
fill(v.begin,v.end(),0);
iota(v.begin(),v.end(),0); //用來讓 v 的元素被設成 0,1,2,3...
v.resize(10);
v.reserve(100);
v.resize(15,3);
v.clear();
v.push_back(48763);
v.emplace_back(8763);
v.pop_back();
unsigned int a=v.size();
bool b=v.empty();
int c=count(v.begin(),v.end(),1);
return 0;
}
int main(){
int n;
cin >> n;
vector<int> v(n);
for(int i=0;i<n;i++){
cin >> v[i];
}
return 0;
}
int main(){
int n;
cin >> n;
vector<int> v(n);
for(int &h:v){
cin >> h;
}
return 0;
}
#include <bits/stdc++.h>
#define fs first
#define sc second
using namespace std;
int main(){
pair<int,int> a;
pair<int,double> b={1,-1.23};
pair<char,double> c('Y',0.48763);
pair<int,pair<int,int> > d=make_pair(1,make_pair(2,3));
pair<string,vector<pair<int,int> > > v;
vector<pair<int,int> > g(10,{2,3});
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
pair<int,int> a(1,2);
int b=a.first,c=a.second;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
stack<int> s;
s.push(10);
s.push(20);
cout << s.top() << "\n"; //20
s.pop();
cout << s.top() << "\n"; //10
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
deque<int> dq;
dq.push_back(10);
dq.push_back(20);
cout << dq.back() << "\n"; //20
dq.pop_back();
cout << dq.front() << "\n"; //10
dq.push_front(15);
dq.push_back(30);
cout << dq.front() << "\n"; //15
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
queue<int> q;
q.push(10);
q.push(20);
cout << q.front() << "\n"; //10
q.pop();
cout << q.front() << "\n"; //20
return 0;
}
stack, queue, deque
也有 size 跟 empty 這些函數
其實這三個東西都是可以用 vector 寫出來的
有興趣可以寫寫看
#include <bits/stdc++.h>
using namespace std;
int main(){
priority_queue<int> pq;
pq.push(3);
pq.push(6);
cout << pq.top() << "\n"; //6
pq.pop();
cout << pq.top() << "\n"; //3
return 0;
}
size 跟 empty 也可以用
#include <bits/stdc++.h>
using namespace std;
int main(){
set<int> s;
s.insert(3);
s.insert(6);
s.insert(8);
s.insert(4);
s.erase(6);
s.find(3);
s.erase(s.find(3));
for(auto h:s){
}
for(set<int>::iterator it=s.begin();it!=s.end();it++){
}
bool a=s.empty();
int z=s.size();
return 0;
}
vector<int> v;
set<int> s;
cout << v[lower_bound(v.begin(),v.end(),3)-v.begin()] << "\n";
cout << *s.lower_bound(4) << "\n";
#include <bits/stdc++.h>
using namespace std;
int main(){
map<int,int> m;
m[3]=1;
if(m.find(5)==m.end()){ //find key
m[5]=2;
}
m.insert({2,2});
m.erase(3);
for(auto h:m){//key
}
for(map<int,int>::iterator it=m.begin();it!=m.end();it++){
}
int a=m.size();
bool b=m.empty();
return 0;
}