标签: mutable

在Python中创建N*N*N列表的问题

我正在尝试在Python中创建一个三维N N N列表,如下所示:

n=3
l = [[[0,]*n]*n]*n
Run Code Online (Sandbox Code Playgroud)

不幸的是,这似乎没有正确"克隆"列表,因为我认为它会:

>>> l
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
>>> l[0][0][0]=1
>>> l
[[[1, 0, 0], [1, 0, 0], [1, 0, 0]], [[1, 0, 0], [1, 0, 0], [1, 0, 0]], [[1, 0, 0], [1, 0, 0], [1, 0, 0]]]
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

python list mutable

2
推荐指数
1
解决办法
1031
查看次数

属性和变量之间的区别

class MyClas
{
   public System.Windows.Point p;
   public void f()
   {
      p.X = 0;
   }
}
Run Code Online (Sandbox Code Playgroud)

这段代码完美无缺.

同时这个导致编译错误("无法修改p的返回值,因为它不是变量"):

class MyClas
{
   public System.Windows.Point p {get; set;}
   public void f()
   {
      p.X = 0;
   }
}
Run Code Online (Sandbox Code Playgroud)


有什么不同?

c# struct mutable

2
推荐指数
1
解决办法
326
查看次数

C++中可变的动机

可能重复:
C++'mutable'关键字
何时使用C++'mutable'关键字?

我理解mutable它的用法和方式,我想知道的是它存在的真正动机是什么.我不认为的唯一动机是绕过的不变性thisconst成员函数我宁愿认为这是给它更多的东西.
我不认为这只是在设计不良的系统中绕过问题的手段吗?或者是吗?

原始问题的明显分支何时使用mutable即使在良好的设计中也是有意义的?

c++ mutable

2
推荐指数
3
解决办法
1260
查看次数

强制外部函数为const

这是我的问题.我创建了一个类,其成员函数声明为const使用我无法修改的外部函数(在其他人的代码中声明)并且未声明const.更确切地说

别人的代码

class B {
public:
    void foo();
};
Run Code Online (Sandbox Code Playgroud)

我的代码

class A : public B {
public:
    void bar() const {
        this->foo();
    }
};
Run Code Online (Sandbox Code Playgroud)

我知道对于成员数据,我们可以使用mutable或强制const正确性const_cast.我怎么能'破解' foo这样我的编译器才能理解我想使用它就好像它是const一样,即使它没有在别人的代码中声明它?

c++ const const-correctness mutable const-cast

2
推荐指数
1
解决办法
266
查看次数

为什么不将可变变量分配在一起而不被链接?

考虑以下:

>>> a = {}
>>> b = {}
>>> c = {}
>>> c['a'] = 'b'
>>> a
{}
>>> b
{}
>>> c
{'a': 'b'}
Run Code Online (Sandbox Code Playgroud)

好的,这一切都很好.正是我所期待的.那么我缩短它.

>>> a = b = c = {}
>>> c['a'] = 'b'
>>> a
{'a': 'b'}
>>> b
{'a': 'b'}
>>> c
{'a': 'b'}
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?其他不可变数据类型(如整数)不会发生这种情况.

>>> a = b = c = 0
>>> a += 1
>>> a
1
>>> b
0
>>> c
0
Run Code Online (Sandbox Code Playgroud)

我认为这可能与不变性有关,但这种行为对我来说很奇怪.谁能解释为什么会发生这种情况?

python mutable immutability variable-assignment

2
推荐指数
1
解决办法
111
查看次数

在objectiveC中使用int键映射

我需要将整数值映射到某种可变数组中的对象.什么是最好的方法来解决这个问题.我看到的唯一选择是使用objectiveC++ ...

std::map<int, id> theMap;  // if i can use id?
Run Code Online (Sandbox Code Playgroud)

或者将内注放入NSStringNSNumber用作钥匙NSMutableDictionary.

我在我的网络类中使用它,客户端将发送一些数据(密钥将在有效负载中).然后在读取时,将从数据包中提取密钥,然后将地图中的对象拉出以基于所接收的内容继续该程序.(我担心客户端无法接收有效负载或丢失有效负载.)

哪种方式最好/最快?

objective-c mutable map nsdictionary

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

可变变量的JS闭包

请帮助我找出循环期间本地"j"变量继续变化的原因:

var a1 = a2 = a3 = {};

for (var i = 1; i < 4; i ++ ) {
  (function(j){
    console.log(j);
    window['a'+j].fu = function(){
      console.log('fu:',j);
    };
  })(i);

}

a1.fu(); // returns "fu:,3" - why not 1?

a2.fu(); // returns "fu:,3" - why not 2?

a3.fu(); // returns "fu:,3"
Run Code Online (Sandbox Code Playgroud)

我在类似的问题上阅读了很好的答案,但这不符合我的情况.可以通过闭包访问可变变量.我怎样才能解决这个问题?

javascript closures mutable

2
推荐指数
1
解决办法
136
查看次数

F#:如何使用Argument Byref Int调用函数

我有这个代码:

let sumfunc(n: int byref) =
  let mutable s = 0 
  while n >= 1 do
    s <- n + (n-1)
    n <- n-1
  printfn "%i" s

sumfunc 6
Run Code Online (Sandbox Code Playgroud)

我收到错误:

(8,10): error FS0001: This expression was expected to have type
    'byref<int>'
but here has type
    'int'
Run Code Online (Sandbox Code Playgroud)

所以我可以告诉我这是什么问题,但我不知道如何解决它.我想我需要指定数字6是byref<int>某种方式.我只是不知道如何.我的主要目标是使n函数或函数参数可变,以便我可以在函数内部更改和使用它的值.

int f# function mutable byref

2
推荐指数
1
解决办法
292
查看次数

如何修复这两个for循环以允许修改向量内容?

我试图在使用Vec<f64>Vec内部制作的矩阵上做一个循环,然后逐个改变它的元素.

我似乎无法使其发挥作用; 我对语法仍然太困惑了......

extern crate rand;

use std::ptr;
use std::mem;

use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();
    let mut v: Vec<Vec<f64>> = Vec::new();
    v.push(vec![0f64; 35]);
    v.push(vec![0f64; 35]);
    v.push(vec![0f64; 35]);
    v.push(vec![0f64; 35]);

    let len = v.len();
    for &el in &v {
        for q in &mut el {
            q = rng.gen::<f64>();
            println!("{}", q);
        }
        println!("{:?}", el);
    }
    println!("float: {}", rng.gen::<f64>());
    //println!("vec: {:?}, len: {}",v,len);
}
Run Code Online (Sandbox Code Playgroud)

编译器说:

error[E0308]: mismatched types
  --> src/main.rs:19:17
   |
19 |             q = rng.gen::<f64>(); …
Run Code Online (Sandbox Code Playgroud)

for-loop mutable rust borrowing

2
推荐指数
1
解决办法
116
查看次数

c ++:即使成员变量是可变的,也会丢弃限定符

我有一些const/mutable和protected的问题.也许这些让我感到困惑所以我会举一个例子.

class Base 
    {
       virtual void foo() const ;
       protected :
          void bar( int y ){ d_x = y } ;
       private :
         mutable int d_x ;
    }
Run Code Online (Sandbox Code Playgroud)

所以基类有虚拟foo.

class Derived : public Base
{
   void foo() const { bar(5); }
   private :
      mutable int d_x ;
}
Run Code Online (Sandbox Code Playgroud)

所以在我的派生类中,我实现了foo哪个类bar然后写入私有d_x.

我认为很好 - 但编译器说:

passing 'Derived' as 'this' argument of bar discards qualifier.
Run Code Online (Sandbox Code Playgroud)

这是为什么?我认为通过使用mutable我可以使我的成员函数成为const.

c++ inheritance const mutable

2
推荐指数
1
解决办法
572
查看次数