STL

  1 #include<iostream>
  2 #include<vector>
  3 #include<list>
  4 #include<set>
  5 #include<map>
  6 #include<string>
  7 #include<algorithm>
  8 using namespace std;
  9 
 10 /**
 11 *
 12 *判断vector正反向元素是否一样
 13 */
 14 template<class T>
 15 bool fun1(const vector<T> v) {
 16     for (int i = 0; i < v.size() / 2; i++) {
 17         if (v.at(i) != v.at(v.size() - i - 1)) {
 18             return false;
 19         }
 20         else {
 21             return true;
 22         }
 23     }
 24 }
 25 
 26 /**
 27 *判断list正反向元素是否一样
 28 */
 29 template<class T>
 30 bool fun2(const list<T> l) {
 31     auto start = l.begin();
 32     auto tail = l.end();
 33     while (1) {
 34         tail--;
 35         if (start == tail) break;
 36         if ((*start) != (*tail)) {
 37             return false;
 38         }
 39         start++;
 40         if (start == tail) break;
 41     }
 42     return true;
 43 }
 44 
 45 
 46 template<class T>
 47 void test1(const vector<T>& v) {
 48     cout << fun1(v) << endl;
 49 }
 50 
 51 template<class T>
 52 void test2(const list<T> l) {
 53     cout << fun2(l) << endl;
 54 }
 55 
 56 
 57 template<class T>
 58 void test3(vector<T>& v) {
 59 
 60     int n;
 61     cin >> n;
 62     T temp;
 63     for (int i = 0; i < n; i++) {
 64         cin >> temp;
 65         v.push_back(temp);
 66     }
 67     cout << "Before sort ! " << endl;
 68     for (auto i : v) {
 69         cout << i << ' ';
 70     }
 71     sort(v.begin(), v.end());
 72     cout << endl << "After sort ! " << endl;
 73     for (auto i : v) {
 74         cout << i << ' ';
 75     }
 76     cout << endl << "Please input a number you want to search : ";
 77     auto ite = v.begin();
 78     while (cin >> n) {
 79         if ((ite = find(v.begin(), v.end(), n)) != v.end()) {
 80             cout << *ite;                                                        /*****/
 81         }
 82         else {
 83             cout << "No find!";
 84         }
 85         cout << endl << "Please input a number you want to search : ";
 86     }
 87 }
 88 
 89 
 90 /**
 91 *求两个集合的交集
 92 */
 93 void test4() {
 94     set<int> s1 = { 9,5,6,3,2,1,8,4,7,1,5,2,4,6,5,4,7,8,5,9,6,4,2 };
 95     set<int> s2 = { 1,0,10,5,6,4,3,2,8,7,9,100,5,8, 5,8,5,5,8,25, };
 96     set<int> s3;
 97     cout << "s1 : ";
 98     for (auto ele : s1) {
 99         cout << ele << ' ';
100     }
101     cout << endl << "s2 : ";
102     for (auto ele : s2) {
103         cout << ele << ' ';
104     }
105     cout << endl;
106     for (auto ele : s1) {
107         if (s2.find(ele) != s2.end()) {
108             s3.insert(ele);
109         }
110     }
111     cout << endl << "s3 : ";
112     for (auto ele : s3) {
113         cout << ele << ' ';
114     }
115 }
116 
117 /**
118 *测试map类
119 */
120 void test5() {
121     map<string, string> m;
122     m.insert(pair<string, string>("zero", ""));
123     m.insert(pair<string, string>("one", ""));
124     m.insert(pair<string, string>("two", ""));
125     m.insert(pair<string, string>("three", ""));
126     m.insert(pair<string, string>("four", ""));
127     m.insert(pair<string, string>("five", ""));
128     m.insert(pair<string, string>("six", ""));
129     m.insert(pair<string, string>("seven", ""));
130     m.insert(pair<string, string>("eight", ""));
131     m.insert(pair<string, string>("nine", ""));
132     m.insert(pair<string, string>("ten", ""));
133     string key;
134     pair<string, string> a;
135     while (cin >> key) {
136         if (m.find(key) != m.end()) {
137             a = *(m.find(key));
138             cout << a.second << endl;
139         }
140         else {
141             cout << "No find!" << endl;
142         }
143     }
144 }
145 
146 
147 
148 /**
149 *计算一个文章中某个单词出现的频率
150 */
151 void test6() {
152     map<string, int> m;
153     pair<string, int> a;
154     pair<std::map<string, string>::iterator, bool> judge;
155     vector<string> v;
156     string temp;
157     int num = 0;
158     while (cin >> temp) {
159         a.first = temp;
160         a.second = 0;
161         m.insert(a);
162         v.push_back(temp);
163     }
164     cin.clear();
165     for (auto ele : v) {
166         m[ele]++;
167     }
168     cout << "Please input the word you want to see : ";
169     while (cin >> temp) {
170         cout << m[temp];
171         cout << "Please input the word you want to see : ";
172     }
173 }
174 
175 int main() {
176     int a[10] = { 1,2,3,4,5,6 };
177     vector<int> v(a, a + 10);
178     vector<int> vv;
179     list<int> l;
180     l.assign(a, a + 10);
181     //test1(v);
182     //test2(l);
183     //test3(vv);
184     //test4();
185     //test5();
186     test6();
187 
188 }

 

posted @ 2016-06-30 18:41  阙建明  阅读(130)  评论(0编辑  收藏  举报