C++primer 9.3.4節練習

來源:互聯網
上載者:User

標籤:log   primer   void   練習   names   std   div   UI   out   

練習9.27

 1 #include <iostream> 2 #include <vector> 3 #include <list> 4 #include <deque> 5 #include <string> 6 #include <iterator> 7 #include <forward_list> 8  9 using namespace std;10 11 12 int main()13 {14     forward_list<int> flist{ 1,2,3,4,5,6,7 };15     auto prev = flist.before_begin();16     auto curr = flist.begin();17     while (curr != flist.end())18     {19         if (*curr % 2)20         {21             curr = flist.erase_after(prev);22         }23         else24         {25             prev = curr;26             ++curr;27         }28     }29     for (auto c : flist)30         cout << c << endl;31     system("pause");32     return 0;33 }

練習9.28

 1 #include <iostream> 2 #include <vector> 3 #include <list> 4 #include <deque> 5 #include <string> 6 #include <iterator> 7 #include <forward_list> 8  9 using namespace std;10 11 void checkAndInsert(forward_list<string> &str, string str1, string str2);12 13 int main()14 {15     forward_list<string> s{ "qwer","asdf", "ghjk","zxcv" };16     string s1{ "tyui" };17     string s2{ "haha" };18     checkAndInsert(s, s1, s2);19     for (auto c : s)20         cout << c << endl;21     system("pause");22     return 0;23 }24 25 void checkAndInsert(forward_list<string> &str, string str1, string str2)26 {27     auto prev = str.before_begin();28     auto curr = str.begin();29     auto last = str.end();30     int count = 0;31     while (curr != last)32     {33         if (*curr == str1)34         {35             prev = curr;36             curr = str.insert_after(curr, str2);37             ++count;38         }39         else40         {41             prev = curr;42             ++curr;43         }44         if (!count)45         {46             if (curr == last)47                 str.insert_after(prev, str2);48         }49     }50 }

 

C++primer 9.3.4節練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.