1. 程式人生 > 其它 >C++學習記錄(十一)容器deque、list、stack、set、map,I/O檔案流,多執行緒入口

C++學習記錄(十一)容器deque、list、stack、set、map,I/O檔案流,多執行緒入口

這是第十一天的學習。

  1 #include <iostream>
  2 #include <deque>
  3 #include <algorithm>
  4 #include <list>
  5 #include <stack>
  6 #include <queue>
  7 #include <set>
  8 #include <map>
  9 
 10 using namespace std;
 11 
 12 void Show(int& value)
 13 {
14 cout << value << endl; 15 } 16 17 void test() 18 { 19 deque<int> _deque; 20 _deque.push_back(10); 21 _deque.push_back(30); 22 _deque.push_back(50); 23 _deque.push_back(70); 24 _deque.push_back(90); 25 for_each(_deque.begin(),_deque.end(),&Show);
26 cout << "---***---" << endl; 27 sort(_deque.begin(),_deque.end(),greater<int>()); 28 for_each(_deque.begin(),_deque.end(),&Show); 29 } 30 31 void test1() 32 { 33 list<int> _list; 34 _list.push_back(20); 35 _list.push_back(40); 36 _list.push_back(10
); 37 _list.push_back(90); 38 _list.push_back(60); 39 _list.sort(); 40 for_each(_list.begin(),_list.end(),&Show); 41 cout << "---***---" << endl; 42 _list.sort(greater<int>()); 43 for_each(_list.begin(),_list.end(),&Show); 44 cout << "---***---" << endl; 45 _list.reverse(); 46 for_each(_list.begin(),_list.end(),&Show); 47 cout << "---***---" << endl; 48 for (auto it = _list.begin(); it!=_list.end(); it++) 49 { 50 cout << *it << endl; 51 } 52 cout << "---***---" << endl; 53 cout << _list.size() << endl; 54 } 55 56 void test2() 57 { 58 stack<int> _stack; 59 _stack.push(55); 60 _stack.push(17); 61 _stack.push(66); 62 _stack.push(77); 63 while (!_stack.empty()) 64 { 65 cout << _stack.top() << endl; 66 _stack.pop(); 67 } 68 } 69 70 void test3() 71 { 72 queue<int> _qu; 73 _qu.push(10); 74 _qu.push(20); 75 _qu.push(30); 76 while (!_qu.empty()) 77 { 78 cout << _qu.front() << endl; 79 _qu.pop(); 80 } 81 } 82 83 class Stu 84 { 85 private: 86 int id; 87 string name; 88 int age; 89 public: 90 Stu(int id = 100, string name = "", int age = 0) 91 { 92 this->id = id; 93 this->name = name; 94 this->age = age; 95 } 96 void showInfo() const 97 { 98 cout << "Number: " << id << ", name: " << name << ", age: " << age << endl; 99 } 100 101 template<class T> 102 friend class My_compair; 103 }; 104 template<class T> 105 class My_compair 106 { 107 public: 108 //返回值為布林,稱為謂詞 109 //bool operator()(T t1, T t2) { return t1.id < t2.id; } 110 bool operator()(const T& t1, const T& t2) { return t1.id < t2.id; } 111 }; 112 113 void test4() 114 { 115 //set<int> _set; 116 // 從大到小排序 117 set<int, greater<int>> _set; 118 _set.insert(10); 119 _set.insert(90); 120 _set.insert(30); 121 _set.insert(70); 122 _set.insert(50); 123 for_each( _set.begin(), _set.end(), [=](int value){ 124 cout << value << endl;} 125 ); 126 cout << "---***---" << endl; 127 128 auto it = _set.find(50); 129 if( it != _set.end()) { cout << "Finded this value! " << *it << endl; } 130 else { cout << "Not find this value! " << endl; } 131 cout << "---***---" << endl; 132 133 // set中的比較器,只能為函式物件,不能為函式指標 134 135 set<Stu, My_compair<Stu>> s1; 136 s1.insert(Stu(101, "zhangsan", 18)); 137 s1.insert(Stu(102, "lisi", 19)); 138 s1.insert(Stu(105, "wangwu", 21)); 139 s1.insert(Stu(104, "zhaoliu", 20)); 140 //for_each(s1.begin(),s1.end(),[&](const Stu& stu){ stu.showInfo(); } ); 141 //cout << "---***---" << endl; 142 143 pair< set<Stu, My_compair<Stu> >::iterator, bool> inst1; 144 inst1 = s1.insert(Stu(103, "songqi", 22)); 145 if (!inst1.second) { cout << "Insert failed!" << endl; } 146 else { cout << "Insert successed!" << endl; inst1.first->showInfo(); } 147 cout << "---***---" << endl; 148 149 pair<int, string> p1 = make_pair(106, "sunba"); 150 cout << p1.first << ", name: " << p1.second << endl; 151 pair<int, string> p2(107, "zhoujiu"); 152 cout << p2.first << ", name: " << p2.second << endl; 153 } 154 155 void test5() 156 { 157 //map<int, string> map1; 158 map<int, string, greater<int>> map1; 159 map1.insert(pair<int, string>(10,"xiaohong")); 160 map1.insert(make_pair(11,"xiaohei")); 161 map1.insert(map<int, string>::value_type(12,"xiaohuang")); 162 // map中鍵值是隻讀的,需要加const 163 for_each(map1.begin(),map1.end(),[=](pair<const int,string>& _map){ 164 cout << _map.first << "," << _map.second << endl; } 165 ); 166 } 167 168 int main() 169 { 170 // vector:歷史操作記錄儲存,經常檢視資料 171 // deque:排隊購票,頭尾資料快速處理 172 // list:公交車乘客儲存,平凡不確定位置元素插入 173 // set:手機遊戲個人得分記錄,高低順序排序 174 // map:按ID號儲存十萬個使用者,通過ID查詢,查詢效率高 175 176 // -1- deque雙端佇列,允許在其首尾兩端插入 177 //test(); 178 179 // -2- list可以快速插入和刪除資料 180 //test1(); 181 182 // -3- stack棧,先進後出,queue佇列,先進先出 183 //test2(); 184 //test3(); 185 186 // -4- set對所以元素鍵值排序,值唯一 187 //test4(); 188 189 // -5- map有序鍵值對容器 190 test5(); 191 192 //cout << "Hello World!" << endl; 193 return 0; 194 }
  1 #include <iostream>
  2 #include <fstream>
  3 #include <sstream>
  4 
  5 #include <thread>
  6 #include <unistd.h>
  7 
  8 using namespace std;
  9 
 10 void write()
 11 {
 12     ofstream file;
 13     file.open("./1.txt",ios_base::app);
 14     //ofstream file1("./2.txt",ios_base::app);
 15     if(!file.is_open()) { cout << "Write file failed" << endl; }
 16     file << "xieru" << endl;
 17     file.close();
 18 }
 19 
 20 void read()
 21 {
 22     ifstream file;
 23     file.open("./1.txt", ios_base::in);
 24     if(!file.is_open()) { cout << "Open file failed" << endl; }
 25 
 26     // -1-
 27     /*
 28     string filetext;
 29     while (getline(file,filetext))
 30     {
 31         cout << filetext << endl;
 32     }
 33     */
 34 
 35     // -2-
 36     stringstream ss;
 37     ss << file.rdbuf();
 38     string str = ss.str();
 39     cout << str << endl;
 40 }
 41 
 42 void show()
 43 {
 44     cout << " Thread begin running" << endl;
 45     for(int i = 0; i<10; i++)
 46     {
 47         cout << i << endl;
 48         sleep(1);
 49     }
 50     cout << " Thread end" << endl;
 51 }
 52 
 53 class A
 54 {
 55 public:
 56     void operator() ()
 57     {
 58         cout << " Thread begin running" << endl;
 59         for(int i = 0; i<10; i++)
 60         {
 61             cout << i << endl;
 62             sleep(1);
 63         }
 64         cout << " Thread end" << endl;
 65     }
 66     void showInfo()
 67     {
 68         cout << " Thread begin running" << endl;
 69         for(int i = 0; i<10; i++)
 70         {
 71             cout << i << endl;
 72             sleep(1);
 73         }
 74         cout << " Thread end" << endl;
 75     }
 76 };
 77 
 78 int main()
 79 {
 80     // I/O 操作
 81     //write();
 82     //read();
 83 
 84     // 多執行緒
 85     // -1- 使用全域性函式指標來實現執行緒入口
 86     //thread my_thread(&show);
 87     //my_thread.join();
 88 
 89     // -2- 使用匿名函式
 90     /*
 91     thread my_thread2( [=](){
 92         cout << " Thread begin running" << endl;
 93         for(int i = 0; i<10; i++)
 94         {
 95             cout << i << endl;
 96             sleep(1);
 97         }
 98         cout << " Thread end" << endl;
 99     } );
100     my_thread2.join();
101     */
102 
103     // -3- 使用仿函式
104     A a;
105     thread my_thread3(a);
106     my_thread3.join();
107 
108     // -4- 使用成員函式
109     A a1;
110     thread my_thread4(&A::showInfo, &a1);
111     my_thread4.join();
112 
113     //cout << "Hello World!" << endl;
114     return 0;
115 }