⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 karlsson.txt

📁 用C++编的小程序
💻 TXT
字号:
Lambda Expressions & C++         
by Bjorn Karlsson  

Listing 1:

template <typename T, typename Stream> class print_to_stream_t {
  Stream& stream_;
public:
  print_to_stream_t(Stream& s):stream_(s) {}
  void operator()(const T& t) const {
    stream_ << t;
  }
};
template <typename T,typename Stream> 
print_to_stream_t<T,Stream>   print_to_stream(Stream& s) {
  return print_to_stream_t<T,Stream>(s);
}


Listing 2:

#include <boost/lambda/lambda.hpp>
int main() {
  using namespace boost::lambda;

  std::vector<std::string> vec;
  vec.push_back("This");
  vec.push_back(" is");
  vec.push_back(" extremely");
  vec.push_back(" cool.");

  std::for_each(vec.begin(),vec.end(),std::cout << _1);
}

Listing 3:

namespace sample {
  struct placeholder_1 {};
  placeholder_1 _1;
  template <typename T> struct simple_binder_lshift {
    T& t_;
  public:
    simple_binder_lshift(T& t):t_(t) {}
    template <typename U> T& operator()(U u) {
      t_ << u;
      return t_;
    }
  };
}
template <typename T> sample::simple_binder_lshift<T> 
  operator<<(T& t,sample::placeholder_1 ignore) {
    return sample::simple_binder_lshift<T>(t);
}
int main() {
  using sample::_1;
  std::vector<std::string> vec;
  vec.push_back("Simple");
  vec.push_back(" example");
  std::for_each(vec.begin(),vec.end(),std::cout << _1);
}

Listing 4:

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
int main() {
  using namespace boost::lambda;
  std::vector<int> vec;
  for(int i=1;i<50;++i) {
    vec.push_back(i);
  }
  std::cout << "Print the odd numbers\n";
  std::for_each( 
    vec.begin(),
    vec.end(),
    if_then(_1%2,std::cout << constant(" ") << _1));
  std::cout << 
    "\n\nIf the number is even, double it, otherwise print it\n";
  std::for_each(
    vec.begin(),
    vec.end(),
    if_(_1%2==0)[_1*=2].else_[std::cout << constant(" ") <<  _1]);
  std::cout << "\nVerify the above\n";
  constant_type<char>::type space(constant(' '));
  std::for_each(
    vec.begin(),
    vec.end(),
    if_(_1%2==0)[std::cout << space << _1]);
}


Listing 5:

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

class missile_error : public std::exception {
public:
  virtual const char* what() {
    return "This missile has gone bad.";
  }
};
class missile {
  bool broken_;
public:
  void fire() {
    if (broken_) 
      throw missile_error();
    std::cout << "Gone like the wind\n";
  }
  void destroy() {
    broken_=true;
  } 
};
int main() {
  using namespace boost::lambda;
  std::vector<missile> vec;
  for (int i=0;i<10;++i)
    vec.push_back(missile());
  vec[4].destroy();
  // Here we go
  for_each(
    vec.begin(),
    vec.end(),
    try_catch(
      bind(&missile::fire,_1),catch_exception<missile_error>(
        std::cout << constant("Caught missile_error: ") 
          << bind(&std::exception::what, _e))));

}

Listing 6:

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

int main() {
  using namespace boost::lambda;
  std::vector<boost::any> stuff;
  stuff.push_back(std::string("This"));
  stuff.push_back(42);
  stuff.push_back(std::string("is")); 
  stuff.push_back(3.14159265);
  stuff.push_back(std::string("neat"));
  constant_type<char>::type noop(constant(' '));
  std::for_each(
    stuff.begin(),
    stuff.end(),
    try_catch(
    (
      std::cout << 
      bind<std::string>(
        &boost::any_cast<std::string>,_1) << ' ', noop), 
    catch_exception<boost::bad_any_cast>(
      // Eat the exception
      noop   
      )
    ));
}





3


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -