• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Impossible is nothing  
              愛過知情重醉過知酒濃   花開花謝終是空   緣份不停留像春風來又走   女人如花花似夢
            公告
            日歷
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567
            統計
            • 隨筆 - 8
            • 文章 - 91
            • 評論 - 16
            • 引用 - 0

            導航

            常用鏈接

            留言簿(4)

            隨筆分類(4)

            隨筆檔案(8)

            文章分類(77)

            文章檔案(91)

            相冊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             

            如果你想要把一個容器內的所有元素累加起來,應該怎么辦?

            如果你想要把一個容器內的所有元素累加起來,應該怎么辦?

            STL 的 accumulate 可以讓我們不必自己寫循環:

            #include <iostream>
            #include <functional>
            #include <numeric>
            #include <vector>
            #include <string>

            int main()
            {
            ? std::vector<int> vect;
            ? vect.push_back(1);
            ? vect.push_back(2);
            ? vect.push_back(3);
            ? vect.push_back(4);
            ?
            ? std::cout << "Accumulate: " <<
            ??? std::accumulate( vect.begin(), vect.end(), 0, std::plus<int>());
            }

            輸出:

            Accumulate: 10

            其中的 std::plus<int>() 可以省略,因為這將是3個參數的 accumulate 的默認行為。 注意 accumulate 算法是定義在 numeric 里面而不是 algorithm 里面的。

            由于 accumulate 和 plus 都是泛型的,所以如果你要累加的不是 int 而是字符串,對程序的修改也并不大:

            #include <iostream>
            #include <functional>
            #include <numeric>
            #include <vector>
            #include <string>

            int main()
            {
            ? std::vector<std::string> vect;
            ? vect.push_back("1");
            ? vect.push_back("2");
            ? vect.push_back("3");
            ? vect.push_back("4");
            ?
            ? std::cout << "Accumulate: " <<
            ??? std::accumulate( vect.begin(), vect.end(), std::string(""));
            }

            輸出:
            Accumulate: 1234

            不過,如果使用 boost.lambda ,這個問題會有一些很好看又容易理解的解法:

            #include <iostream>
            #include <algorithm>
            #include <numeric>
            #include <vector>
            #include <string>

            #include <boost/lambda/lambda.hpp>
            #include <boost/lambda/bind.hpp>
            //#include <boost/bind.hpp>

            using namespace boost::lambda;
            //using namespace boost;

            int main()
            {
            ? std::vector<std::string> vect;
            ? vect.push_back("1");
            ? vect.push_back("2");
            ? vect.push_back("3");
            ? vect.push_back("4");
            ?
            ? std::string result;
            ?
            ? std::for_each( vect.begin(), vect.end(), result += _1);
            ?
            ? std::cout << result;
            }

            輸出:
            1234

            這里要借用變量 result ,在這個程序中顯得多了幾行,但是我們調用 accumulate 的目的也往往是把結果放到一個變量中,這樣的話,使用 boost.lambda 反而會漂亮一些。

            在上面的程序中,另一個丑陋的地方就是 vector 的初始化,為了把 1, 2, 3, 4 放進 vect 里面,我們居然要調用 push_back 4次!不過,使用 boost.lambda 就好得多了。

            ? std::vector<int> vect(10);
            ? int i = 0;
            ? std::for_each( vect.begin(), vect.end(), _1 = ++var(i) );

            這里有兩個地方值得注意:
            1. 現在必須在 vect 的聲明中指出其大小,否則 for_each 對一個空容器可是什么也不會做
            2. 必須使用 ++var(i) ,而不是 ++i 。var 在這里的作用是強迫 lazy evaluation ,也就是讓變量在被用到的時候在求值,如果用 ++i ,你會得到一個裝有10個1的 vect ,而不是裝有1-10。

            =================================================================================

            許多問題遇到 map 都會變得復雜起來,如果想要把一個 map 中所有的 key 或者 value 累加起來,該怎么辦呢?這個時候已經不能直接使用 accumulate 了,用 boost.bind 可以辦到,做法是這樣的:

            #include <iostream>
            #include <algorithm>
            #include <numeric>
            #include <map>
            #include <string>

            #include <boost/bind.hpp>

            using namespace boost;

            int main()
            {
            ? std::map<int, std::string> persons;
            ? persons[123] = "Amy";
            ? persons[234] = "Ralph";
            ? persons[345] = "Simon";
            ? persons[456] = "Maggie";
            ?
            ? std::cout << std::accumulate( persons.begin(), persons.end(), 0,
            ??? bind(std::plus<int>(), _1, bind(&std::map<int, std::string>::value_type::first, _2)) )
            ??? << std::endl;

            ? std::cout << std::accumulate( persons.begin(), persons.end(), std::string(),
            ??? bind(std::plus<std::string>(), _1, bind(&std::map<int, std::string>::value_type::second, _2)) )
            ??? << std::endl;
            }

            輸出:

            1158
            AmyRalphSimonMaggie

            辦是辦到了,但是平心而論,的確算不上是漂亮。連續的 bind 并不比自己寫的循環更讓人頭暈。boost.lambda 也要用到 bind ,然而可以清晰許多:

            #include <iostream>
            #include <algorithm>
            #include <numeric>
            #include <map>
            #include <string>

            #include <boost/lambda/lambda.hpp>
            #include <boost/lambda/bind.hpp>

            using namespace boost::lambda;

            int main()
            {
            ? std::map<int, std::string> persons;
            ? persons[123] = "Amy";
            ? persons[234] = "Ralph";
            ? persons[345] = "Simon";
            ? persons[456] = "Maggie";

            ? int iresult = 0;
            ? std::string sresult;
            ?
            ? std::for_each( persons.begin(), persons.end(),
            ??? iresult += bind(&std::map<int, std::string>::value_type::first, _1)
            ? );
            ?
            ? std::for_each( persons.begin(), persons.end(),
            ??? sresult += bind(&std::map<int, std::string>::value_type::second, _1)
            ? );
            ?
            ? std::cout << iresult << std::endl;
            ? std::cout << sresult << std::endl;
            }

            輸出和上面的一樣:

            1158
            AmyRalphSimonMaggie

            有了它的幫助,即便間接層次再增加一層,也不會有太多困難:假如你的 map 并不直接存儲 string ,而是存儲 Person 對象,而它們的名字要通過 Name() 方法來取得,代碼只需要稍微的修改:


            #include <iostream>
            #include <algorithm>
            #include <numeric>
            #include <map>
            #include <string>

            #include <boost/lambda/lambda.hpp>
            #include <boost/lambda/bind.hpp>

            using namespace boost::lambda;

            class Person
            {
            public:
            ? Person(){}
            ? Person(const std::string& name) : name_(name){}
            ?
            ? std::string& Name()
            ? { return name_; }
            ?
            private:
            ? std::string name_;
            };

            int main()
            {
            ? std::map<int, Person> persons;
            ? persons[123] = Person("Amy");
            ? persons[234] = Person("Ralph");
            ? persons[345] = Person("Simon");
            ? persons[456] = Person("Maggie");

            ? std::string result;
            ?
            ? std::for_each( persons.begin(), persons.end(),
            ??? result += bind(&Person::Name, bind(&std::map<int, Person>::value_type::second, _1))
            ? );
            ?
            ? std::cout << result;
            }

            輸出:

            AmyRalphSimonMaggie

            posted on 2006-06-28 13:44 笑笑生 閱讀(158) 評論(0)  編輯 收藏 引用
             
            Copyright © 笑笑生 Powered by: 博客園 模板提供:滬江博客
            亚洲精品成人网久久久久久| 久久精品成人欧美大片| 久久九九青青国产精品| 久久99国产精品99久久| 久久久久女教师免费一区| 久久亚洲AV无码精品色午夜| 精品久久久久久综合日本| 久久久这里有精品| 久久综合狠狠综合久久97色| 久久亚洲国产精品成人AV秋霞| 成人免费网站久久久| 久久综合色老色| 国产精品一区二区久久精品无码 | 日韩AV无码久久一区二区 | 久久水蜜桃亚洲av无码精品麻豆 | 久久伊人五月天论坛| 97久久久久人妻精品专区 | 2021国内精品久久久久久影院| 韩国三级大全久久网站| 色欲av伊人久久大香线蕉影院| 日本道色综合久久影院| 久久国产亚洲精品无码| 久久久久久久97| 亚洲国产成人精品久久久国产成人一区二区三区综 | 欧美日韩中文字幕久久久不卡| 久久精品国产亚洲AV高清热| 色综合久久夜色精品国产| 久久av高潮av无码av喷吹| 久久Av无码精品人妻系列| 国产成人精品三上悠亚久久| 欧美性猛交xxxx免费看久久久| 国产AV影片久久久久久| 2021久久国自产拍精品| 久久精品午夜一区二区福利| 久久夜色精品国产噜噜麻豆| 亚洲国产精品无码久久久秋霞2| 思思久久99热只有频精品66| 99久久做夜夜爱天天做精品| 久久久久久精品久久久久| 午夜天堂av天堂久久久| 国产午夜免费高清久久影院|