标签: assignment-operator

JavaScript中的`x = y,z`逗号分配

可能重复:
Javascript语法:逗号是什么意思?

我在阅读本文时遇到了代码(执行Ctrl+ F搜索Andre Breton):

//function returning array of `umbrella` fibonacci numbers
function Colette(umbrella) {
  var staircase = 0, galleons = 0, brigantines = 1, armada = [galleons, brigantines], bassoon;
  Array.prototype.embrace = [].push;

  while(2 + staircase++ < umbrella) {
    bassoon = galleons + brigantines;
    armada.embrace(brigantines = (galleons = brigantines, bassoon));
  }

  return armada;
}
Run Code Online (Sandbox Code Playgroud)

什么是x = (y = x, z)结构是什么意思?或者更具体地说,是什么y = x, z意思?我称之为逗号分配,因为它看起来像赋值并且有逗号.

在Python中,它意味着元组解包(或在这种情况下打包).这是同样的情况吗?

javascript variable-assignment assignment-operator assign iterable-unpacking

21
推荐指数
3
解决办法
7635
查看次数

如何实现std :: vector insert?C++

最近我正在重读ISO C++标准,并发现了非常有趣的说明:

请注意,std::vector上式的唯一约束Tstd::vector<T>是该类型T必须有拷贝构造函数.实际上,如果在插入时向量的内存已满,则分配一个新的内存size = 2 * oldSize(这是依赖于实现的),然后在其中复制旧元素并插入该元素.

可是等等??

要分配类型的新内存,我们需要这样的东西, ptr = new T[2*size];

  1. 如何做到这一点,因为类型T可能没有默认构造函数?
  2. 然后分配,在分配内存之后我们必须将旧值分配给新内存,对吧?
  3. 考虑到这两件事,如何std::vector用"仅复制构造器"来做到这一点?使用了哪些实现和语言习语?

c++ stl vector copy-constructor assignment-operator

21
推荐指数
2
解决办法
4767
查看次数

数组类型char []不可分配

那么这是我的第一篇文章.我一直在尝试做这个选择选择的东西,我希望用户只选择数字而不是键入它们(更容易)但是当我希望数字等于一个字符串时,它说"数组类型char[30]不可分配".即使在后面我也放了分号.

#include <stdio.h>

int main() {
  int choice1;
  char word[30];

  printf("You have three choice.\n");
  printf("[1] Jump [2] Run [3] Dance\n");
  scanf("%d",&choice1);
  if (choice1 == 1)
  {
    word = "Jump" //Error #1
  }
  else if (choice1 == 2)
  {
    word = "Eat" //Error #2
  }
  else if (choice1 == 3)
  {
    word = "Sleep"; //Error #3
  }

  printf("You will now be %sing",word);

}
Run Code Online (Sandbox Code Playgroud)

c arrays string assignment-operator

21
推荐指数
4
解决办法
5万
查看次数

Python赋值运算符与非赋值不同

我遇到了这种奇怪的行为,我找不到解释。

MWE:

l = [1]
l += {'a': 2}
l
[1, 'a']
l + {'B': 3}
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: can only concatenate list (not "dict") to list
Run Code Online (Sandbox Code Playgroud)

基本上,当我+=python 不会引发错误并将密钥附加到列表时,而当我只计算时,+我得到了预期的TypeError.

注意:这是 Python 3.6.10

python assignment-operator

20
推荐指数
1
解决办法
2723
查看次数

为什么赋值运算符会返回对象的引用?

我正在修改我的C++,我正在处理运算符重载,特别是"="(赋值)运算符.我在网上看到并且遇到了讨论它的多个主题.在我自己的笔记中,我把所有的例子都记下来了

class Foo
{
    public:  
        int x;  
        int y;  
        void operator=(const Foo&);  
};  
void Foo::operator=(const Foo &rhs)
{
    x = rhs.x;  
    y = rhs.y;  
}
Run Code Online (Sandbox Code Playgroud)

在我在网上找到的所有参考文献中,我注意到操作符返回对源对象的引用.为什么返回对象的引用的正确方法而不是什么都没有?

c++ operator-overloading return-type assignment-operator

19
推荐指数
3
解决办法
1万
查看次数

这个C++代码是否会泄漏内存?

struct Foo
{
    Foo(int i)
    {
        ptr = new int(i);
    }
    ~Foo()
    {
        delete ptr;
    }
    int* ptr;
};

int main()
{
    {
        Foo a(8);
        Foo b(7);
        a = b;
    }
    //Do other stuff
}
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,编译器将自动为其创建赋值运算符成员函数Foo.但是,这只需要输入ptrin 的值b并将其放入a.a最初分配的内存似乎丢失了.我可以a.~Foo();在进行赋值之前进行调用,但我听说你应该很少需要显式调用析构函数.所以让我们说我编写一个赋值运算符Foo,删除int左操作数的指针,然后将r值赋给l值.像这样:

Foo& operator=(const Foo& other) 
{
    //To handle self-assignment:
    if (this != &other) {
        delete this->ptr;
        this->ptr = other.ptr;
    }
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我这样做,那么当Foo aFoo …

c++ memory-leaks assignment-operator

19
推荐指数
2
解决办法
813
查看次数

在C++中编写复制构造函数和赋值运算符的清单

请编写复制构造函数和赋值运算符在C++中需要执行的任务列表,以保证异常安全,避免内存泄漏等.

c++ copy-constructor assignment-operator

18
推荐指数
1
解决办法
4704
查看次数

我必须在什么情况下为我的C++类提供赋值运算符,复制构造函数和析构函数?

假设我有一个类,其中唯一的数据成员是std::string或类似的std::vector.我是否需要提供复制构造函数,析构函数和赋值运算符?

c++ destructor copy-constructor assignment-operator

18
推荐指数
2
解决办法
4150
查看次数

Python:产量和产量分配

这个代码,包括赋值和yield运算符,如何工作?结果相当混乱.

def test1(x): 
    for i in x:
        _ = yield i 
        yield _
def test2(x): 
    for i in x:
        _ = yield i 

r1 = test1([1,2,3])
r2 = test2([1,2,3])
print list(r1)
print list(r2)
Run Code Online (Sandbox Code Playgroud)

输出:

[1, None, 2, None, 3, None] 
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)

python yield assignment-operator

18
推荐指数
2
解决办法
6375
查看次数

什么是R赋值运算符:= for?

通过深入研究R源代码(文件R-3.2.2/src/main/gram.y2836,2852)我发现R解析器/标记器认为这:=是一个LEFT_ASSIGNMENT标记.

但是当我尝试将它用作赋值运算符时R.3.2.2,
我有一个错误(无法找到:=...的函数)但是你可以看到R认为它是一个类似的赋值<-:

> myVar := 42
Erreur : impossible de trouver la fonction ":="
> :=
Erreur : unexpected assignment in ":="
> <-
Erreur : unexpected assignment in "<-"
Run Code Online (Sandbox Code Playgroud)

它是一个bug,还是:=需要从tokenizer源代码中删除令牌?

:=R中有关于运算符的过去的故事吗?

r assignment-operator data.table colon-equals

18
推荐指数
3
解决办法
4760
查看次数