在C++ 11中,如果复制和移动赋值都可用,则编译器会在参数为左值时自动选择复制赋值,如果是左值则移动赋值.使用std::move它可以明确地选择左值的移动分配.但是如何明确选择rvalue的复制赋值呢?
代码示例:
#include <iostream>
class testClass
{
public:
testClass &operator=(const int &other) {
std::cout << "Copy assignment chosen." << std::endl;
return *this;
}
testClass &operator=(int &&other) {
std::cout << "Move assignment chosen." << std::endl;
return *this;
}
};
int main(int argc, char *argv[])
{
int a = 4;
testClass test;
test = a; // Selects copy assignment
test = 3; // Selects move assignment
test = std::move(a); // Selects move assignment
// test = std::copy(3); // <--- …Run Code Online (Sandbox Code Playgroud) 在CMake中,您可以使用相对路径转换为绝对路径
get_filename_component(ABSOLUTE_PATH ${RELATIVE_PATH} ABSOLUTE)
Run Code Online (Sandbox Code Playgroud)
但是,诸如../../other_program/基于源目录(即文件所在的CMakeLists.txt目录)的路径,而不是构建目录(即从中cmake调用的目录).如果将相对路径指定为命令行选项,则可能会导致一些混淆.
有没有办法告诉get_filename_component它应该基于当前二进制目录而不是当前源目录的相对路径?
我已经分叉了一个github存储库,提交了一个提交(标题Some small changes),提交了一个pull请求,它被合并到主存储库中(提交标题Some small changes (#12),其中#12是pull请求号).到现在为止还挺好.
现在,当我想更新我的fork(git rebase upstream/master见这里)时,我在我的存储库中有两次这样的提交.先是Some small changes一次又一次Some small changes (#12).如果我创建一个新的pull请求,Some small changes则会再次将提交添加到pull请求中.
有两种方法可以解决此问题:
这两种情况都涉及重写我的历史并不得不强制推动.在提交拉取请求时,是否有更好的方法使fork保持同步?
在pyplot中,您可以使用该zorder选项或通过更改plot()命令的顺序来更改不同图形的顺序.但是,当您添加替代轴时ax2 = twinx(),新轴将始终覆盖旧轴(如文档中所述).
是否可以更改轴的顺序以将替代(双对)y轴移动到背景?
在下面的例子中,我想在直方图的顶部显示蓝线:
import numpy as np
import matplotlib.pyplot as plt
import random
# Data
x = np.arange(-3.0, 3.01, 0.1)
y = np.power(x,2)
y2 = 1/np.sqrt(2*np.pi) * np.exp(-y/2)
data = [random.gauss(0.0, 1.0) for i in range(1000)]
# Plot figure
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax2.hist(data, bins=40, normed=True, color='g',zorder=0)
ax2.plot(x, y2, color='r', linewidth=2, zorder=2)
ax1.plot(x, y, color='b', linewidth=2, zorder=5)
ax1.set_ylabel("Parabola")
ax2.set_ylabel("Normal distribution")
ax1.yaxis.label.set_color('b')
ax2.yaxis.label.set_color('r')
plt.show()
Run Code Online (Sandbox Code Playgroud)
编辑:由于某种原因,我无法上传此代码生成的图像.我稍后会再试一次.
标题<stdexcept>定义了几个标准异常.但是,我在确定何时使用哪个例外时遇到了麻烦.是否有良好的在线指南?我试着通过一个例子说明我的问题:
函数采用(物理)矢量的长度和角度(在0和pi之间)来返回新的矢量.如果角度是负的那就是
std::invalid_argument,因为负角度无效?std::logic_error,在这种情况下,负角度没有意义吗?std::out_of_range,因为负角度超出允许的角度范围?std::domain_error,因为数学函数没有在负角度上定义.(如果有人想知道:我试图在三斜模拟框中变换坐标,实际上是三个长度和三个角度 - 如果你有兴趣,请看这里.)
为了使用 python 的 matplotlib.pyplot 在一个图中绘制两个图,通常会这样做:
import matplotlib.pyplot as plt
xdata = [0, 1, 2, 3, 4]
ydata1 = [0, 1, 2, 3, 4]
ydata2 = [0, 0.5, 2, 4.5, 8]
plt.plot(xdata, ydata1, 'r-', xdata, ydata2, 'b--')
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是,我只想在某些情况下绘制第二个数据集,如下所示:
plt.plot(xdata, ydata1, 'r-')
if DrawSecondDataset:
plt.plot(data, ydata2, 'b--')
Run Code Online (Sandbox Code Playgroud)
不幸的是,plot第二次调用意味着第一个数据集被删除。
如何将图表添加到已有的绘图中?
编辑:
正如答案正确指出的那样,只有在plt.show()两者之间调用数据集才会被删除plt.plot()数据集才会被删除。因此,上面的示例实际上显示了两个数据集。
为了完整起见:是否可以选择将图形添加到plt.show()已调用的现有绘图中?例如
plt.plot(xdata, ydata1, 'r-')
plt.show()
...
plt.plot(data, ydata2, 'b--')
plt.show()
Run Code Online (Sandbox Code Playgroud) 使用GNU make,可以通过以下方式执行空运行
make -n
Run Code Online (Sandbox Code Playgroud)
CMake是否有类似的选择?我能想到的最好的是
cmake ..
make -n | grep -vi cmake
Run Code Online (Sandbox Code Playgroud)
但是,我过于严格,想知道是否有更简单的方法来实现这一目标。
monkeypatch是 pytest 中的一个很棒的工具,允许您替换当前测试范围内的任何函数。最棒的事情之一是甚至可以修补构造函数。然而不幸的是,我在修补析构函数时遇到了麻烦。它似乎只有在测试成功时才有效。如果测试失败,则调用常规构造函数。考虑这个代码:
class MyClass:
def __init__(self):
print("Constructing MyClass")
def __del__(self):
print("Destroying MyClass")
def test_NoPatch():
c = MyClass()
def test_Patch(monkeypatch, mocker):
monkeypatch.setattr(MyClass, '__init__', mocker.MagicMock(return_value=None))
monkeypatch.setattr(MyClass, '__del__', mocker.MagicMock(return_value=None))
c = MyClass()
def test_PatchWithFailure(monkeypatch, mocker):
monkeypatch.setattr(MyClass, '__init__', mocker.MagicMock(return_value=None))
monkeypatch.setattr(MyClass, '__del__', mocker.MagicMock(return_value=None))
c = MyClass()
assert False
Run Code Online (Sandbox Code Playgroud)
将给出以下结果:
====================================================================================================== test session starts ======================================================================================================
platform linux -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 -- /home/julian/devel/tests/test_pytest_monkeypatch/testenv/bin/python3
cachedir: .pytest_cache
rootdir: /home/julian/devel/tests/test_pytest_monkeypatch
plugins: mock-3.5.1
collected 3 items
test.py::test_NoPatch Constructing MyClass
Destroying MyClass
PASSED
test.py::test_Patch PASSED
test.py::test_PatchWithFailure FAILED …Run Code Online (Sandbox Code Playgroud) 是否有一种简单的方法来逃避\字符串中的所有出现?我从以下字符串开始:
#include <string>
#include <iostream>
std::string escapeSlashes(std::string str) {
// I have no idea what to do here
return str;
}
int main () {
std::string str = "a\b\c\d";
std::cout << escapeSlashes(str) << "\n";
// Desired output:
// a\\b\\c\\d
return 0;
}
Run Code Online (Sandbox Code Playgroud)
基本上,我正在寻找这个问题的反面.问题是我无法\在字符串中搜索,因为C++已经将它视为转义序列.
注意: 我无法在第一时间更改字符串str.它是从LaTeX文件解析的.因此,这个类似问题的答案不适用. 编辑:由于无关的问题,解析失败,这里的问题是关于字符串文字.
编辑:有很好的解决方案来查找和替换已知的转义序列,例如这个答案.另一种选择是使用boost::regex("\p{cntrl}").但是,我还没有找到适用于未知(错误)转义序列的转发序列.
c++ ×3
python ×3
c++11 ×2
cmake ×2
matplotlib ×2
exception ×1
git ×1
git-fork ×1
github ×1
makefile ×1
pull-request ×1
pytest ×1
python-3.x ×1
qsub ×1
slurm ×1
string ×1
unit-testing ×1