6 Mart 2017 Pazartesi

range

make_iterator_range metodu - vector
Şu satırı dahil ederiz.
#include <boost/range/iterator_range.hpp>
Şöyle yaparız.
const std::vector<int> v {1, 2, 3};

const auto range = boost::make_iterator_range(v);

std::copy(std::crbegin(range),
          std::crend(range),
          std::ostream_iterator<int> {std::cout, " "});
make_iterator_range metodu - directory_iterator + directory_iterator
Örnek 1 - for döngüsü
Şöyle yaparız.
for (fs::path p : boost::make_iterator_range(fs::directory_iterator("."), {})) {
  if (!fs::is_regular_file(p) || p.extension() != ".txt")
    continue;

  std::ifstream infile(p.string()); // Read file line by line
  while (std::getline(infile, line)) {
    if (boost::regex_search(line, result, pattern)) {
      std::cout << "\t" << result.str() << "\n";
    }
  }
}    
Örnek 2 - lambda
Şöyle yaparız.
path const p(".");

auto list = [=] { return boost::make_iterator_range(directory_iterator(p), {}); };

// Save entries of 'list' in the vector of strings 'names'.
std::vector<std::string> names;
for(auto& entry : list())
{
  names.push_back(entry.path().string());
}

Hiç yorum yok:

Yorum Gönder