我不明白下面定义的模板,有人可以帮我解码吗?
template <typename Impl>
template <typename datadec, typename T>
inline datadec<Impl>::codec(const T& value)
{
return codec<datadec>(Master::char_data(value), Master::size(value));
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个编程项目列表,而这个项目是制作15个拼图(幻灯片拼图)的。当我遇到一个小障碍时,我正在从事该项目。
我的代码编译得很好,但是当我运行它时,在第12行出现了分段错误: pos[0] = x;
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
class Tile{
private:
vector<int> pos;
int value;
public:
Tile(int x, int y, int value_){
pos[0] = x;
pos[1] = y;
value = value_;
}
~Tile(){}
int getPos(int a){return pos[a];}
void setPos(int a, int b){pos[a] = b;}
};
int main(){
Tile tile1(1, 2, 10);
Tile* t1;
t1 = &tile1;
// returns position "x"
cout << t1->getPos(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的意思是,我可以不必使用向量/数组来处理整个项目就可以完成整个项目,但是我仍然想知道,就我自己的理解而言,这为什么行不通。
基于我运行的调试,程序在初始化pos []向量的值时遇到了麻烦。
另一个问题:可能与此有关,我尝试在实例化矢量时设置其大小。
vector<int> pos(2); …
我有一个需要整理的大型数据集(35.8 GB,超过 10 亿行)。
这是一些可重现的代码来显示其格式。
id <- c(1, 1, 2, 2)
item <- c("a1", "a2", "a1", "a2")
itemformat <- c("AA", "AB", "BB", "AB")
full <- cbind(id, item, itemformat)
full <- as.data.table(full)
full
Run Code Online (Sandbox Code Playgroud)
此代码适用于此示例:
full2 <- full %>%
pivot_wider(names_from = item, values_from = itemformat)
full2
Run Code Online (Sandbox Code Playgroud)
然而,当在海量数据集上使用此方法时,R 要么说大小为 3.8 GB 的向量太大(我重新启动 R 并使用 gc() 函数),要么直接崩溃(工作超过 30 分钟后)。
我注意到data.table包中的fread()和fwrite()比我使用过的其他任何东西(例如read.csv()和read_csv() )要快得多。
那么,有没有一种方法(也许是根据data.table包?)将这些数据重新排列为大约 800,000 列?
另外,800,000 列是否超出某种限制?
整理完毕后,文件大小应该会减小(希望如此)。
旁注:我有 64 GB 的 RAM。
谢谢你!
我有以下嵌套循环:
for (x in xs) {
for (y in ys) {
# Do something with x and y
}
}
Run Code Online (Sandbox Code Playgroud)
我想拉平,所以我想建立两个向量的笛卡尔乘积的xs和ys并遍历结果.在Python中,这将是微不足道的:
for xy in product(xs, ys):
# x, y = xy[0], xy[1]
Run Code Online (Sandbox Code Playgroud)
但在R中,我发现的最简单的等价物看起来令人生畏:
xys <- expand.grid(xs, ys)
for (i in 1 : nrow(xys)) {
xy <- as.vector(xys[i, ])
# x <- xy[1], y <- xy[2]
}
Run Code Online (Sandbox Code Playgroud)
当然必须有更好的方法,不是吗?(为了澄清,我不想迭代索引 ......我认为必须有一种方法可以直接迭代产品中的元组.)
假设我有这个可变参数的基类模板:
template <typename ... Types>
class Base
{
public:
// The member foo() can only be called when its template
// parameter is contained within the Types ... pack.
template <typename T>
typename std::enable_if<Contains<T, Types ...>::value>::type
foo() {
std::cout << "Base::foo()\n";
}
};
Run Code Online (Sandbox Code Playgroud)
foo()只有在其template-parameter至少匹配其中一个参数Base(Contains此帖子底部列出的实现)时,才能调用该成员:
Base<int, char>().foo<int>(); // fine
Base<int, char>().foo<void>(); // error
Run Code Online (Sandbox Code Playgroud)
现在我使用非重叠类型集定义一个从Base继承两次的派生类:
struct Derived: public Base<int, char>,
public Base<double, void>
{};
Run Code Online (Sandbox Code Playgroud)
我希望在打电话的时候
Derived().foo<int>();
Run Code Online (Sandbox Code Playgroud)
编译器会找出要使用的基类,因为它是不包含的基类的SFINAE int.然而,GCC 4.9和Clang 3.5都抱怨模糊的电话.
我的问题是双重的:
我有一个调用mkstemp()的程序,写了一些返回fd的东西,然后关闭fd.我希望文件保留,直到我自己删除它!使用rm命令等等.我的问题是:Linux会在关闭后删除此文件(fd)吗?
以下代码在g ++(各种版本)上编译良好,但在我的系统上使用libc ++的clang ++ - 3.4失败:
#include <map>
#include <string>
std::map<std::string, std::string> f() {
return {};
}
int main() {
auto m = f();
}
Run Code Online (Sandbox Code Playgroud)
clang标志着以下问题:
x.cpp:6:12: error: chosen constructor is explicit in copy-initialization
return {};
^~
/usr/local/Cellar/llvm34/3.4.2/lib/llvm-3.4/bin/../include/c++/v1/map:838:14: note: constructor declared here
explicit map(const key_compare& __comp = key_compare())
^
Run Code Online (Sandbox Code Playgroud)
实际上,include文件将构造函数声明为explicit.但它在我的C++ 11草案标准中没有标记.这是clang ++/libc ++中的错误吗?我无法找到相关的错误报告.
我正在学习 C++ 中的类和 OOP 的一些基本知识。据我了解,初始化变量的首选现代方法是使用统一初始化。
在下面的简单类头示例中,使用统一初始化来初始化 3 个数据成员(长度、宽度和高度)。
为了保持一致性,我认为在构造函数声明中设置默认值时使用统一初始化可能是个好主意,但这不起作用,编译器(Debian Stretch 上的 gcc 6.3)会生成错误。从我所看到的来看,编译器认为大括号 {} 是构造函数定义主体的开始(显然不是,因为尚未添加右括号“)”)。
我承认这行不通,但出于好奇,有什么原因吗?我希望与我的代码保持一致,并尽可能使用统一的初始化。
谢谢。
#ifndef BOX_H
#define BOX_H
class Box
{
private:
double length {1.0};
double width {1.0};
double height {1.0};
public:
//constructor
Box(double l = 1.0, double w = 1.0, double h = 1.0); //ok
//Box(double l {1.0}, double w {1.0}, double h {1.0}); //Error
double volume();
};
#endif
Run Code Online (Sandbox Code Playgroud)
编辑...感谢到目前为止的评论,但我不确定我是否理解为什么不能对默认参数使用统一初始化的原因。有人可以指点我一些标准的 C++ 文档吗?
例如,采用下面的基本程序,可以使用统一初始化将n初始化为 5 ,但是不能像这样在函数头中将x初始化为默认参数(我使用带有 -std 的 gcc 6.3 …
有没有一种简单的方法来检索R中非对称矩阵的上(或下)三角矩阵?对于对称矩阵,这可以使用mat[upper.tri(mat)],但对于非对称矩阵怎么样?这里上三角矩阵的定义如下:如果具有超过1/2部分的单元属于由对角线界定的矩阵的右上角,则该单元属于上三角矩阵(例如, ,图中的红色部分).
我的项目需要以下 vmArgs 才能运行:
"vmArgs": "-javaagent:lib/aspectjweaver-1.9.5.jar -javaagent:lib/spring-instrument-5.2.3.RELEASE.jar --module-path lib/javafx-sdk-13.0.2/lib --add-modules=javafx.controls"
Run Code Online (Sandbox Code Playgroud)
把这个放在launch.json配置对象中,可以让项目完美运行。对于单元测试,应该将相同的 vmArgs 放入 settings.json 的 java.test.config 对象中,如下所示:
{
"java.test.config":
[
{
"name": "testConfig",
"workingDirectory": "${workspaceFolder}",
"vmArgs": "-javaagent:lib/aspectjweaver-1.9.5.jar -javaagent:lib/spring-instrument-5.2.3.RELEASE.jar --module-path lib/javafx-sdk-13.0.2/lib --add-modules=javafx.controls"
}
],
"java.test.defaultConfig": "testConfig"
}
Run Code Online (Sandbox Code Playgroud)
这部分不起作用。我的 JUnit 测试没有被正确检测。我究竟做错了什么?vmArgs 行是我在launch.json 中的精确副本。