我正在尝试将char*值从循环中分配到队列中.我一直在复制指针,而不是自己分配数据.我觉得这是一个简单的答案,但我能找到一个很好的例子.
例
while(something) {
next = queue.front();
queue.pop();
while(something) {
/* do work */
/* text has new value of char* */
queue.push(text);
}
}
Run Code Online (Sandbox Code Playgroud)
显然,这不起作用,因为当我为文本分配新值时,队列中的所有条目都成为新值.我需要知道如何正确复制/分配.
Python 2.7.1
我想明白为什么我不能做以下似乎是明智的事情
def do_stuff():
# return a function which takes a map as an argument and puts a key in there
f = lambda map: map['x'] = 'y' #compilation error
return f
x = do_stuff()
map = {}
x(map)
print map['x']
Run Code Online (Sandbox Code Playgroud)
我可以让lambda函数变得更简单,f = lambda map: os.path.exists但是我不能让它改变地图.谁能告诉我如何实现这一目标?如果这根本不可能,为什么?
我试图在python 3.3中运行一个用python 2.7(?)创建的旧python代码,我不得不更新代码来运行.它一直告诉我"'范围'对象不支持项目分配",而且我的生活无法弄明白.代码是我在谷歌上发现的"50州琐事"游戏.
错误在线 answer[i] = "%s " % flower[pick[i]].rstrip()
谢谢你的帮助!
pick = random.sample(range(50), 4)
print("The state flower of %s is:" % state[pick[0]])
answer = range(4)
for i in range(4):
if i == 0:
answer[i] = "%s " % flower[pick[i]].rstrip()
else:
answer[i] = "%s" % flower[pick[i]].rstrip()
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这段代码在这里
我只是检查一些表达式的正则表达式,在方法中,我一次又一次地重用该变量
Pattern pattern = Pattern.compile("([^test])");
Matcher matcher = pattern.matcher("SS");
if (matcher.find()) {
return false;
}
//Making it 'null' before re-use
pattern = null;
matcher = null;
pattern = Pattern.compile("([newtest]){4,}");
matcher = pattern.matcher("test");
if (matcher.find()) {
return false;
}
pattern = null;
matcher = null;
pattern = Pattern.compile("[stack]{2,}");
matcher = pattern.matcher("overflow");
if (matcher.find()) {
return false;
}
Run Code Online (Sandbox Code Playgroud)
null在设置其他新值之前,将变量设置为好吗?
它有意义吗?
注意:它可能是非标准的符号,再次使用相同的变量,但我只是想知道在重新赋值之前是否可以使变量无效?
有一个更好的方法吗?
a, b, c, = "yyy", "yyy", "yyy"
Run Code Online (Sandbox Code Playgroud)
明显的尝试失败了
a, b, c, = "yyy"
a, b, c = "yyy"*3
Run Code Online (Sandbox Code Playgroud)
从技术上讲,以下工作,但我不认为这是直观的,因为这个逻辑说a,b和c是相同的,而我想要做的就是说它们初始化为相同的值
a=b=c="yyy"
Run Code Online (Sandbox Code Playgroud) 假设我有一个带有重写赋值运算符的类:
class Test
{
public:
Test& operator =(const Test&) {return *this;}
};
Test f();
Run Code Online (Sandbox Code Playgroud)
有没有办法让它成为编译时错误来分配函数的结果?
例子:
f() = test();
Test t;
f() = t;
// if there were also a Test Test::operator+(Test);
(t + t) = test();
Run Code Online (Sandbox Code Playgroud)
原始类型已经发生这种情况,我想为这个类复制它:
int g();
g() = 5; // error
(3 + 4) = 5; // error
Run Code Online (Sandbox Code Playgroud)
编辑:我使用的是Visual C++,它不支持所有的C++ 11.具体来说,它不支持ref-qualifiers.
当我将一个布尔表达式的计算结果分配给一个变量时,我在PHP脚本中看到了一个非常奇怪的行为.
当我评估这段代码时......
<?php var_dump($is_admin or ($logged_in and ($user->uid === $uid))); ?>
Run Code Online (Sandbox Code Playgroud)
......结果是:
bool(true)
Run Code Online (Sandbox Code Playgroud)
但是,当我将布尔表达式赋值给变量时,例如......
<?php $can_edit = $is_admin or ($logged_in and ($user->uid === $uid)); ?>
<?php var_dump($can_edit); ?>
Run Code Online (Sandbox Code Playgroud)
......结果变成:
bool(false)
Run Code Online (Sandbox Code Playgroud)
是什么导致了这种行为?我正在使用PHP 5.3.10和Drupal 7
php boolean-logic drupal variable-assignment boolean-expression
关于这一点有很多类似的问题,但是我还没有找到一个明确列出别名差异的答案,所以我在这里问.
我知道一个简单的原始赋值语句复制值:
int x = 1;
int y = x;
x = 2;
StdOut.print(x); // prints 2
StdOut.print(y); // prints 1
Run Code Online (Sandbox Code Playgroud)
然后我被告知在赋值语句中数组是"别名".所以:
int[] x = {1, 2, 3, 4, 5};
int[] y = x;
x[0] = 6;
StdOut.print(x[0]); // prints 6
StdOut.print(y[0]); // prints 6
Run Code Online (Sandbox Code Playgroud)
但是,如果您将其中一个变量指定为完全不同的数组,则此别名会"消失":
int[] x = {1, 2, 3, 4, 5};
int[] y = x;
x = new int[]{1, 2, 3, 4, 5};
x[0] = 6;
StdOut.print(x[0]); // prints 1
StdOut.print(y[0]); // prints 6
Run Code Online (Sandbox Code Playgroud)
这是什么原因?
然后,我来参考类型.使用赋值语句时,它是复制的引用,而不是值.所以:
Counter …Run Code Online (Sandbox Code Playgroud) 因此,根据规范,在调用defer函数时会计算defer函数中的值,但是在封闭函数返回之前不会执行这些操作.我得到了这个,并且理解了整个'for i:= 0; i <4; i ++ defer example打印3210'的事情.
但是,当我尝试使用延迟临时分配覆盖值(将max m分配给队列长度q)时,请确保在完成后重置值(示例为演示简化):
type tss struct {
q int
m int
}
func (t *tss) test() {
if true {
defer func() {
t.q=t.q //this should evaluate to 't.q = 50' right?
fmt.Println("assigned",t.q,"to t.q")
}()
t.q = t.m
}
fmt.Printf("q=%v, m=%v\n", t.q, t.m)
}
func main() {
ts := tss{50,1}
fmt.Printf("q=%v, m=%v\n", ts.q, ts.m)
ts.test()
fmt.Printf("q=%v, m=%v\n", ts.q, ts.m)
}
Run Code Online (Sandbox Code Playgroud)
我希望收到输出:
q=50, m=1
q=1, m=1
assigned 50 to t.q
q=50, m=1
Run Code Online (Sandbox Code Playgroud)
但实际输出是: …
我正在尝试对c-string进行冒泡排序 - 请参阅下面的函数.目的是根据ASCII码对字符进行排序.
编译器在运行时不会抱怨,此时我会遇到分段错误.我运行了gdb,程序运行正常,直到第12行.在第12行,gdb调试器声明'Process gdb-inferior killed'.
为什么第12行不正确?我在程序的其他地方遇到过这个问题 - 无法将字符分配给c字符串的元素.我知道strcpy()需要字符串赋值,但是对于特定的元素,我认为它没问题.任何帮助很多人赞赏.
1. void bubblesort(char *str)
2. {
3. int length = strlen(str);
4.
5. for (int i = length - 1; i > 0; i--)
6. {
7. for (int j = 0; j < i; j++)
8. {
9. if (str[j] > str[j + 1])
10. {
11. char temp = str[j + 1];
12. str[j + 1] = str[j];
13. str[j] = temp;
14. }
15. }
16. }
17. …Run Code Online (Sandbox Code Playgroud)