我不明白为什么在foobar下面我需要指定std::vector<int>{}而在foobar2我不需要:
#include <iostream>
#include <memory>
#include <vector>
#include <tuple>
std::tuple<std::unique_ptr<int>, std::vector<int>> foobar() {
std::unique_ptr<int> test = std::make_unique<int>(42);
return { std::move(test), {} }; // <= this is a syntax error
// return { std::move(test), std::vector<int>{} } // <= this compiles...
}
std::tuple<int, std::vector<int>> foobar2() {
return { {}, {} };
}
int main() {
std::cout << *std::get<0>(foobar()) << "\n";
std::cout << std::get<0>(foobar2()) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
来自 GCC 的错误消息是
<source>: In function 'std::tuple<std::unique_ptr<int, std::default_delete<int> …Run Code Online (Sandbox Code Playgroud) 为什么是下面这个
#include <iostream>
#include <string>
#include <range/v3/all.hpp>
std::vector<int> some_ints() {
return { 1,2,3,4,5 };
}
int main() {
auto num_strings = some_ints() |
ranges::views::transform([](int n) {return std::to_string(n); }) |
ranges::to_vector;
for (auto str : num_strings) {
std::cout << str << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
一个错误,同时
int main() {
auto ints = some_ints();
auto num_strings = ints |
ranges::views::transform([](int n) {return std::to_string(n); }) |
ranges::to_vector;
for (auto str : num_strings) {
std::cout << str << "\n";
}
return 0;
} …Run Code Online (Sandbox Code Playgroud) 我遇到了 Android Studio 和 Flutter 框架的问题。
当我使用 Flutter 开发移动应用程序时,Android Studio 显示的代码完成建议现在是不连贯的类型,但是,在 Visual Studio Code 中代码完成工作正常。(它曾经也适用于 Android Studio,但我可能在不知情的情况下破坏了某些东西。)
这是 Visual Studio Code ScreenShot
如您所见,Visual Studio Code 提出了对这个特定属性有意义的所有类型。但是 Android Studio 显示了完全不同的东西——我什至在那里找不到那些类型。
我尝试了以下方法:
我使用的是最新版本的 Android Studio、Flutter 和 Dart。任何帮助将不胜感激。
考虑以下:
struct foo {
};
struct bar {
};
int main()
{
foo f;
bar b;
std::variant<foo*, bool> v;
v = &b; // compiles in Visual Studio 19 v16.7.3
}
Run Code Online (Sandbox Code Playgroud)
正如评论中所讨论的,我相信以上是合法的 C++17。有一个提案P0608R3已被标准接受,以解决这种令人惊讶的行为,但它在 2018 年(在圣地亚哥会议上)被接受,因此适用于 C++20 而不是 C++17。此外,P0608R3 当前未在 Visual Studio 中实现,即使在编译到 C++20 预览版时也是如此。
从指向非 foo 的指针创建此变体的最佳/最不冗长的方法是编译时错误?我相信以下内容有效,但如果变体包含多个项目,则是很多样板。
struct foo {
};
struct bar {
};
using variant_type = std::variant<foo*, bool>;
struct var_wrapper : public variant_type
{
var_wrapper(foo* v = nullptr) : variant_type(v)
{}
var_wrapper(bool v) : variant_type(v)
{}
template<typename …Run Code Online (Sandbox Code Playgroud) 我喜欢你可以使用惰性范围的想法std::views::iota,但惊讶地发现这iota是目前标准中唯一类似的东西;views::single它是除了和之外唯一的“范围工厂” views::empty。例如,目前还没有相当于std::generate炼油厂的工厂。
然而我注意到,通过在 iota 上使用变换视图来实现语义是微不足道的generate,并且忽略 iota 传递给变换的值,即
#include <iostream>
#include <ranges>
#include <random>
template<typename F>
auto generate1(const F& func) {
return std::views::iota(0) | std::views::transform([&func](int) {return func(); });
}
std::random_device dev;
std::mt19937 rng(dev());
int main() {
auto d6 = []() {
static std::uniform_int_distribution<> dist(1, 6);
return dist(rng);
};
for (int v : generate1(d6) | std::views::take(10)) {
std::cout << v << ' ';
}
std::cout << '\n';
}
Run Code Online (Sandbox Code Playgroud)
我的问题是实施这样的事情的“真正方法”是什么?制作一个可通过管道传输的范围视图对象,而不仅仅是使用iota. …
我应该使用什么算法来查找有向下限但不是上限的有向图上的最小流量?比如这个简单的例子:

在文献中,这是最小的成本流问题.然而,在我的情况下,成本与每个边缘所需流量的非零下限相同,所以我在上面提出问题.在文献中,问题是:找到单源/单宿有向无环图的最小成本流的最佳算法是什么,其中每个边具有无限容量,流上的非零下界,以及成本等于流量的下限.
根据我的研究,似乎人们处理任何类型网络的任何类型的最低成本的主要方式是将问题设置为LP类型问题并以此方式解决.然而,我的直觉是没有流动的上限,即具有无限容量的边缘,使问题更容易,所以我想知道是否有一种算法专门针对这种情况使用比单纯形法等更多的"图形"技术. .人.
我的意思是,如果所有的成本和下限都是1,如上所述...我们正在寻找一个涵盖所有边缘的流程,遵守流程规则,并且不会在从s到t的任何路径上推动太多流量.这只是感觉它不应该要求LP求解器,事实上维基百科关于最小成本流的文章指出"如果容量约束被移除,问题就会减少到最短路径问题"但我认为他们正在讨论下限全部为零的情况.
还有开源C/C++代码,可以在任何地方实现最低成本流量吗?从谷歌搜索可用的东西,我发现我可以自己设置问题作为LP问题,并用开源LP解算器解决,或者我可以使用LEMON,它为最低成本流提供了几种算法.据我所知,boost图库不包含实现.
还有别的事吗?
假设您有一个foo包装某种可调用对象集合的类.foo有一个成员函数run(),它迭代集合并调用每个函数对象.foo还有一个成员remove(...)将从集合中删除可调用对象.
是否有一个习惯,RAII风格的后卫,你可以把foo.run()和foo.remove(...)使得被调用驱动消除了foo.run()
将被推迟到后卫的析构函数火灾?可以用标准库中的东西来完成吗?这个模式有名字吗?
我目前的代码似乎不够优雅,所以我正在寻找最佳实践类型的解决方案.
注意:这不是关于并发性的.非线程安全的解决方案很好.问题在于重新引入和自我引用.
这是一个问题的例子,没有优雅的"延迟删除"警卫.
class ActionPlayer
{
private:
std::vector<std::pair<int, std::function<void()>>> actions_;
public:
void addAction(int id, const std::function<void()>& action)
{
actions_.push_back({ id, action });
}
void removeAction(int id)
{
actions_.erase(
std::remove_if(
actions_.begin(),
actions_.end(),
[id](auto& p) { return p.first == id; }
),
actions_.end()
);
}
void run()
{
for (auto& item : actions_) {
item.second();
}
}
};
Run Code Online (Sandbox Code Playgroud)
其他地方:
...
ActionPlayer player;
player.addAction(1, []() …Run Code Online (Sandbox Code Playgroud) 这是我几周前提出的一个问题的后续,其中的答案是,在模板中使用仅在模板实例化时完成但在模板实例化时不完整的类型是格式不正确的,不需要诊断。其定义的时间。
我的后续问题是,在不完整类型本身依赖于模板参数的情况下,这仍然成立吗?因为似乎事实并非如此。以下内容在 Godbolt 上的所有编译器中进行编译,即使只foo::do_stuff()使用了foo_wrapper::value()类模板foo_wrapper最终存在的前向声明。
#include <iostream>
template<typename T>
class foo_wrapper;
template<typename T>
class foo {
foo_wrapper<T>& parent_;
public:
foo(foo_wrapper<T>& wrapped) : parent_(wrapped)
{}
void do_stuff() {
std::cout << "do stuff " << parent_.value() << "\n";
}
};
template<typename T>
class foo_wrapper {
foo<T> foo_;
T value_;
public:
foo_wrapper(T n) :
foo_(*this),
value_(n)
{}
void do_stuff() {
foo_.do_stuff();
}
T value() const {
return value_;
}
};
int main()
{
foo_wrapper<int> fw(42);
fw.do_stuff();
}
Run Code Online (Sandbox Code Playgroud) 给定以下两个函数,为什么第一个n的时间复杂度为什么第二个为2 ^ n?
唯一的区别是第二个函数返回之前的+1。我不知道这如何影响时间复杂度。
int f1(int n){
if (n==1)
return 1;
return f1(f1(n-1));
}
int f2(int n){
if (n==1)
return 1;
return 1+f2(f2(n-1));
}
Run Code Online (Sandbox Code Playgroud) 如果实例化模板时类型是完整的,那么在模板中使用不完整类型是否合法?
如下
#include <iostream>
struct bar;
template <typename T>
struct foo {
foo(bar* b) : b(b) {
}
void frobnicate() {
b->frobnicate();
}
T val_;
bar* b;
};
struct bar {
void frobnicate() {
std::cout << "foo\n";
}
};
int main() {
bar b;
foo<int> f(&b);
f.frobnicate();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 编译上述内容没有任何抱怨。GCC 发出警告invalid use of incomplete type 'struct bar'但编译。Clang 错误与member access into incomplete type 'bar'.
c++ ×8
templates ×2
android ×1
c++20 ×1
dart ×1
flutter ×1
network-flow ×1
raii ×1
range-v3 ×1
std ×1
std-ranges ×1
std-variant ×1
stdtuple ×1
time ×1