我知道的是,除非另有说明,所有的标准库函数接受右值引用参数都保证要离开移动,从参数中有效,但不确定状态,而这里的一些例子可以表现出不确定的行为结果,而是根本问题不依赖于此.
以下程序:
// testmove1.cpp
#include <iostream>
#include <string>
int main() {
std::string s1{"String"};
std::cout << "s1: [" << s1 << "]" << std::endl;
std::string s2{std::move(s1)};
std::cout << "s2: [" << s2 << "]" << std::endl;
std::cout << "s1 after move: [" << s1 << "]" << std::endl; // Undefined
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
paul@local:~$ ./testmove1
s1: [String]
s2: [String]
s1 after move: []
paul@local:~$
Run Code Online (Sandbox Code Playgroud)
s1移动后的输出对我来说似乎没有定义,但是留空的字符串至少是一个可行的选择.Valgrind报告为该程序进行了单一分配,这正是您所期望的.
如果我做了一些非常相似的事情,但是有了一个班级成员,我会得到不同的结果:
// testmove2.cpp
#include <iostream>
#include <string>
class MyClass {
std::string m_data;
public:
MyClass(const …Run Code Online (Sandbox Code Playgroud) 我独立完成了斯坦福大学的iTunes U编程iOS 7课程,因此我现在有一个问题,我不能问老师,我希望有人可以帮助我.理想情况下,通过告诉我如何追踪这类问题,但我也在http://talix.homeip.net/2014/Matchismo.zip发布我的整个项目(因为我认为我不能有用地发布用户可以在这篇文章中调试它,就像普通的文本代码一样),所以只要告诉我具体问题在哪里也会非常感激!对于任何参加同一课程的人来说,我正处于任务3的中间位置.
我有一个非常基本的卡片匹配游戏,可以玩扑克牌或"设置".扑克牌标签效果很好.只要我按下新游戏按钮或点击卡片(这会导致开始新游戏),"设置"选项卡会以未捕获的异常(下面复制)崩溃.我在我的代码中放了一个断点,以便捕获New Game操作,并逐步完成所有生成的代码.当我踩过我的代码时,这一切似乎都很好.当我到达我的最后一个函数的末尾时,由于操作而被调用,然后我按下XCode中的Continue,这就是它崩溃的时候.鉴于这一事实,我不确定如何追踪导致问题的确切原因.
另一个问题:从异常文本发布整个调用堆栈是否有用,或者顶级耦合行是唯一真正有用的(对于像我这样的初学者)?我想我看到有问题的无法识别的选择器是[__NSCFConstantString _isDefaultFace]:但是我不知道在哪里调用它作为我假设的一部分是我使用的类的类型后面的UI代码(UILabel,等等.).
以下是整个异常文本以防万一:
2014-09-28 18:45:54.337 Matchismo[17292:2714082] -[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0xc721c
2014-09-28 18:45:54.346 Matchismo[17292:2714082] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0xc721c'
*** First throw call stack:
(
0 CoreFoundation 0x01cfddf6 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x01987a97 objc_exception_throw + 44
2 CoreFoundation 0x01d05a75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x01c4e9c7 ___forwarding___ + 1047
4 CoreFoundation 0x01c4e58e _CF_forwarding_prep_0 + 14
5 UIFoundation …Run Code Online (Sandbox Code Playgroud) 我需要找到一个在平面上只有三个坐标点的三角形的角度。在等式末尾的规则三角学中,我将使用:
cos = (a ** 2) - (b ** 2) - (c ** 2) / -2 * b * c
Run Code Online (Sandbox Code Playgroud)
我使用**,和的幂作为运算符sideA,sideB并且sideC是三角形边的长度。
我目前正在使用math.acos()该角度,但是遇到了数学域错误。是math.acos()用什么我所理解的反余弦正确的功能?
这是我程序的代码摘录:
x = 100
y = 100
centerX = x + 50
centerY = y + 50
if event.type == MOUSEMOTION:
mousex, mousey = event.pos
sideA = math.sqrt((x - mousex)**2)+((y - mousey)**2)
sideB = math.sqrt((centerX - mousex)**2)+((centerY - mousey)**2)
sideC = math.sqrt((centerX - x)**2)+((centerY - y)**2) …Run Code Online (Sandbox Code Playgroud) 当我为我的算法编写基准测试时,我对一个问题感到困惑!
我的测试代码详细信息已推送到 github,我将其复制到此处并添加一些注释。
https://github.com/hidstarshine/Algorithm/blob/master/leet/problem24_test.go
var TDBenchmarkSwapPairs1 *leet.ListNode
// This function may be not good, it should be init()?
func FTDBenchmarkSwapPairs1() {
TDBenchmarkSwapPairs1 = &leet.ListNode{
Val: 0,
Next: nil,
}
changeNode := TDBenchmarkSwapPairs1
for i := 1; i < 100; i++ {
changeNode.Next = &leet.ListNode{
Val: i,
Next: nil,
}
changeNode = changeNode.Next
}
}
func BenchmarkSwapPairs1(b *testing.B) {
FTDBenchmarkSwapPairs1() // problem is here
for i := 0; i < b.N; i++ {
leet.SwapPairs1(TDBenchmarkSwapPairs1)
}
}
Run Code Online (Sandbox Code Playgroud)
在问题行中,我调用FTDBenchmarkSwapPairs1(FTD表示填充测试数据)来初始化数据。
然后令人惊奇的事情发生了,BenchmarkSwapPairs1 似乎在许多 goroutine 中运行。 …
我从我的书中复制了这个练习.
使用此声明char*string ="hiii"; 什么是有价值的
1) string[0]
2) *string
3) string[99]
4) *string+8
Run Code Online (Sandbox Code Playgroud)
我编写以下代码试图显示其值string[0],但是当我运行代码时,它崩溃了.谁能告诉我为什么我的代码崩溃了?
int main(int argc, char *argv[]) {
char *string = "hiii";
printf("%s", string[0]);
return 0;
}
Run Code Online (Sandbox Code Playgroud) c ×1
c++ ×1
cocoa-touch ×1
exception ×1
go ×1
go-testing ×1
ios ×1
math ×1
objective-c ×1
python ×1
trigonometry ×1