我有两个数据表,DT1和DT2:
set.seed(1)
DT1<-data.table(id1=rep(1:3,2),id2=sample(letters,6), v1=rnorm(6), key="id2")
DT1
## id1 id2 v1
## 1: 2 e 0.7383247
## 2: 1 g 1.5952808
## 3: 2 j 0.3295078
## 4: 3 n -0.8204684
## 5: 3 s 0.5757814
## 6: 1 u 0.4874291
DT2<-data.table(id2=c("n","u"), v1=0, key="id2")
DT2
## id2 v1
## 1: n 0
## 2: u 0
Run Code Online (Sandbox Code Playgroud)
我想基于与DT2的连接更新DT1,但仅针对DT1的子集.例如,对于DT1[id1==3],我希望第4行中的v1的值更新,如下面的结果:
DT1
## id1 id2 v1
## 1: 2 e 0.7383247
## 2: 1 g 1.5952808
## 3: 2 j 0.3295078
## 4: …Run Code Online (Sandbox Code Playgroud) 我的问题是在R语言中解析表达式.让我跳到一个例子中:
fun_text <- c("
0 -> var
f1 <- function()
{
0 -> sum_var
sum_var2 = 0
sum_var3 <- 0
}
(function()
{
0 -> sum_var
sum_var2 = 0
sum_var3 <- 0
})->f2
f3 = function(x)
{
0 -> sum_var
sum_var2 = 0
sum_var3 <- 0
}
")
fun_tree <- parse(text=fun_text)
fun_tree
fun_tree[[1]]
fun_tree[[2]]
fun_tree[[3]]
fun_tree[[4]]
Run Code Online (Sandbox Code Playgroud)
之后,我们获得了这些结果:
expression(0 -> var, f1 <- function()
{
0 -> sum_var
sum_var2 = 0
sum_var3 <- 0
}, (function()
{
0 -> sum_var
sum_var2 …Run Code Online (Sandbox Code Playgroud) 根据ISO C11 - 6.5.16.3,它说
- 赋值运算符将值存储在左操作数指定的对象中.赋值表达式在赋值后具有左操作数的值,但不是左值.赋值表达式的类型是左值操作数在左值转换后将具有的类型.在左右操作数的值计算之后,对更新左操作数的存储值的副作用进行排序.对操作数的评估是不确定的.
所以我想这意味着,例如,
int x = 10;
x = 5 + 10;
Run Code Online (Sandbox Code Playgroud)
x计算为10,右操作数计算为15.x.但是如果赋值的目的是存储右操作数的evalauted值(就像在step2中那样),为什么左操作数的评估是必要的?评估左操作数有什么意义?
我有一个类,需要一个非默认的复制构造函数和赋值运算符(它包含指针列表).有没有通用的方法来减少复制构造函数和赋值运算符之间的代码重复?
据我所知,当重载operator =时,返回值应该是非const引用.
A& A::operator=( const A& )
{
// check for self-assignment, do assignment
return *this;
}
Run Code Online (Sandbox Code Playgroud)
在以下情况下允许调用非const成员函数是非const的:
( a = b ).f();
Run Code Online (Sandbox Code Playgroud)
但为什么要返回参考?如果返回值未被声明为引用,它会在什么情况下产生问题,让我们说按值返回?
假设正确实现了复制构造函数.
我试图使用if条件为xquery中的变量赋值.我不知道该怎么做.
这是我试过的:
declare namespace libx='http://libx.org/xml/libx2';
declare namespace atom='http://www.w3.org/2005/Atom';
declare variable $entry_type as xs:string external;
let $libx_node :=
if ($entry_type = 'package' or 'libapp') then
{element {fn:concat("libx:", $entry_type)} {()} }
else if ($entry_type = 'module') then
'<libx:module>
<libx:body>{$module_body}</libx:body>
</libx:module>'
Run Code Online (Sandbox Code Playgroud)
此代码抛出[XPST0003]不完整的'if'表达式错误.有人可以帮我解决这个问题吗?
此外,有人可以建议一些很好的教程来学习xqueries.
谢谢,索尼
定义赋值运算符时,它总是如下所示:
class X {...};
X& X::operator=(...whatever...);
Run Code Online (Sandbox Code Playgroud)
也就是说,它具有返回类型"对X的引用".这里,参数(...whatever...)可以是X&,const X&只X在使用复制和交换习语时,或任何其他类型.
似乎很奇怪,无论参数如何,每个人都建议返回非const引用X.这显然允许表达式(a = b).clear(),这应该是好的.
我有不同的意见,我希望禁止般的表情(x=y).clear,(x=y)=z甚至x=y=z在我的代码.我的想法是,这些表达式在一行代码上做的事情太复杂了.所以我决定让我的赋值运算符返回void:
void X::operator=(X) {...}
void X::operator=(int) {...}
Run Code Online (Sandbox Code Playgroud)
这有哪些负面影响?(除了看起来与平常不同)
我的班级X可以用于标准容器(例如std::vector<X>)吗?
我正在使用C++ 03(如果重要的话).
我已经扩展了std :: string来满足我不得不将自定义函数构建编写到名为CustomString的字符串类中的需求
我已经定义了构造函数:
class CustomString : public std::string {
public:
explicit CustomString(void);
explicit CustomString(const std::string& str);
explicit CustomString(const CustomString& customString);
//assignment operator
CustomString& operator=(const CustomString& customString);
... };
Run Code Online (Sandbox Code Playgroud)
在第三个构造函数(复制构造函数)和赋值运算符中,其定义为:
CustomString::CustomString(const CustomString& customString):
std::string(static_cast<std::string>(customString))
{}
CustomString& CustomString::operator=(const CustomString& customString){
this->assign(static_cast<std::string>(customString));
return *this;
}
Run Code Online (Sandbox Code Playgroud)
首先,因为这是"明确的"; 意味着需要显式转换以分配给另一个CustomString对象; 它抱怨任务.
CustomString s = CustomString("test");
Run Code Online (Sandbox Code Playgroud)
我不确定在哪里确实需要铸造.
如果复制构造函数不是显式的,那么代码可以正常工作,但我想知道并实现显式定义而不是"猜测正确的强制转换".
我的理解是C++ 隐式生成的赋值运算符执行成员方式的复制(这似乎也得到了这个答案的证实).但是,如果在成员副本期间抛出异常(例如,因为无法分配该成员的资源),被复制的对象是否会陷入无效状态?
换句话说,隐式生成的赋值运算符是仅实现基本保证,而不是强保证?
如果我们想要为我们的类副本提供强有力的保证,我们是否必须使用copy-and-swap惯用法手动实现赋值运算符?
此代码无法使用gcc 4.8.2(-std = c ++ 11)进行编译,但使用clang 3.4(trunk)编译(-std = c ++ 11):
#include <type_traits>
#include <vector>
struct X {
X& operator=(X&&) noexcept = default;
// adding noexcept this leads to an error in gcc, but works in clang:
// function ‘X& X::operator=(X&&)’ defaulted on its first
// declaration with an exception-specification that differs from the
// implicit declaration ‘X& X::operator=(X&&)’
std::vector<char> m;
};
// this assert holds, even without noexcept
static_assert(std::is_nothrow_move_assignable<X>::value,
"type-specification violation");
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这static_assert …
c++ ×6
r ×2
c ×1
c++11 ×1
data.table ×1
dry ×1
evaluation ×1
exception ×1
explicit ×1
if-statement ×1
join ×1
parsing ×1
stdstring ×1
subset ×1
xquery ×1