C++stl简单使用

  1 //1.sort函数排序
  2 /*
  3 #include <iostream>
  4 #include <algorithm>
  5 using namespace std;
  6 int main()
  7 {
  8   int a[] = { 2,0,3,1,8,2,4,0 };
  9   sort(a, a + 3);//对前三个数排序
 10   for (int i=0;i<8;++i)
 11   {
 12     cout << a[i] << " ";
 13   }
 14   return 0;
 15 }*/
 16 //2.字符串处理
 17 /*
 18 #include <iostream>
 19 #include <string>
 20 using namespace std;
 21 int main()
 22 {
 23   string s = "hello world!";
 24   cout<<s<<endl;
 25   string a;
 26   getline(cin, a);//获取一行数据
 27   cout << a << endl;
 28   return 0;
 29 }*/
 30 /*
 31 #include <iostream>
 32 #include <string>
 33 #include <algorithm>
 34 using namespace std;
 35 int main()
 36 {
 37   string s;
 38   s += "小明";
 39   s =s+"小红";
 40   int a = 5;
 41   s += (a + '0');//把a加入到字符串中
 42   cout << s << endl;
 43   string x = "741852";
 44   sort(x.begin(),x.end());//排序
 45   cout << x << endl;
 46   string n = "7a4185b2";
 47   n.erase(n.begin());//删除第一个元素
 48   n.erase(--n.end());//删除最后一个元素
 49   cout << n << endl;
 50   string m = "147258369";
 51   m = m.substr(2, 5);//取72583,从2位置开始往后面截断5个
 52   cout << m<<endl;
 53   m = m.substr(2, -1);//索引为2,截断到最后
 54   cout << m << endl;
 55   return 0;
 56 }*/
 57 //循环
 58 /*
 59 #include <iostream>
 60 using namespace std;
 61 int main()
 62 {
 63   string s="147258369";
 64   for (int i=0;i<s.length();++i)
 65   {
 66     cout <<s[i] ;
 67   }
 68   cout << endl;
 69   //迭代器
 70   for (string ::iterator it=s.begin();it!=s.end();++it)
 71   {
 72     cout << *it;
 73   }
 74   cout << endl;
 75     //迭代器化简
 76   for (auto it=s.begin();it!=s.end();++it)
 77   {
 78     cout << *it;
 79   }
 80   cout << endl;
 81   //C++11新特性
 82   for (auto x:s)
 83   {
 84     cout << x;
 85   }
 86   return 0;
 87 }*/
 88 //3.vector.vector相当于数组,模板类型相当于存放的内容
 89 /*
 90 #include <iostream>
 91 #include <vector>
 92 using namespace std;
 93 int main()
 94 {
 95   //1.vector构造
 96   vector <int> v0;//定义一个空的vector
 97   vector<int> v2(4);//定义一个4个大小的vector,初始为0
 98   for (auto x:v2)
 99   {
100     cout << x;
101   }
102   cout << endl;
103   vector<int> v3(4, 6);//定义一个4个大小的vector,初始值为6
104   for (int i=0;i<v3.size();++i)
105   {
106     cout << v3[i];
107   }
108   cout << endl;
109   vector<int> v{ 8,2,3,4,5 };//定义一个vector,数字为1,2,3,4,5
110   for (auto it=v.begin();it!=v.end();++it)
111   {
112     cout << *it;
113   }
114   cout << endl;
115   cout << v[1];//取索引为1的元素
116   cout << endl;
117   cout << v.at(2);//取索引为1的元素
118   return 0;
119 }*/
120 /*
121 #include <iostream>
122 #include <vector>
123 using namespace std;
124 int main()
125 {
126   //push_back追加内容
127   vector <int> v;
128   v.push_back(1);
129   v.push_back(2);
130   v.push_back(3);
131   v.push_back(4);
132   v.push_back(6);
133   for (int i=0;i<v.size();++i)
134   {
135     cout << v[i];
136   } 
137   cout << endl;
138   v.resize(10);//进行重置大小,不赋值默认为0
139   for (int i = 0; i < v.size(); ++i)
140   {
141     cout << v[i];
142   }
143   cout << endl;
144   v.erase(v.begin());
145   v.erase(--v.end());
146   for (int i = 0; i < v.size(); ++i)
147   {
148     cout << v[i];
149   }
150   return 0;
151 }*/
152 /*
153 #include <iostream>
154 #include <vector>
155 #include <algorithm>
156 using namespace std;
157 int main()
158 {
159   //push_back追加内容
160   vector <int> v;
161   v.push_back(1);
162   v.push_back(2);
163   v.push_back(3);
164   v.push_back(4);
165   v.push_back(6);
166   cout << v.front()<<" ";//获取第一个元素
167   cout << v[0]<<" ";
168   cout << *v.begin()<<" ";
169   cout << v.back()<<" ";//获取最后一个元素
170   cout << v[v.size()-1]<<" ";
171   cout << *--v.end()<<endl;
172   //排序
173   //vector<int> v{ 1, 4,  7, 2, 5, 8, 3, 6, 9 };
174   sort(v.begin(), v.end(), less<int>());//从小到大排列
175   for (auto x : v)
176   {
177     cout << x;
178   }
179   cout << endl;
180   sort(v.begin(), v.end(), greater<int>());//从大到小排列
181   for (int i=0;i<v.size();++i)
182   {
183     cout << v[i];
184   }
185   return 0;
186 }*/
187 //4.栈
188 /*
189 #include <iostream>
190 #include <stack>
191 using namespace std;
192 int main()
193 {
194   stack<int> s;
195   //s.push(1);
196   s.push(2);
197   s.push(3);
198   s.push(4);
199   s.push(5);
200   cout << s.top() << endl;
201   s.pop();
202   cout << s.top() << endl;
203   cout << s.size() << endl;
204   bool a = s.empty();
205   cout << a << endl;
206   return 0;
207 }*/
208 //进制转换(十转二)
209 /*
210 #include <iostream>
211 #include <stack>
212 using namespace std;
213 int itob(int decimal)
214 {
215   stack<int> s;
216   int res = 0;
217   while (decimal!=0)
218   {
219     s.push(decimal % 2);
220     decimal /= 2;
221   }
222   while (!s.empty())
223   {
224     res = res * 10 + s.top();
225     s.pop();
226   }
227   return res;
228 }
229 int main()
230 {
231   int n;
232   cin >> n;
233   cout << itob(n);
234   return 0;
235 }*/
236 //输入一行字符串,将字符串逆序打印
237 //输入:hello world
238 //输出:world hello
239 /*
240 #include <iostream>
241 #include <stack>
242 #include <sstream>
243 using namespace std;
244 int main()
245 {
246   string str;
247   stack<string> s;
248   getline(cin, str);
249   stringstream ss;
250   ss << str;
251   while (ss>>str)
252   {
253     s.push(str);
254   }
255   while (!s.empty())
256   {
257     cout << s.top();
258     s.pop();
259     if (s.size() != 0) cout << " ";
260   }
261   return 0;
262 }*/
263 /*#include <iostream>
264 #include <stack>
265 #include <sstream>
266 using namespace std;
267 int main()
268 {
269   //1.字符串转数字
270   string s = "123456";
271   int i;
272   stringstream ss;
273   ss << s; // 将string类型的值放入输入流中
274   ss >> i;// 从sstream中抽取前面插入的string类型的值,赋给int类型
275   cout << i << endl;
276   //方法2
277   string a = "123455";
278   int b = stoi(a);
279   cout << b << endl;
280   //数字转字符串
281   int c = 1234;
282   string out;
283   stringstream aa;
284   aa << c;
285   aa >> out;
286   cout << out << endl;
287   //方法2
288   int d=23123456;
289   cout << to_string(d) << endl;
290   return 0;
291 }*/
292 //4.队列
293 /*
294 #include<iostream>
295 #include<queue>
296 using namespace std;
297 int main()
298 {
299   queue<int> q;
300   q.push(5);
301   q.push(6);
302   q.push(7);
303   cout << q.front() << endl;
304   cout << q.size() << endl;
305   return 0;
306 }*/
307 /*
308 #include<iostream>
309 #include <map>//树状表
310 using namespace std;
311 int main()
312 {
313   map<int, int> m;//有序的,树状结构
314   m[6] = 3;
315   m[5] = 8;
316   m[4] = 9;
317   for (auto it=m.begin();it!=m.end();it++)
318   {
319     cout << it->first << " " << it->second << endl;
320   }
321   for (auto tmp : m)
322   {
323     cout << tmp.first << " " << tmp.second << endl;
324   }
325   return 0;
326 }*/
327 /*
328 #include<iostream>
329 #include <unordered_map>//哈希表
330 using namespace std;
331 int main()
332 {
333   unordered_map<int, int> m;//无序的
334   m[6] = 3;
335   m[5] = 8;
336   m[4] = 9;
337   m[2] = 6;
338   m[1] = 0;
339   for (auto it = m.begin(); it != m.end(); it++)
340     cout << it->first << " " << it->second << endl;
341   for (auto tmp : m) {
342     cout << tmp.first << " " << tmp.second << endl;
343   }
344   return 0;
345 }*/
346 //pair的用法,将map转成vector进行排序
347 /*
348 #include<iostream>
349 #include <unordered_map>//哈希表
350 #include <algorithm>
351 using namespace std;
352 bool cmp(pair<int, int> a, pair<int, int> b)
353 {
354   return a.first > b.first;
355 }
356 int main()
357 {
358   unordered_map<int, int> m;//无序的
359   m[4] = 9;
360   m[2] = 6;
361   m[6] = 3;
362   m[5] = 8;
363   m[1] = 0;
364   vector<pair<int, int>> v(m.begin(), m.end());
365   sort(v.begin(), v.end(), cmp);
366   for (auto tmp : v) {
367     cout << tmp.first << tmp.second << endl;
368   }
369   return 0;
370 }*/
371 //集合,计数去重
372 /*
373 #include<iostream>
374 #include <set>
375 #include <unordered_set>
376 using namespace std;
377 int main()
378 {
379   set<int> s;//树状结构,有序
380   unordered_set<int> s2;//哈希结构,无序,快
381   s.insert(3);
382   s.insert(4);
383   s.insert(4);
384   s.insert(4);
385   s.insert(5);
386   cout << s.size() << endl;
387   for (auto tmp:s)
388   {
389     cout << tmp << " ";
390   }
391   cout << endl;
392   for (auto it=s.begin();it!=s.end();++it)
393   {
394     cout << *it << " ";
395   }
396   cout << endl;
397   return 0;
398 }*/
399 //deque双端队列
400 /*
401 #include<iostream>
402 #include <deque>
403 #include <algorithm>
404 using namespace std;
405 int main()
406 {
407   deque<int> d;
408   //4 9 1 2
409   d.push_back(1);
410   d.push_back(6);
411   d.push_back(5);
412   d.push_back(7);
413   d.push_back(2);
414   d.push_front(9);
415   d.push_front(5);
416   d.push_front(3);
417   d.push_front(6);
418   d.push_front(4);
419   d.pop_back();
420   d.pop_front();
421   for (auto tmp:d)
422   {
423     cout << tmp <<" ";
424   }
425   cout << endl;
426   sort(d.begin(), d.end(), greater<int>());
427   for (auto it=d.begin();it!=d.end();++it)
428   {
429     cout << *it <<" ";
430   }
431   cout << endl;
432   return 0;
433 }*/
434 //list双向链表
435 #include<iostream>
436 #include <list>
437 using namespace std;
438 int main()
439 {
440   list<int> li;
441   li.push_back(6);
442   li.push_front(5);
443   li.emplace_front(9);//在开头添加元素
444   li.emplace_back(10);
445   li.insert(++li.begin(), 2);
446   for (auto tmp:li)
447   {
448     cout << tmp << " ";
449   }
450   cout << endl;
451   for (auto it = li.begin(); it != li.end(); it++) cout << *it << endl;
452   return 0;
453 }

 

posted @ 2020-11-22 09:40  丁帅帅dss  阅读(100)  评论(0)    收藏  举报