//1.sort函數排序/*using namespace std;int main(){ int a[] = { 2,0,3,1,8,2,4,0 }; sort(a, a + 3);//對前三個數排序 for (int i=0;i<8;++i) { cout << a[i] << " "; } return 0;}*///2.字符串處理/*using namespace std;int main(){ string s = "hello world!"; cout<<s<<endl; string a; getline(cin, a);//獲取一行數據 cout << a << endl; return 0;}*//*using namespace std;int main(){ string s; s += "小明"; s =s+"小紅"; int a = 5; s += (a + '0');//把a加入到字符串中 cout << s << endl; string x = "741852"; sort(x.begin(),x.end());//排序 cout << x << endl; string n = "7a4185b2"; n.erase(n.begin());//刪除第一個元素 n.erase(--n.end());//刪除最後一個元素 cout << n << endl; string m = "147258369"; m = m.substr(2, 5);//取72583,從2位置開始往後面截斷5個 cout << m<<endl; m = m.substr(2, -1);//索引為2,截斷到最後 cout << m << endl; return 0;}*///循環/*using namespace std;int main(){ string s="147258369"; for (int i=0;i<s.length();++i) { cout <<s[i] ; } cout << endl; //迭代器 for (string ::iterator it=s.begin();it!=s.end();++it) { cout << *it; } cout << endl; //迭代器化簡 for (auto it=s.begin();it!=s.end();++it) { cout << *it; } cout << endl; //C++11新特性 for (auto x:s) { cout << x; } return 0;}*///3.vector.vector相當於數組,模板類型相當於存放的內容/*using namespace std;int main(){ //1.vector構造 vector <int> v0;//定義一個空的vector vector<int> v2(4);//定義一個4個大小的vector,初始為0 for (auto x:v2) { cout << x; } cout << endl; vector<int> v3(4, 6);//定義一個4個大小的vector,初始值為6 for (int i=0;i<v3.size();++i) { cout << v3[i]; } cout << endl; vector<int> v{ 8,2,3,4,5 };//定義一個vector,數字為1,2,3,4,5 for (auto it=v.begin();it!=v.end();++it) { cout << *it; } cout << endl; cout << v[1];//取索引為1的元素 cout << endl; cout << v.at(2);//取索引為1的元素 return 0;}*//*using namespace std;int main(){ //push_back追加內容 vector <int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(6); for (int i=0;i<v.size();++i) { cout << v[i]; } cout << endl; v.resize(10);//進行重置大小,不賦值默認為0 for (int i = 0; i < v.size(); ++i) { cout << v[i]; } cout << endl; v.erase(v.begin()); v.erase(--v.end()); for (int i = 0; i < v.size(); ++i) { cout << v[i]; } return 0;}*//*using namespace std;int main(){ //push_back追加內容 vector <int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(6); cout << v.front()<<" ";//獲取第一個元素 cout << v[0]<<" "; cout << *v.begin()<<" "; cout << v.back()<<" ";//獲取最後一個元素 cout << v[v.size()-1]<<" "; cout << *--v.end()<<endl; //排序 //vector<int> v{ 1, 4, 7, 2, 5, 8, 3, 6, 9 }; sort(v.begin(), v.end(), less<int>());//從小到大排列 for (auto x : v) { cout << x; } cout << endl; sort(v.begin(), v.end(), greater<int>());//從大到小排列 for (int i=0;i<v.size();++i) { cout << v[i]; } return 0;}*///4.棧/*using namespace std;int main(){ stack<int> s; //s.push(1); s.push(2); s.push(3); s.push(4); s.push(5); cout << s.top() << endl; s.pop(); cout << s.top() << endl; cout << s.size() << endl; bool a = s.empty(); cout << a << endl; return 0;}*///進位轉換(十轉二)/*using namespace std;int itob(int decimal){ stack<int> s; int res = 0; while (decimal!=0) { s.push(decimal % 2); decimal /= 2; } while (!s.empty()) { res = res * 10 + s.top(); s.pop(); } return res;}int main(){ int n; cin >> n; cout << itob(n); return 0;}*///輸入一行字符串,將字符串逆序列印//輸入:hello world//輸出:world hello/*using namespace std;int main(){ string str; stack<string> s; getline(cin, str); stringstream ss; ss << str; while (ss>>str) { s.push(str); } while (!s.empty()) { cout << s.top(); s.pop(); if (s.size() != 0) cout << " "; } return 0;}*//*#include <iostream>using namespace std;int main(){ //1.字符串轉數字 string s = "123456"; int i; stringstream ss; ss << s; // 將string類型的值放入輸入流中 ss >> i;// 從sstream中抽取前面插入的string類型的值,賦給int類型 cout << i << endl; //方法2 string a = "123455"; int b = stoi(a); cout << b << endl; //數字轉字符串 int c = 1234; string out; stringstream aa; aa << c; aa >> out; cout << out << endl; //方法2 int d=23123456; cout << to_string(d) << endl; return 0;}*///4.隊列/*using namespace std;int main(){ queue<int> q; q.push(5); q.push(6); q.push(7); cout << q.front() << endl; cout << q.size() << endl; return 0;}*//*using namespace std;int main(){ map<int, int> m;//有序的,樹狀結構 m[6] = 3; m[5] = 8; m[4] = 9; for (auto it=m.begin();it!=m.end();it++) { cout << it->first << " " << it->second << endl; } for (auto tmp : m) { cout << tmp.first << " " << tmp.second << endl; } return 0;}*//*using namespace std;int main(){ unordered_map<int, int> m;//無序的 m[6] = 3; m[5] = 8; m[4] = 9; m[2] = 6; m[1] = 0; for (auto it = m.begin(); it != m.end(); it++) cout << it->first << " " << it->second << endl; for (auto tmp : m) { cout << tmp.first << " " << tmp.second << endl; } return 0;}*///pair的用法,將map轉成vector進行排序/*using namespace std;bool cmp(pair<int, int> a, pair<int, int> b){ return a.first > b.first;}int main(){ unordered_map<int, int> m;//無序的 m[4] = 9; m[2] = 6; m[6] = 3; m[5] = 8; m[1] = 0; vector<pair<int, int>> v(m.begin(), m.end()); sort(v.begin(), v.end(), cmp); for (auto tmp : v) { cout << tmp.first << tmp.second << endl; } return 0;}*///集合,計數去重/*using namespace std;int main(){ set<int> s;//樹狀結構,有序 unordered_set<int> s2;//哈希結構,無序,快 s.insert(3); s.insert(4); s.insert(4); s.insert(4); s.insert(5); cout << s.size() << endl; for (auto tmp:s) { cout << tmp << " "; } cout << endl; for (auto it=s.begin();it!=s.end();++it) { cout << *it << " "; } cout << endl; return 0;}*///deque雙端隊列/*using namespace std;int main(){ deque<int> d; //4 9 1 2 d.push_back(1); d.push_back(6); d.push_back(5); d.push_back(7); d.push_back(2); d.push_front(9); d.push_front(5); d.push_front(3); d.push_front(6); d.push_front(4); d.pop_back(); d.pop_front(); for (auto tmp:d) { cout << tmp <<" "; } cout << endl; sort(d.begin(), d.end(), greater<int>()); for (auto it=d.begin();it!=d.end();++it) { cout << *it <<" "; } cout << endl; return 0;}*///list雙向鍊表using namespace std;int main(){ list<int> li; li.push_back(6); li.push_front(5); li.emplace_front(9);//在開頭添加元素 li.emplace_back(10); li.insert(++li.begin(), 2); for (auto tmp:li) { cout << tmp << " "; } cout << endl; for (auto it = li.begin(); it != li.end(); it++) cout << *it << endl; return 0;}