首页 > 代码库 > 随手贴点代码记录一下吧,其实啥用没有

随手贴点代码记录一下吧,其实啥用没有

  1 #include <iostream>
  2 #include <string>
  3 #include <unordered_map>
  4 #include <fstream>
  5 #include <sstream>
  6 
  7 using namespace std;
  8 void transform(ifstream &mf, ifstream &wf);
  9 
 10 int main()
 11 {
 12     ifstream f1("d:\\m.txt");
 13     ifstream f2("d:\\t.txt");
 14     transform(f1,f2);
 15     return 0;
 16 }
 17 
 18 unordered_map<string, string> buildMap(ifstream& fs)
 19 {
 20     unordered_map<string, string> map;
 21     string line, key;
 22     while (fs >> key) {
 23         getline(fs, line);
 24         map[key] = line.substr(1);
 25     }
 26     return map;
 27 }
 28 
 29 string transword(string word, unordered_map<string,string>& map)
 30 {
 31     auto it = map.find(word);
 32     return it == map.end() ? word : map[word];
 33 }
 34 
 35 void transform(ifstream &mf, ifstream &wf)
 36 {
 37     unordered_map<string, string> map = buildMap(mf);
 38     string word, line;
 39     while (getline(wf, line)) {
 40         bool head = true;
 41         istringstream ss(line);
 42         while (ss >> word) {
 43             if (head)
 44                 head = false;
 45             else
 46                 cout << " ";
 47             cout << transword(word,map);
 48         }
 49         cout << endl;
 50     }
 51     return;
 52 }
 53 
 54 #include <iostream>
 55 #include <map>
 56 #include <string>
 57 
 58 using namespace std;
 59 int main()
 60 {
 61     multimap<string, string> m;
 62     m = { {"jia","jaa"}, {"yi","yaa"},{"bing","baa"},{"yi","bb"} ,{ "yi","yaa" } ,{ "yi","bb" } };
 63     //pair<string, string> p = { "yi","bb" };
 64     //for (auto it = m.find(p.first); (it != m.end()) && (it->first == p.first); )
 65     //{
 66     //    if (it->second == p.second)
 67     //        it = m.erase(it);
 68     //    else
 69     //        ++it;
 70     //}
 71 
 72     string name = m.begin()->first;
 73     cout << name << ":\n";
 74 
 75     for (const auto &e : m) {
 76         if (e.first == name)
 77             cout << e.second << "\t";
 78         else {
 79             name = e.first;
 80             cout << "\n" << name << ": \n";
 81             cout << e.second << "\t";
 82         }
 83     }
 84     return 0;
 85 }
 86 
 87 map<string, vector<int>> m;
 88 auto i = m.find(k);
 89 map<string, vector<int>>::iterator i = m.find(k);
 90 
 91 key min
 92 lower_bound m.begin()
 93 upper_bound m.begin()
 94 equal_range pair<m.begin() m.begin()>
 95 
 96 key max
 97 lower_bound m.end()
 98 upper_bound m.end()
 99 equal_range pair<m.end() m.end()>
100 
101 key 
102 lower_bound m.in()
103 upper_bound m.in()
104 equal_range pair<m.in() m.in()>
105 
106 #include <iostream>
107 #include <map>
108 #include <vector>
109 #include <string>
110 
111 using namespace std;
112 int main()
113 {
114     multimap<string, vector<string>> fm;
115     string x;
116     while ([&x]()->bool {
117         std::cout << "Enter 姓?" << endl;
118         return (cin >> x&&x != "q");
119     }()) {
120         std::cout << "Enter 名?" << endl;
121         string n;
122         auto it = fm.find(x);
123         it == fm.cend() ? it = fm.emplace(x,vector<string>()) : it ;
124         while (cin >> n&&n != "q")
125             it->second.push_back(n);
126         cin.clear();
127     }
128     std::cout << "**********************" << endl;
129     for (const auto &s : fm) {
130         std::cout << s.first << endl;
131         for (const auto &s : s.second) {
132             std::cout << s << " ";
133             std::cout << endl;
134         }
135     }
136     return 0;
137 }
138 
139 #include <iostream>
140 #include <unordered_map>
141 #include <string>
142 #include <algorithm>
143 
144 std::string sconvert(std::string &s)
145 {
146     auto itl = s.begin();
147     while (ispunct(*itl)) {
148         ++itl;
149     }
150     auto itr = s.rbegin();
151     while (ispunct(*itr)) {
152         ++itr;
153     }
154 
155     std::string s2(itl, itr.base());
156     for (auto &c : s2) {
157         if (isupper(c))
158             c = tolower(c);
159     }
160 
161     return s2;
162 }
163 
164 int main()
165 {
166     std::unordered_map<std::string, unsigned> wc;
167     std::string words;
168     while (std::cin >> words) {
169         words = sconvert(words);
170         //++wc[words];
171         auto re = wc.insert({ words,1 });
172         if (!re.second)
173             ++(re.first->second);
174     }
175     std::for_each(wc.cbegin(), wc.cend(), [](const std::pair<std::string, unsigned> p) 
176     {std::cout << p.first << "\t" << p.second<<std::endl; });
177     return 0;
178 }
179 
180 #include <vector>
181 #include <string>
182 #include <iostream>
183 #include <utility>
184 using namespace std;
185 int main()
186 {
187     vector<string> sv = { "ggg","ere","dfdf","444" };
188     vector<int> iv = { 1,3,4 };
189     vector<pair<string, int>> v;
190     for (int i = 0; i != sv.size() && i != iv.size(); ++i)
191         //v.push_back(make_pair(sv[i], iv[i]));
192         //v.push_back(pair<string, int>(sv[i], iv[i]));
193         v.push_back({ sv[i], iv[i] });
194     return 0;
195 }
196 
197 #include <vector>
198 #include <string>
199 #include <iostream>
200 #include <sstream>
201 #include <map>
202 #include <list>
203 using namespace std;
204 
205 int main()
206 {
207     map<string, list<int>> m;
208     string line;
209     string word;
210     int no = 1;
211     while (getline(cin, line)) {
212         istringstream is(line);
213         while (is >> word) {
214             m[word].push_back(no);
215         }
216         ++no;
217     }
218     for (const auto &e : m) {
219         cout << e.first << ":\n";
220         for (const auto &i : e.second)
221             cout << i << " ";
222         cout << endl;
223     }
224     return 0;
225 }
226 
227 #include <vector>
228 #include <string>
229 #include <iostream>
230 using namespace std;
231 
232 int main()
233 {
234     string word;
235     vector<string> v;
236     while (cin >> word) {
237         if (find(v.cbegin(), v.cend(), word) == v.cend())
238             v.push_back(word);
239     }
240     for (const auto &s : v)
241         cout << s << endl;
242     return 0;
243 }
244 
245 #include <iostream>
246 #include <map>
247 #include <vector>
248 #include <string>
249 
250 using namespace std;
251 int main()
252 {
253     map<string, vector<string>> fm;
254     string x;
255     while ([&x]()->bool {
256         cout << "Enter 姓?" << endl;
257         return (cin >> x&&x != "q");
258     }()) {
259         cout << "Enter 名?" << endl;
260         string n;
261         while (cin >> n&&n != "q")
262             fm[x].push_back(n);
263         cin.clear();
264     }
265     cout << "**********************" << endl;
266     for (const auto &s : fm) {
267         cout << s.first << endl;
268         for (const auto &s : s.second) {
269             cout << s << " ";
270             cout << endl;
271         }
272     }
273     return 0;
274 }
275 
276 #include <iostream>
277 #include <map>
278 #include <string>
279 #include <algorithm>
280 
281 std::string sconvert(std::string &s)
282 {
283     auto itl = s.begin();
284     while (ispunct(*itl)) {
285         ++itl;
286     }
287     auto itr = s.rbegin();
288     while (ispunct(*itr)) {
289         ++itr;
290     }
291 
292     std::string s2(itl, itr.base());
293     for (auto &c : s2) {
294         if (isupper(c))
295             c = tolower(c);
296     }
297 
298     return s2;
299 }
300 
301 int main()
302 {
303     std::map<std::string, unsigned> wc;
304     std::string words;
305     while (std::cin >> words) {
306         words = sconvert(words);
307         ++wc[words];
308     }
309     std::for_each(wc.cbegin(), wc.cend(), [](const std::pair<std::string, unsigned> p) 
310     {std::cout << p.first << "\t" << p.second<<std::endl; });
311     return 0;
312 }
313 
314 #include <iostream>
315 #include <list>
316 #include <string>
317 
318 using namespace std;
319 
320 int main()
321 {
322     list<string> v{ "fsfdgdsg","dfsd","sdf","ddddddd","sdf","dfsd","23","343433","23" };
323     v.sort();
324     cout << v.size() << endl;
325     v.unique();
326     cout << v.size() << endl;
327     for (auto &s : v)
328         cout << s << endl;
329 
330     return 0;
331 }
332 
333 #include <iostream>
334 #include <vector>
335 #include <list>
336 #include <iterator>
337 #include <algorithm>
338 
339 int main()
340 {
341     std::vector<int> v{ 1,2,4,0,9 };
342 
343     //10.34
344     std::ostream_iterator<int> os(std::cout, " ");
345     copy(v.rbegin(), v.rend(), os);
346     std::cout << std::endl;
347 
348 
349     //10.35
350     for (auto it = v.cend(); it != v.begin();) {
351         std::cout << *--it << " ";
352     }
353     std::cout << std::endl;
354 
355     //10.36
356     std::list<int> lt{ 1,2,4,0,9 };
357     auto it = std::find(lt.rbegin(), lt.rend(), 0);
358     std::cout << *it++ << std::endl;
359     std::cout << *it << std::endl;
360 
361     //10.37
362     std::vector<int> ivec{ 1, 2, 3, 4, 5, 6, 7,8,9,0 };
363     std::list<int> l;
364     std::reverse_copy(ivec.begin() + 3, ivec.begin() + 7, back_inserter(l));
365     for (auto it = l.cbegin(); it != l.cend();++it) {
366         std::cout << *it << " ";
367     }
368     std::cout << std::endl;
369 
370     return 0;
371 }
372 
373 #include <iostream>
374 #include <algorithm>
375 #include <vector>
376 #include <list>
377 #include <iterator>
378 
379 
380 int main()
381 {
382     std::vector<int> v1{ 1, 2, 3, 4, 5, 6, 7,8,9 };
383     std::list<int> v2;
384     std::list<int> v3;
385     std::list<int> v4;
386     std::list<int> v5;
387 
388     copy(v1.begin(), v1.end(), back_inserter(v2));
389     copy(v1.begin(), v1.end(), front_inserter(v3));
390     copy(v1.begin(), v1.end(), inserter(v4, v4.end()));
391     copy(v1.begin(), v1.end(), inserter(v5, v5.begin()));
392     for (auto i : v2)
393         std::cout << i << std::endl;
394     std::cout << std::endl;
395     for (auto i : v3)
396         std::cout << i << std::endl;
397     std::cout << std::endl;
398     for (auto i : v4)
399         std::cout << i << std::endl;
400     std::cout << std::endl;
401     for (auto i : v5)
402         std::cout << i << std::endl; 
403     return 0;
404 }
405 
406 #include <iostream>
407 #include <algorithm>
408 #include <vector>
409 #include <list>
410 #include <string>
411 #include <functional>
412 
413 using namespace std;
414 using namespace placeholders;
415 
416 bool check_size(int i, const string &s)
417 {
418     return i > s.size();
419 }
420 
421 int main()
422 {
423     vector<int> v{ 1,2,3,4,6,4,2 };
424     vector<int>::const_iterator it = find_if(v.cbegin(), v.cend(), bind(check_size, _1, "dfd"));
425     cout << *it << endl;
426     return 0;
427 }
428 
429 #include <iostream>
430 using namespace std;
431 int main()
432 {
433     int i = 10;
434 
435     auto ff=[&i]() {if (i != 0) { --i; return false; }
436     else return true; };
437     while(!ff())
438         cout << i << endl;
439 
440     return 0;
441 }
442 
443 #include <iostream>
444 #include <algorithm>
445 #include <vector>
446 #include <list>
447 #include <string>
448 
449 using namespace std;
450 
451 int main()
452 {
453     vector<string> v{ "fsfdgdsg","dfsd","sdf","ddddddd","23","343433","34" };
454     int sz = 6;
455     auto i = count_if(v.cbegin(), v.cend(), [=](const string& s) {return s.size() > sz; });
456     cout << i << endl;
457     return 0;
458 }
459 
460 #include <iostream>
461 #include <algorithm>
462 #include <vector>
463 #include <list>
464 #include <string>
465 #include <functional>
466 
467 using namespace std;
468 void bigger(vector<string> &v, vector<string>::size_type sz);
469 
470 int main()
471 {
472     vector<string> v{ "fsfdgdsg","dfsd","sdf","ddddddd","23","343433","34" };
473     bigger(v, 6);
474     return 0;
475 }
476 
477 bool check_size(const string &s, string::size_type sz)
478 {
479     return s.size() >= sz;
480 }
481 
482 void bigger(vector<string> &v, vector<string>::size_type sz)
483 {
484     sort(v.begin(), v.end());
485     auto it = unique(v.begin(), v.end());
486     v.erase(it, v.end());
487     //stable_sort(v.begin(), v.end(), [](const string &s1, const string &s2) {return s1.size() < s2.size(); });
488 
489     //auto it2 = find_if(v.begin(), v.end(), [sz](const string &s) {return s.size() >= sz; });
490     //for_each(it2, v.end(), [](const string &s) {cout << s << " "; });
491     auto it2 = stable_partition(v.begin(), v.end(), bind(check_size, placeholders::_1, sz));
492     for_each(v.begin(), it2, [](const string &s) {cout << s << " "; });
493 
494 }
495 
496 #include <iostream>
497 #include <algorithm>
498 #include <vector>
499 #include <list>
500 #include <string>
501 
502 using namespace std;
503 
504 bool foo(const string &s)
505 {
506     return 5 <= s.size();
507 }
508 
509 int main()
510 {
511     vector<string> words = { "23515","555","dfd","12","hello","fddfdfdf","ewreret","23515","555","dfd","12" };
512     vector<string>::iterator lin = partition(words.begin(), words.end(), foo);
513     for (auto it=words.begin(); it != lin; ++it)
514         cout << *it << " ";
515     cout << endl;
516     return 0;
517 }
518 
519 #include <iostream>
520 #include <algorithm>
521 #include <vector>
522 #include <list>
523 #include <string>
524 #include <numeric>
525 
526 using namespace std;
527 vector<string> elim(istream& is);
528 void print(vector<string>& v);
529 bool isShorter(const string& s1, const string& s2);
530 
531 int main()
532 {
533     vector<string> vec=elim(cin);
534     stable_sort(vec.begin(), vec.end(), isShorter);
535     print(vec);
536     return 0;
537 }
538 
539 vector<string> elim(istream& is)
540 {
541     vector<string> v;
542     string s;
543     while (is >> s)
544         v.push_back(s);
545     cout << "vector:" << v.size() << endl;
546     print(v);
547 
548     sort(v.begin(), v.end());
549     cout << "sort:" << v.size() << endl;
550     print(v);
551 
552     auto it = unique(v.begin(), v.end());
553     cout << "unique:" << v.size() << endl;
554     print(v);
555 
556     v.erase(it, v.cend());
557     cout << "erase:" << v.size() << endl;
558     print(v);
559 
560     return v;
561 }
562 
563 void print(vector<string>& v)
564 {
565     for (const auto s : v)
566         cout << s << " ";
567     cout << endl;
568     return;
569 }
570 
571 bool isShorter(const string& s1, const string& s2)
572 {
573     return s1.size() < s2.size();
574 }
575 
576 #include <iostream>
577 #include <algorithm>
578 #include <vector>
579 #include <list>
580 #include <string>
581 #include <numeric>
582 #include <memory>
583 
584 using namespace std;
585 shared_ptr<vector<string>> elim(istream& is);
586 void print(vector<string>& v);
587 bool isShorter(const string& s1, const string& s2);
588 
589 int main()
590 {
591     auto vec = elim(cin);
592     stable_sort(vec->begin(), vec->end(), isShorter);
593     print(*vec);
594     return 0;
595 }
596 
597 shared_ptr<vector<string>> elim(istream& is)
598 {
599     shared_ptr<vector<string>> v(new vector<string>);
600     string s;
601     while (is >> s)
602         v->push_back(s);
603     cout << "vector:" << v->size() << endl;
604     print(*v);
605 
606     sort(v->begin(), v->end());
607     cout << "sort:" << v->size() << endl;
608     print(*v);
609 
610     auto it = unique(v->begin(), v->end());
611     cout << "unique:" << v->size() << endl;
612     print(*v);
613 
614     v->erase(it, v->cend());
615     cout << "erase:" << v->size() << endl;
616     print(*v);
617 
618     return v;
619 }
620 
621 void print(vector<string>& v)
622 {
623     for (const auto s : v)
624         cout << s << " ";
625     cout << endl;
626     return;
627 }
628 
629 bool isShorter(const string& s1, const string& s2)
630 {
631     return s1.size() < s2.size();
632 }

 

随手贴点代码记录一下吧,其实啥用没有