这是我以前见过的做法,但并不常见:在对值本身进行求值的同时将变量分配给一个值(或者是对表达式本身进行求值?)。例子:
// Outputs "The value is 1"
$value = 1;
if ($var = $value) {
echo "The value is $var";
}
Run Code Online (Sandbox Code Playgroud)
似乎与以下相同:
$value = 1;
$var = $value;
if ($var) {
echo "The value is $var";
}
Run Code Online (Sandbox Code Playgroud)
另一个例子:
// Outputs "The value is 1"
$value = 1;
echo "The value is ".$var = $value;
Run Code Online (Sandbox Code Playgroud)
我一直在使用它来缩短我的代码,主要是第一个示例:用于评估第一个变量或表达式,同时将其分配给同一表达式中的另一个变量或表达式。像这样的东西:
if ($status = User::save($data)) {
echo "User saved.";
}
// do something else with $status
Run Code Online (Sandbox Code Playgroud)
这看起来很基本,但我实际上找不到任何关于此的文档,也许我不知道在哪里查找。在看到它多年后,我最近才弄清楚它是如何工作的,我真的很喜欢使用它,但我不想随意使用它。
它使代码更短,也许对某些人来说不太清楚,但绝对减少了重复性。这种方法有什么注意事项吗?这是否完全安全,或者是否存在可能失败或导致意外行为的情况?这似乎不是一种很常见的做法,所以我希望在开始“疯狂”之前找到一个解释。如果有记录,我们将非常感谢指向正确页面的链接。
如果我有这个清单
'((x 3) (y 4) (z 2))
Run Code Online (Sandbox Code Playgroud)
我如何将 3 分配给 x,将 y 分配给 4,将 z 分配给 2 以使用它来进行数学运算?
3 + x
Run Code Online (Sandbox Code Playgroud)
或者
y + z
Run Code Online (Sandbox Code Playgroud)
谢谢
我在 Swift 中有一个字符串变量:
let foo = "bar"
Run Code Online (Sandbox Code Playgroud)
但它也可以是任何其他任意字符串。现在我想做以下事情:
let bar = ... // either the value of foo, or if foo is "", it will be "default"
Run Code Online (Sandbox Code Playgroud)
我查找了“合并运算符”,这似乎是正确的方向。我用下面的代码试了一下:
let bar = foo ?? "default"
Run Code Online (Sandbox Code Playgroud)
但是好像什么时候foo为空,还是会取这个,不会设置default为值。
我假设合并运算符仅适用于nil值而不适用于空字符串。但是,是否有解决方案可以测试我的值是否为空字符串并分配默认值?我不能使用 if/else,因为它将位于一个类中。
当我这样做时,它完美地打印出“2”。
int main()
{
int *p;
int x = 2;
*p = x;
cout << *p;
}
Run Code Online (Sandbox Code Playgroud)
但是当我第一次将 *p 初始化为 null 时,程序崩溃了。
int main()
{
int *p=0;
int x = 2;
*p = x;
cout << *p;
}
Run Code Online (Sandbox Code Playgroud)
我想问一下,第一个程序到底成功运行了什么,为什么可以给一个未初始化的指针赋值?
[编辑] 我的问题实际上与我得到的这个过去的考试问题有关。您可以勾选多个答案,看来 (b) 和 (c) 都是正确的。但现在我知道 (c) 是否有效纯粹是因为运气。
我的 C++ 程序中有对象作为用户数据传递给 Lua,并且我覆盖了此用户数据的元表,以便对对象索引的赋值(通过 __newindex 和 __index)导致对 C 的调用,从而转换赋值影响 C++ 对象或将 C++ 元素转换为 Lua 值(另一个用户数据或基本类型,如 bool、number、string)。用户数据作为参数传递给从我的 C++ 程序调用的类似事件的 Lua 函数。
luaL_newmetatable(L, "object");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); /* pushes the metatable */
lua_settable(L, -3); /* metatable.__index = metatable */
luaL_openlib(L, NULL, vallib_m, 0);
luaL_openlib(L, "internal", vallib_f, 0);
lua_pushstring(L, "__index");
lua_pushstring(L, "get");
lua_gettable(L, 2); /* get val.get */
lua_settable(L, 1); /* metatable.__index = val.get */
lua_pushstring(L, "__newindex");
lua_pushstring(L, "set");
lua_gettable(L, 2); /* get array.set */
lua_settable(L, 1); /* metatable.__newindex = val.set */ …Run Code Online (Sandbox Code Playgroud) 根据有关扩展切片的 Python 文档:
如果您有一个可变序列,例如列表或数组,您可以分配或删除扩展切片,但分配给扩展切片和常规切片之间存在一些差异。对常规切片的赋值可用于更改序列的长度:
Run Code Online (Sandbox Code Playgroud)>>> a = range(3) >>> a [0, 1, 2] >>> a[1:3] = [4, 5, 6] >>> a [0, 4, 5, 6]扩展切片不是那么灵活。分配给扩展切片时,语句右侧的列表必须包含与其要替换的切片相同数量的项目:
Run Code Online (Sandbox Code Playgroud)>>> a = range(4) >>> a [0, 1, 2, 3] >>> a[::2] [0, 2] >>> a[::2] = [0, -1] >>> a [0, 1, -1, 3] >>> a[::2] = [0,1,2] Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: attempt to assign sequence of size 3 to extended slice of …
以下代码始终有效还是依赖于编译器/平台?显然,我可以edges使用值构造函数进行初始化,但是我很好奇operator=在edges初始化为大小 0时复制赋值是否在此处工作,然后设置为等于带括号的 r 值。
它适用于我的 macbook。
std::vector<std::vector<int>> edges;
edges = {{1,2,3},{4},{5,6}};
Run Code Online (Sandbox Code Playgroud) StackOverflow 站点的问答中+=经常使用递增赋值运算符 ( )[PowerShell]来构造集合对象,例如:
$Collection = @()
1..$Size | ForEach-Object {
$Collection += [PSCustomObject]@{Index = $_; Name = "Name$_"}
}
Run Code Online (Sandbox Code Playgroud)
然而,它似乎是一种非常低效的操作。
一般情况下,+=在 PowerShell 中构建对象集合时应该避免使用递增赋值运算符 ( ) 是否可以?
Very often I process single elements of tuples like this:
size, duration, name = some_external_function()
size = int(size)
duration = float(duration)
name = name.strip().lower()
Run Code Online (Sandbox Code Playgroud)
If some_external_function would return some equally typed tuple I could use map in order to have a (more functional) closed expression:
size, duration, name = map(magic, some_external_function())
Run Code Online (Sandbox Code Playgroud)
Is there something like an element wise map? Something I could run like this:
size, duration, name = map2((int, float, strip), some_external_function())
Run Code Online (Sandbox Code Playgroud)
Update: I know I can …
python functional-programming variable-assignment python-3.x iterable-unpacking
C++ 不允许使用=. 但是允许使用 复制结构=,如本链接中所示 -> Copying array in C v/s copying structure in C 。它还没有任何可靠的答案。但请考虑以下代码
#include <iostream>
using namespace std;
struct user {
int a[4];
char c;
};
int main() {
user a{{1,2,3,4}, 'a'}, b{{4,5,6,7}, 'b'};
a = b; //should have given any error or warning but nothing
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码段没有给出任何错误和警告,而且工作正常。为什么?考虑解释两个问题(这个和上面链接的一个)。