博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
随手贴点代码记录一下吧,其实啥用没有
阅读量:5318 次
发布时间:2019-06-14

本文共 12156 字,大约阅读时间需要 40 分钟。

1 #include 
2 #include
3 #include
4 #include
5 #include
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
buildMap(ifstream& fs) 19 { 20 unordered_map
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
& 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
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
55 #include
56 #include
57 58 using namespace std; 59 int main() 60 { 61 multimap
m; 62 m = { { "jia","jaa"}, { "yi","yaa"},{ "bing","baa"},{ "yi","bb"} ,{ "yi","yaa" } ,{ "yi","bb" } }; 63 //pair
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
> m; 88 auto i = m.find(k); 89 map
>::iterator i = m.find(k); 90 91 key min 92 lower_bound m.begin() 93 upper_bound m.begin() 94 equal_range pair
95 96 key max 97 lower_bound m.end() 98 upper_bound m.end() 99 equal_range pair
100 101 key 102 lower_bound m.in()103 upper_bound m.in()104 equal_range pair
105 106 #include
107 #include
108 #include
109 #include
110 111 using namespace std;112 int main()113 {114 multimap
> 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
()) : 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
140 #include
141 #include
142 #include
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
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
p) 176 {std::cout << p.first << "\t" << p.second<
181 #include
182 #include
183 #include
184 using namespace std;185 int main()186 {187 vector
sv = { "ggg","ere","dfdf","444" };188 vector
iv = { 1,3,4 };189 vector
> 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
(sv[i], iv[i]));193 v.push_back({ sv[i], iv[i] });194 return 0;195 }196 197 #include
198 #include
199 #include
200 #include
201 #include
202 #include
203 using namespace std;204 205 int main()206 {207 map
> 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
228 #include
229 #include
230 using namespace std;231 232 int main()233 {234 string word;235 vector
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
246 #include
247 #include
248 #include
249 250 using namespace std;251 int main()252 {253 map
> 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
277 #include
278 #include
279 #include
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
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
p) 310 {std::cout << p.first << "\t" << p.second<
315 #include
316 #include
317 318 using namespace std;319 320 int main()321 {322 list
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
334 #include
335 #include
336 #include
337 #include
338 339 int main()340 {341 std::vector
v{ 1,2,4,0,9 };342 343 //10.34344 std::ostream_iterator
os(std::cout, " ");345 copy(v.rbegin(), v.rend(), os);346 std::cout << std::endl;347 348 349 //10.35350 for (auto it = v.cend(); it != v.begin();) {351 std::cout << *--it << " ";352 }353 std::cout << std::endl;354 355 //10.36356 std::list
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.37362 std::vector
ivec{ 1, 2, 3, 4, 5, 6, 7,8,9,0 };363 std::list
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
374 #include
375 #include
376 #include
377 #include
378 379 380 int main()381 {382 std::vector
v1{ 1, 2, 3, 4, 5, 6, 7,8,9 };383 std::list
v2;384 std::list
v3;385 std::list
v4;386 std::list
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
407 #include
408 #include
409 #include
410 #include
411 #include
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
v{ 1,2,3,4,6,4,2 };424 vector
::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
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
444 #include
445 #include
446 #include
447 #include
448 449 using namespace std;450 451 int main()452 {453 vector
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
461 #include
462 #include
463 #include
464 #include
465 #include
466 467 using namespace std;468 void bigger(vector
&v, vector
::size_type sz);469 470 int main()471 {472 vector
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
&v, vector
::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
497 #include
498 #include
499 #include
500 #include
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
words = { "23515","555","dfd","12","hello","fddfdfdf","ewreret","23515","555","dfd","12" };512 vector
::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
520 #include
521 #include
522 #include
523 #include
524 #include
525 526 using namespace std;527 vector
elim(istream& is);528 void print(vector
& v);529 bool isShorter(const string& s1, const string& s2);530 531 int main()532 {533 vector
vec=elim(cin);534 stable_sort(vec.begin(), vec.end(), isShorter);535 print(vec);536 return 0;537 }538 539 vector
elim(istream& is)540 {541 vector
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
& 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
577 #include
578 #include
579 #include
580 #include
581 #include
582 #include
583 584 using namespace std;585 shared_ptr
> elim(istream& is);586 void print(vector
& 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
> elim(istream& is)598 {599 shared_ptr
> v(new vector
);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
& 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 }

 

转载于:https://www.cnblogs.com/why2CS/p/6853399.html

你可能感兴趣的文章
【转】系统无法进入睡眠模式解决办法
查看>>
思维导图 第六章 项目进度管理
查看>>
[Tex学习笔记]尝试数学公式
查看>>
省市县,循环组装,整合大数组
查看>>
C语言中返回字符串函数的四种实现方法
查看>>
Jmeter学习及使用(一)安装
查看>>
H5 调用手机摄像机、相册功能
查看>>
Google Closure Compiler 高级模式及更多思考(转)
查看>>
python--闭包函数、装饰器
查看>>
【坑】linux目录软连接的相关操作--很容易误操作
查看>>
Phpstorm中使用SFTP
查看>>
stm32中字节对齐问题(__align(n),__packed用法)
查看>>
like tp
查看>>
分布式系统事务一致性解决方案
查看>>
开启一个项目如何上传到git
查看>>
ie文本框内容不居中问题
查看>>
利用grub2制作多启动U盘
查看>>
MQTT的学习研究(十三) IBM MQTTV3 简单发布订阅实例
查看>>
使用 github Pages 服务建立个人独立博客全过程
查看>>
posix多线程有感--线程高级编程(线程属性函数总结)(代码)
查看>>