当我迈出进入 BGL 的第一步时,我很难理解为什么最小惊讶原则在这里有点被欺负。
因此,经过大量的努力,我可以构建一个树图,并且我期望能够写出类似的东西
tree.dfs(visitor);
Run Code Online (Sandbox Code Playgroud)
现在我知道 BGL 并不是一个面向对象的 API,我想写类似的东西是有意义的
depth_first_search(tree, visitor)
Run Code Online (Sandbox Code Playgroud)
然后,由于我了解到 BGL 中没有 Tree 类,因此设计选择是使根的存在成为值属性而不是类型属性。所以我想这解释了为什么我们必须传递根描述符,比如
depth_first_search(graph_that_is_a_tree, visitor, root);
Run Code Online (Sandbox Code Playgroud)
到目前为止,我感觉我(或多或少)遵循了设计。但为了让我的代码编译,我实际上必须编写:
auto [root, tree] = my_stuff::to_k_ary_tree<my_vertex, my_edge>(ast);
auto indexmap = boost::get(boost::vertex_index, tree);
auto colormap = boost::make_vector_property_map<boost::default_color_type>(indexmap);
MyVisitor<my_stuff::k_ary_tree<my_vertex,my_edge>> vis;
std::vector<boost::default_color_type> colors(num_vertices(tree));
boost::depth_first_search(tree, vis, colormap, root);
Run Code Online (Sandbox Code Playgroud)
这就是我失去它的地方(直觉):即使我被警告大多数算法都需要颜色图,我也不理解/欣赏为什么我需要这个签名。
为什么DFS算法不能负责处理这些细节呢?
我试图限制 Callable 在评估时返回布尔值。我一直在尝试使用这个概念std::predicate,但它似乎并没有达到我想要的效果。
所以我定义了自己的概念,它是可调用的并返回可转换为布尔值的东西。但同样,我很难理解我能用它做什么或不能做什么,我想知道它的实际用例是什么std::predicate?
#include<concepts>
#include<string>
template<class F, class... Args>
concept Predicate = std::invocable<F, Args...> &&
std::convertible_to<std::invoke_result_t<F, Args...>, bool>;
int main(int argc, char *argv[])
{
constexpr Predicate auto f1 = [](){return true;}; // ok
constexpr std::predicate auto f2 = [](){return true;}; // ok
constexpr int x = 34;
constexpr Predicate auto f3 = [x](){ return x==42;}; // ok
// Pas ok: error: deduced initializer does not satisfy placeholder constraints
//constexpr Predicate auto f4 = [](auto x){ …Run Code Online (Sandbox Code Playgroud) 我想约束策略类的模板参数。\n也就是说,当我调用 时Foo<policy>,如果策略类不满足我想要的要求,我希望编译器在此处停止。
为了简化问题,我们只考虑策略类必须声明一个静态变量的要求,该静态变量本身满足另一个概念(这里是mp-units 库中的 Acceleration 概念) 。
\n#include <units/isq/si/si.h>\n\nusing units::isq::Acceleration;\n\n// A policy\nstruct earth\n{\n // requirement seems to be fulfilled\n static inline constexpr Acceleration auto gravity = standard_gravity<>;\n};\n\n// Let\'s define a concept because I will need soon to use a set of more than 1 requirements\ntemplate <typename T>\nconcept SphericBody = requires(T)\n{\n { T::gravity } -> Acceleration;\n};\n\n// The host class that has a constraint of the template argument\ntemplate<SphericBody T>\nclass Foo\n{\n // ...\n}\n\nint main()\n{\n Foo<earth> …Run Code Online (Sandbox Code Playgroud) 我在使用 C# 实现接口时遇到问题:
public interface IFooable
{
public (IFooable left, IFooable right) FooWith(IFooable other);
}
Run Code Online (Sandbox Code Playgroud)
然后在我的具体课程中,我有
class Concrete
{
public (Concrete left, Concrete right) FooWith(Concrete other)
{
return GoFooSomewhereElse(...);
}
}
Run Code Online (Sandbox Code Playgroud)
但在我的测试中,编译器告诉我Concrete没有实现 FooWith 函数。
我真的不明白发生了什么,因为我对 C# 很陌生,很容易被这门语言搞糊涂!
我发现了类似的问题(例如为什么我不能重写我的接口方法?),但它们没有解决我的问题。
从输入文件中读取一个数字作为 astd::string并转换为double.
我根据我在网上找到的内容尝试了各种方法:
std::string str = "?12.5799";
std::cout << str << std::endl;
double d;
std::stringstream(str) >> d;
std::cout << d << std::endl;
std::cout << atof(str.c_str()) << std::endl;
std::cout << stod(str) << std::endl;
Run Code Online (Sandbox Code Playgroud)
当从文件中读取正数时,它似乎工作得很好,但是当std::string.
输出:
?12.5799
0
0
stod
Run Code Online (Sandbox Code Playgroud)
我显然错过了什么?