- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
This answer将std::unique_ptr
显示为带有boost-graph的 bundle 属性的成员。
提供的示例可以试用
Live On Coliru 。
因为我只对设计方面感兴趣,而不对答案的算法部分感兴趣,所以我提供了一个简化示例 Live On Coliru 。
#include <iostream>
#include <fstream>
#include <boost/graph/adjacency_list.hpp>
struct custom_node{
custom_node(std::string name, int capacity) : name(name), capacity(capacity) {}
std::string name = "uninitialized";
int capacity = -1;
};
void usage(){}
using namespace boost;
struct VertexProperties {
int id;
std::unique_ptr<custom_node> node;
};
typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
typedef graph_traits<DirectedGraph>::vertex_descriptor custom_vertex;
typedef graph_traits<DirectedGraph>::edge_descriptor custom_edge;
int main() {
DirectedGraph g;
boost::add_vertex(g);
g[0].node = std::make_unique<custom_node>("inner", 2);
////boost::add_vertex(VertexProperties{0, std::make_unique<custom_node>("inner", 2)}, g); // compilation error
std::cout << boost::num_vertices(g);
std::cout << g[0].id << "\n";
std::cout << g[0].node->name << "\n";
std::cout << g[0].node->capacity << "\n";
}
可以通过
boost::add_vertex(g)
添加顶点,然后可以实例化
std::unique_ptr
。
add_vertex
中的列表初始化通过创建具有所有属性的顶点来实现相同目的,则有关
implicitly-deleted copy constructor of 'VertexProperties'
的编译错误。
VertexProperties
的副本构造函数已删除,因为
std::unique_ptr
的副本构造函数已删除。
最佳答案
Why is the copy constructor is necessary for list initialization in the first place? Is there something I do not understand about it, or is this a shortcoming of boost-graph?
Aside: in copy-initialization the assignment can be elided by the compiler, but operator= still needs to be accessible for it to be valid code.
add_vertex(g)
可以工作),因此通过始终分配给默认构造的属性来简化实现。
if(g[v].node_logic) {
g[v].node.reset(new custom_node(g[v].vertex_name, 0, standby, normal));
}
VertexProperties props {0,
std::make_unique<custom_node>("inner", 2)};
auto vd = boost::add_vertex(g);
g[vd] = std::move(props);
auto add_vertex = [&g](VertexProperties&& props) {
auto vd = boost::add_vertex(g);
g[vd] = std::move(props);
return vd;
};
add_vertex({0, std::make_unique<custom_node>("inner", 2)});
auto add_vertex = [&g](int id, std::string name, int capacity = -1) {
auto vd = boost::add_vertex(g);
g[vd] = { id, std::make_unique<custom_node>(name, capacity) };
return vd;
};
add_vertex(0, "inner", 2);
add_vertex(1, "outer", 3);
add_vertex(2, "other");
#include <boost/graph/adjacency_list.hpp>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <memory>
namespace MyLib { // for ADL demo
struct custom_node {
custom_node(std::string name, int capacity)
: name(std::move(name)), capacity(capacity) {}
std::string name = "uninitialized";
int capacity = -1;
friend std::ostream& operator<<(std::ostream& os, custom_node const& cn) {
return os << "{" << std::quoted(cn.name) << ", " << cn.capacity << "}";
}
};
struct VertexProperties {
int id{};
std::unique_ptr<custom_node> node;
friend std::ostream& operator<<(std::ostream& os, VertexProperties const& vp) {
os << vp.id;
if (vp.node)
os << ", " << *vp.node;
return os;
}
};
template <typename G>
auto add_vertex(G& g, int id, const std::string& name, int capacity = -1) {
auto vd = boost::add_vertex(g);
g[vd] = { id, std::make_unique<custom_node>(name, capacity) };
return vd;
}
}
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, MyLib::VertexProperties>;
using custom_vertex = Graph::vertex_descriptor;
using custom_edge = Graph::edge_descriptor;
int main() {
Graph g;
add_vertex(g, 10, "inner", 2);
add_vertex(g, 11, "outer", 3);
add_vertex(g, 12, "other");
for (auto vd : boost::make_iterator_range(vertices(g))) {
std::cout << g[vd] << "\n";
}
}
版画
10, {"inner", 2}
11, {"outer", 3}
12, {"other", -1}
有人提到设计吗?
struct VertexProperties {
int id{};
std::optional<custom_node> node;
};
甚至只是
struct VertexProperties {
int id{};
custom_node node;
};
所有权语义将是相同的,而没有成本:
Graph g;
add_vertex({10, custom_node{"inner", 2}}, g);
add_vertex({11, custom_node{"outer", 3}}, g);
add_vertex({12, custom_node{"other"}}, g);
那只是使用BGL的标准
boost::add_vertex
重载。没有
optional<>
,它甚至可以变得更加简单:
add_vertex({10, {"inner", 2}}, g);
add_vertex({11, {"outer", 3}}, g);
add_vertex({12, {"other"}}, g);
也是
Live On Coliru (不带std::optional:
Live)
#include <boost/graph/adjacency_list.hpp>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <memory>
#include <optional>
struct custom_node {
custom_node(std::string name, int capacity = -1)
: name(std::move(name)), capacity(capacity) {}
std::string name = "uninitialized";
int capacity = -1;
friend std::ostream& operator<<(std::ostream& os, custom_node const& cn) {
return os << "{" << std::quoted(cn.name) << ", " << cn.capacity << "}";
}
};
struct VertexProperties {
int id{};
std::optional<custom_node> node;
friend std::ostream& operator<<(std::ostream& os, VertexProperties const& vp) {
os << vp.id;
if (vp.node) os << ", " << *vp.node;
return os;
}
};
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, VertexProperties>;
using custom_vertex = Graph::vertex_descriptor;
using custom_edge = Graph::edge_descriptor;
int main() {
Graph g;
add_vertex({10, custom_node{"inner", 2}}, g);
add_vertex({11, custom_node{"outer", 3}}, g);
add_vertex({12, custom_node{"other"}}, g);
for (auto vd : boost::make_iterator_range(vertices(g))) {
std::cout << g[vd] << "\n";
}
}
版画
10, {"inner", 2}
11, {"outer", 3}
12, {"other", -1}
关于c++ - 使用std::unique_ptr boost 图形捆绑属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62532431/
我有一个要提交的 iOS 应用程序,我的应用程序在我的 iPhone 上运行。我将 apple id 帐户添加到 Xcode 并在 Xcode 的常规部分下输入我的 bundle id,然后单击“修复
我有一个SDK项目,它在gradle中引用了很多依赖项。我必须要求SDK用户在项目中使用SDK时添加这些依赖项。问题是,每当我添加一些新的依赖项或将当前的依赖项替换为新的依赖项时,我都必须要求用户进行
我使用 Microsoft.AspNet.Web.Optimization用于 css 和 js 捆绑和缩小的 nuget 包。 我在这个路径 ~/bundles/shared.css 中创建了一个包
我使用 laravel-mix(包括 webpack)来打包 JS 文件。使用 BundleAnalyzerPlugin,我发现我的输出文件包含多个 JQuery 库副本,这增加了输出文件的大小。 它
我正在使用 maven felix 插件来创建 OSGi 包,但是假设您有一个包“com.example”存在于project1和project2中。此外,project2 依赖于 project1。
当我尝试捆绑我的 Meteor 应用程序时,我得到: $ meteor bundle app.tgz Errors prevented bundling: Exception while bundli
因此查看 bundleconfig.cs 它应该允许基于设备类型进行捆绑。唯一的问题是因为它在 App_Start 中,所以不允许我访问 Request 对象。有什么想法可以实现基于设备的捆绑吗? 最
上下文 http://news.ycombinator.com/item?id=4125530 问题: 这是否最终意味着 Java 应用程序将能够发布到 Mac 商店? (因为 JRE 自动捆绑到应用
我正在尝试为一个 React/Redux 项目创建我自己的 Webpack 配置。配置看起来很好,但是包的大小很大(在开发模式下,我知道如何在生产模式下减少它) 我的 package.json 看起来
所以我一直收到这个 Bundle ID 错误,说它不可用而且我真的不知道如何修复它。这是错误: 提供的数据有误。请更正并重新提交。标识符为“com.team.AppName”的 App ID 不可用。
我正在浏览 SO 并找到了 some code这向我提出了一个问题。 struct node* BuildOneTwoThree() { struct node *list = malloc(3 *
我正在为 Delphi XE7 使用 intraweb XIV 捆绑版。当我在这个新的捆绑版本中测试一个 intraweb XII 应用程序时,SSL/TLS 不工作。捆绑版本不支持 SSL/TLS?
预期: 当我使用 webpack 构建时,我的所有 JS 文件都会被捆绑,除了 ./src/Portfolio 目录中的文件(根据我的 Webpack.config.js 设置)。 实际: Webpa
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 7 年前。 Improve
我有一个项目引用了许多开源库,有些是新的,有些不是很新。也就是说,它们都很稳定,我希望坚持使用我选择的版本,直到我有时间迁移到更新的版本(我昨天测试了 hsqldb 2.0,它包含许多 api 更改)
我正在创建一个 REST API,并且我一直在研究允许捆绑来自客户端的请求的想法。我所说的捆绑是指他们可以发送一个包含多个“真实”请求的请求,然后将它们一起交付给客户。通常是 javascript a
在我的 AngularJS 项目中,我有一个 HTML 模板,其中 innerText 位于新行中: Click here 我正在使用 webpack 作为我的捆绑器。我希望它 trim
我已经为我的应用程序创建了一个静态库。现在,我的应用程序使用我在应用程序中引用的 plists 和图像等来源。 如何捆绑这些图像并将它们与静态库一起交付,以及我需要在源加载代码中进行哪些更改才能从该
所以, 我是 webpack 的新手,我正在开发一个项目,在该项目中我们只加载一个文件 bundle.js,我知道我可以单独加载文件。 但我想要的是bundle.js中未缩小的文件。目前我正在获取缩小
如何使用用户区域设置登录路径?我试过了 check_path: /{_locale}/login_check 和 check_path: /(en|ru)/login_check 但什么也没有
我是一名优秀的程序员,十分优秀!