31 Ekim 2017 Salı

BOOST_FOREACH Macrosu

Giriş
Şu satırı dahil ederiz.
#include <boost/foreach.hpp>
Örnek
Şöyle yaparız
BOOST_FOREACH(int i, v)
{
  std::cout << i << std::endl;
}


30 Ekim 2017 Pazartesi

graph breadth_first_search metodu

Giriş
Şu satırı dahil ederiz
#include <boost/graph/breadth_first_search.hpp>
Örnek
Şöyle yaparız.
class BreadthFirstSearchVisitor : public boost::default_bfs_visitor
{
  ...
};

GraphType g = ...;
BreadthFirstSearchVisitor breadthFirstSearchVisitor(g);

unsigned int startVertex = 0;

// named argument signature
breadth_first_search(g, vertex(startVertex, g),
 visitor(breadthFirstSearchVisitor).color_map(get(boost::vertex_color_t(), g)));

Örnek
Şöyle yaparız
VertexDescr source = ...; 
VertexDescr target = ...;
DirectedGraph const  g = ...;

std::vector<double> distances(num_vertices(g));
std::vector<boost::default_color_type> colormap(num_vertices(g));

// Run BFS and record all distances from the source node
breadth_first_search(g, source,
  visitor(make_bfs_visitor(boost::record_distances(distances.data(),
    boost::on_tree_edge())))
    .color_map(colormap.data())
);

for (auto vd : boost::make_iterator_range(vertices(g)))
  if (colormap.at(vd) == boost::default_color_type{})
    distances.at(vd) = -1;



python exec metodu

Giriş
Açıklaması şöyle

Effects

Execute Python source code from code in the context specified by the dictionaries globals and locals.

Returns

An instance of object which holds the result of executing the code.
C++ içinde Python kodunu çalıştırır.

Örnek
Şöyle yaparız.
using namespace boost::python;

Py_Initialize();

object main_module = import("__main__");
object main_namespace = main_module.attr("__dict__");

try
{
  std::string comm = ...;

  object bar = exec(comm.c_str(), main_namespace);
  if (bar.is_none())
    std::cout << "None\n";
}
catch (error_already_set const &)
{
  PyErr_Print();
}
Örnek
Şöyle yaparız.
const char *prog = "def ack(m, n):\n"
                     "  if m == 0:\n"
                     "    return n + 1\n"
                     "  elif n == 0:\n"
                     "    return ack(m - 1, 1)\n"
                     "  else:\n"
                     "    return ack(m - 1, ack(m, n - 1))";

Py_Initialize();

boost::python::object mainModule = boost::python::import("__main__");
boost::python::object mainNamespace = mainModule.attr("__dict__");

boost::python::exec(prog, mainNamespace, mainNamespace);
int val = boost::python::extract<int>(
  boost::python::eval("ack(3,3)", mainNamespace, mainNamespace));