小编Arn*_*ler的帖子

Boost Graph DFS(和其他算法)的 API 背后的动机是什么?

一般问题

当我迈出进入 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算法不能负责处理这些细节呢?

我能想到什么

  • 是因为 if tree 不是一种类型吗?如果树和图之间没有类型区别,那么 DFS 就不可能知道不存在循环,因此它需要对所有情况下探索过的顶点进行着色 - 即使您绝对确定它是一个树,有根,无环。
  • 如果是这样,那么它并不能完全向我解释为什么 DFS 无法在我们不知情的情况下创建/访问/销毁此颜色图。

c++ boost boost-graph

6
推荐指数
1
解决办法
253
查看次数

我应该何时以及如何使用 std::predicate

我试图限制 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)

c++ predicate c++20

4
推荐指数
1
解决办法
1133
查看次数

C++ 概念:要求策略类中存在静态变量

我想约束策略类的模板参数。\n也就是说,当我调用 时Foo<policy>,如果策略类不满足我想要的要求,我希望编译器在此处停止。

\n

完整的非工作示例

\n

为了简化问题,我们只考虑策略类必须声明一个静态变量的要求,该静态变量本身满足另一个概念(这里是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++ units-of-measurement c++-concepts

2
推荐指数
1
解决办法
1290
查看次数

在 C# 中实现使用并返回相同接口的接口

我在使用 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# 很陌生,很容易被这门语言搞糊涂!

我发现了类似的问题(例如为什么我不能重写我的接口方法?),但它们没有解决我的问题。

c# interface

2
推荐指数
1
解决办法
105
查看次数

将 std::string 转换为具有负值的 double 会导致零或引发异常

语境

从输入文件中读取一个数字作为 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)

我显然错过了什么?

c++

1
推荐指数
1
解决办法
131
查看次数