用于打印输出的头文件
- basic_alg.h
#ifndef ACAI_COMMON_H
#define ACAI_COMMON_H
#include <algorithm>
#include <iostream>
#include <iterator>
#include <type_traits>
#include <vector>
namespace base {
template <class T, template <typename...> class C_>
std::ostream &operator<<(std::ostream &cout, const C_<T> &value) {
cout << "\n{\n ";
std::copy(std::begin(value), std::end(value),
std::ostream_iterator<T>(cout, " "));
cout << std::endl;
cout << "}";
return cout;
}
void print_msg() { std::cout << std::endl; }
template <typename... ARGS> void print_msg(ARGS &&... args) {
print_msg(std::forward<ARGS &&>(args)...);
}
template <typename T, typename... Args>
void print_msg(T &&value, Args &&... args) {
std::cout << value;
print_msg(std::forward<Args &&>(args)...); //用于完美转发
}
} // namespace base
#endif // ACAI_COMMON_H
还有一个执行的类, 用于执行当前的算法:
- executor.hpp
#pragma once
namespace base
{
template<class Exe>
class executor
{
public:
template<typename ...ARGS>
static void exe(ARGS&&... args)
{
Exe()(std::forward<ARGS&&>(args)...);
}
};
}
下面看一个上面两个头文件的用法, executor.hpp 主要是用来执行Functor 类的方法。
- main.cpp
#include <iostream>
#include "executor.hpp"
#include "basic_alg.h"
struct Foo
{
void operator()(int& value)
{
++value;
}
};
int main(int argc, char** argv)
{
int value = 10;
base::print_msg("value: ", value);
base::executor<Foo>::exe(value);
base::print_msg("value + 1 = : ", value);
std::cout << std::endl;
}
由上面一个例子输出的结果是: