小编dus*_*sha的帖子

STL find比手工制作的循环更好

我有一些问题.给出以下C++代码片段:

#include <boost/progress.hpp>

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

struct incrementor
{
  incrementor() : curr_() {}

  unsigned int operator()()
  { return curr_++; }

private:
  unsigned int curr_;
};

template<class Vec>
char const* value_found(Vec const& v, typename Vec::const_iterator i)
{
  return i==v.end() ? "no" : "yes";
}


template<class Vec>
typename Vec::const_iterator find1(Vec const& v, typename Vec::value_type val)
{
  return find(v.begin(), v.end(), val);
}


template<class Vec>
typename Vec::const_iterator find2(Vec const& v, typename Vec::value_type val)
{
  for(typename Vec::const_iterator i=v.begin(), end=v.end(); i<end; …
Run Code Online (Sandbox Code Playgroud)

c++ performance assembly stl find

14
推荐指数
2
解决办法
4148
查看次数

了解Clojure并发示例

我只是阅读有关Clojure并发性的各种文档,并在网站(http://clojure.org/concurrent_programming)上提供了相应的示例.

(import '(java.util.concurrent Executors))
(defn test-stm [nitems nthreads niters]
(let [refs  (map ref (replicate nitems 0))
      pool  (Executors/newFixedThreadPool nthreads)
      tasks (map (fn [t]
                   (fn []
                     (dotimes [n niters]
                       (dosync
                         (doseq [r refs]
                           (alter r + 1 t))))))
                (range nthreads))]
(doseq [future (.invokeAll pool tasks)]
  (.get future))
(.shutdown pool)
(map deref refs)))
Run Code Online (Sandbox Code Playgroud)

我理解它的作用以及它是如何工作的,但我不明白为什么需要第二个匿名函数fn []?

非常感谢,

dusha.

PS没有这第二个fn []我得到NullPointerException.

concurrency clojure

6
推荐指数
1
解决办法
944
查看次数

标签 统计

assembly ×1

c++ ×1

clojure ×1

concurrency ×1

find ×1

performance ×1

stl ×1