Impossible is nothing  
  爱过知情重醉过知酒浓   花开花谢终是空   缘份不停留像春风来又走   女人如花花似梦
公告
日历
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
统计
  • 随笔 - 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 笑笑生 阅读(149) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © 笑笑生 Powered by: 博客园 模板提供:沪江博客