1. 程式人生 > >STL

STL

不同 不存在 一個 提前 ear class 長度 順序輸出 交換

  1 //****************STL*************
  2 #include <vector> // 向量:用於存儲連續的元素,類似數組
  3 #include <set> // 集合
  4 #include <queue> // 隊列
  5 #include <stack> //
  6 #include <map> // 映射:相當於一個坐標無限大的數組
  7 #include <list> // 列表:類似於鏈表
  8 using namespace std;
  9 int main() 
 10 {
11 min(x, y)//:取兩者中的較小值 12 max(x, y)//:取兩者中的最大值 13 swap(x, y)//:交換兩者的值 14 15 sort(first, last)//:對於在 [first, last) 中的元素進行排序 16 sort(first, last, comp)//:對於在 [first, last) 中的元素進行排序,其中 comp 為比較器 17 18 int a[10] = {5, 2, 1, 3, 4}; 19 sort(a, a+5);// a = [1, 2, 3, 4, 5, ...] 普通排序
20 int cmp(int x, int y) 21 { 22 return x > y; // 比較器 23 } 24 sort(a, a+5, cmp);// a = [5, 4, 3, 2, 1, ...] 從大到小排序 25 26 lower_bound(first, last, element) 27 int a[10] = {5, 2, 1, 3, 4, 3, 2, 4}; 28 sort(a, a+8); // a = [1, 2, 2, 3, 3, 4, 4, 5] 已從小到大排好序 29
int p2 = lower_bound(a, a+8, 2) - a; // p2 = 1// a中最前的不比 2 小的元素是: a[1] = 2 30 31 int p4 = lower_bound(a, a+8, 4) - a; // p4 = 5// a中最前的不比 4 小的元素是: a[5] = 4 32 33 int p0 = lower_bound(a, a+8, 0) - a; // p0 = 0// a中最前的不比 0 小的元素是: a[0] = 1 34 35 int p6 = lower_bound(a, a+8, 6) - a; // p6 = 8// a中沒有不比 6 小的元素,所以返回最後的一個位置 a[8] 36 37 // 註意:a[8] 中並沒有值,是數組的最末端 38 //*********向量************ 39 40 //向量 (vector):用於存儲連續的元素,類似數組;並且可以動 41 //態改變數組的大小(不必提前聲明數組大小) 42 43 // vector 聲明 44 45 vector<int> a; // 聲明一個空的 vector 46 vector<int> b(5, 0); // 聲明 vector 中有 5 個元素,並且初始值都為 0 47 48 // vector 訪問 49 50 for(int i=0; i<5; i++) b[i] = i; // vector 中下標從 0 開始 51 // ! a[0] = 1; 非法操作:a 還是空的,沒有第一個位置 52 // ! a[5] = 5; 非法操作:b 沒有第六個位置 53 54 // vector 添加元素 55 56 for(int i=0; i<5; i++) a.push_back(i * i); // 在 a 最後增加一個元素 57 for(int i=0; i<5; i++) cout << a[i] << "?"; // 輸出 vector a 58 // 輸出結果: 0 1 4 9 16 59 60 // vector 設定長度 61 62 vector<int> c; 63 c.resize(5); // 設置 c 的大小為 5,下標 [0-4] 64 c[4] = -4; // c[4] 是可以被訪問的 65 66 // vector 長度 (數組大小) 67 68 int len = c.size(); // len = 5 69 70 // vector 清空 71 72 c.clear(); // 清空 c 數組 73 // !c[4] = -4; 非法操作:數組已經被清空,長度變為 0 74 75 //****************二元組*********** 76 77 //二元組 (pair):用於存儲二元組 (x,y)(並且 x,y 類型可以不相同) 78 79 #include <algorithm> 80 81 // pair 聲明 82 83 pair<int, int> a; // 聲明一個二元組 a,用尖括號括起來 84 pair<int, int> b(3, 4); // 聲明一個二元組 b, x=3, y=4 85 typedef pair<int, int> PI; // 使用類型定義 86 PI c(5, 6); // 聲明一個二元組 c 87 88 // pair 訪問 89 90 int x = c.first, y = c.second; // x = 5, y = 6 91 PI d = c; // 相同類型的二元組可以直接賦值 92 c = PI(7, 8); // 重新賦值二元組 93 d = make_pair(9, 10); // 另一種賦值方式 94 95 //二元組 (pair)的一個優點是可以直接比較大小;它會先比較第一關鍵字(x),再比較第二關鍵字(y)。 96 97 PI arr[10]; 98 99 for(int i=0; i<5; i++) arr[i] = PI(5 - i, i * i); 100 101 sort(arr, arr+5); 102 103 for(int i=0; i<5; i++) 104 105 cout << "First?element:" << arr[i].first << "?"<< "Second?element:" << arr[i].second << endl; 106 107 /*輸出結果: 108 First element:1 Second element:16 109 First element:2 Second element:9 110 First element:3 Second element:4 111 First element:4 Second element:1 112 First element:5 Second element:0*/ 113 114 typedef pair<int, PI> PII; // 定義三元組 115 116 PII p = PII(2, PI(4, 8)); // 嵌套定義三元組 117 cout << p.first << "," << p.second.first << ","<< p.second.second << endl; // 嵌套輸出 118 typedef pair<string, int> People; // 定義字符串 -整數二元組 119 120 People pa("Edward", 16); 121 People pb("Bob", 17); 122 123 if(pa > pb) swap(pa, pb); // 比較大小 124 cout << pa.first << ":" << pa.second << endl; // 輸出較小值 125 126 //*********set(集合)********* 127 128 //在集合 (set)中,可以像普通的集合一樣,保存所有不同的數: 129 130 set<int> S; // 定義一個整型類型的 set 131 132 for(int i=0; i<10; i++) S.insert(i*i); // 把 0-9 的平方數加入集合中 133 134 set<int>::iterator p; // iterator:叠代器,相當於一個指針 135 136 p = S.find(4); // 指針 -- 找到 4 所在位置 137 138 cout << *p << endl; // 輸出這個位置的值 -- 4 139 140 ++p; // 指針後移 141 142 cout << *p << endl; // 輸出後一個平方數的值 -- 9 143 /*輸出結果: 144 4 145 9*/ 146 147 //在集合 (set)中,可以支持查找一個數的位置: 148 149 p = S.find(5); // 假如我們想要找一個集合中沒有的數 150 cout << "Notice?:?(5?is?not?a?square?number)?" << *p << endl; 151 cout << (p == S.end()) << endl; 152 153 // 就會返回一個特殊的指針:S.end() 結尾的指針 154 // C++ 中的容器都是左閉右開的: 155 // S.begin() 頭指針 - 存放數據,S.end() 尾指針 - 不存放數據 156 157 //在集合 (set)中,還有另一種方式查看一個數是否在集合中: 158 159 for(int i=0; i<10; i++) 160 161 if(S.count(i)) cout << i << "?is?an?element?of?S." << endl; 162 163 else cout << i << "?is?not?an?element?of?S." << endl; 164 165 // count(x),如果 x 存在則返回 1,不存在則返回 0 166 167 /*輸出結果: 168 0 is an element of S. 169 1 is an element of S. 170 2 is not an element of S. 171 3 is not an element of S. 172 4 is an element of S. 173 5 is not an element of S. 174 6 is not an element of S. 175 7 is not an element of S. 176 8 is not an element of S. 177 9 is an element of S.*/ 178 179 //在集合 (set)中,亦可以直接支持lower_bound 操作: 180 181 182 p = S.lower_bound(20); // 假如我們想要找一個集合中不小於 20 的數 183 184 cout << "The?first?element?not?lower?than?20:?" << *p << endl; 185 186 S.delete(p); // 刪除一個數:使用指針的方式 187 p = S.lower_bound(20); // 再次尋找 188 cout << "The?first?element?not?lower?than?20:?" << *p << endl; 189 190 p = S.lower_bound(100); // 假如我們想要找一個集合中不小於 100 的數 191 cout << (p == S.end()) << endl; // 假如沒有數比它大,則返回尾指針 192 193 /*輸出結果: 194 The first element not lower than 20: 25 195 The first element not lower than 20: 36 196 1*/ 197 198 vector<int>::iterator vi; // 定義叠代器,直接在後面加::iterator 199 vector<int> V; // 定義容器 200 201 for(int i=1; i<10; i++) V.push_back(i * 3 - 2); // 新增值 202 for(vi = V.begin(); vi != V.end(); ++vi) cout << *vi << "?"; 203 204 cout << endl; // 按順序輸出容器中的值 205 206 // 特殊的叠代器:V.begin(), V.end()(左閉右開) 207 // 叠代器可以自增:往後挪一位 208 // 不同容器的叠代器存在著區別 209 210 /*輸出結果: 211 1 4 7 10 13 16 19 22 25*/ 212 213 // STL 中每個容器都有對應的叠代器。 214 set<int> S; // 定義容器 215 set<int>::iterator p; // 定義叠代器 216 217 for(int i=0; i<10; i++) S.insert(i * i); // 插入平方數 218 p = S.begin(), ++p; 219 S.erase(p); // 第一種刪除方式(叠代器) :刪除第二個元素 220 S.erase(49); // 第二種刪除方式(值刪除) :刪除 49 221 cout << "The?Sequence?:?"; 222 223 for(p = S.begin(); p != S.end() ; ++p) cout << *p << "?"; 224 cout << endl; // 順序輸出 225 226 /*輸出結果: 227 The Sequence : 0 4 9 16 25 36 64 81*/ 228 229 }

相信你會收獲許多******

想繼續收獲,請搜索 “數據結構” 與本園

STL