我是一名学习C/C++的Java程序员.所以我知道Java有一个类似System.arraycopy()的函数; 复制数组.我想知道C或C++中是否有复制数组的函数.我只能通过使用for循环,指针等找到复制数组的实现.有没有我可以用来复制数组的函数?
查看matplotlib文档,似乎添加AxesSubplot到a 的标准方法Figure是使用Figure.add_subplot:
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )
Run Code Online (Sandbox Code Playgroud)
我希望能够AxesSubPlot独立于图形创建类似对象,因此我可以在不同的图中使用它们.就像是
fig = pyplot.figure()
histoA = some_axes_subplot_maker.hist( some params ..... )
histoA = some_axes_subplot_maker.hist( some other params ..... )
# make one figure with both plots
fig.add_subaxes(histo1, 211)
fig.add_subaxes(histo1, 212)
fig2 = pyplot.figure()
# make a figure with the first plot only
fig2.add_subaxes(histo1, 111)
Run Code Online (Sandbox Code Playgroud)
这是可能的matplotlib,如果可以,我该怎么做?
更新:我还没有设法解除Axes和Figures的创建,但是下面的答案中的示例可以很容易地在new或olf Figure实例中重用以前创建的轴.这可以通过一个简单的功能来说明:
def plot_axes(ax, fig=None, …Run Code Online (Sandbox Code Playgroud) 我试图以一种std::vector<std::unique_ptr<std::string>>与Bjarne Stroustrup的C++ 11 FAQ相同的方式初始化一个:
using namespace std;
vector<unique_ptr<string>> vs { new string{"Doug"}, new string{"Adams"} }; // fails
unique_ptr<string> ps { new string{"42"} }; // OK
Run Code Online (Sandbox Code Playgroud)
我看不出这个语法失败的原因.这种初始化容器的方式有问题吗?
编译器错误消息很大; 我找到的相关部分如下:
/usr/lib/gcc-snapshot/lib/gcc/i686-linux-gnu/4.7.0/../../../../include/c++/4.7.0 /bits/stl_construct.h:77 :7:错误:没有匹配的调用函数
'std::unique_ptr<std::basic_string<char> >::unique_ptr(std::basic_string<char>&)'
修复此错误的方法是什么?
检查元素是否包含在数组/列表中的C++方法是什么,类似于inPython中的运算符?
if x in arr:
print "found"
else
print "not found"
Run Code Online (Sandbox Code Playgroud)
与Python的in运算符相比,C++等价物的时间复杂度如何?
看下面的代码.我知道它不返回局部变量的地址,但为什么它仍然有效并将imain()中的变量赋值为'6'?如果从堆栈内存中删除变量,它如何仅返回值?
#include <iostream>
int& foo()
{
int i = 6;
std::cout << &i << std::endl; //Prints the address of i before return
return i;
}
int main()
{
int i = foo();
std::cout << i << std::endl; //Prints the value
std::cout << &i << std::endl; //Prints the address of i after return
}
Run Code Online (Sandbox Code Playgroud) 天真的布尔否定
std::atomic_bool b;
b = !b;
Run Code Online (Sandbox Code Playgroud)
似乎不是原子的.我怀疑这是因为operator!触发了一个简单的演员bool.如何以原子方式执行等效否定?以下代码说明了天真的否定不是原子的:
#include <thread>
#include <vector>
#include <atomic>
#include <iostream>
typedef std::atomic_bool Bool;
void flipAHundredThousandTimes(Bool& foo) {
for (size_t i = 0; i < 100000; ++i) {
foo = !foo;
}
}
// Launch nThreads std::threads. Each thread calls flipAHundredThousandTimes
// on the same boolean
void launchThreads(Bool& foo, size_t nThreads) {
std::vector<std::thread> threads;
for (size_t i = 0; i < nThreads; ++i) {
threads.emplace_back(flipAHundredThousandTimes, std::ref(foo));
}
for (auto& thread : threads) …Run Code Online (Sandbox Code Playgroud) 我正在使用-O3编译程序以获得性能,使用-g编译调试符号(如果发生崩溃,我可以使用核心转储).有一件事困扰我,-g选项会导致性能下降吗?当我查看使用和不使用-g的编译输出时,我看到没有-g的输出比使用-g的编译输出小80%.如果额外的空间用于调试符号,我不关心它(我猜)因为在运行时没有使用这部分.但是如果没有-g的编译输出中的每条指令,我需要在编译输出中用-g做更多4条指令,我当然更愿意停止使用-g选项,即使代价是无法处理核心转储.
如何知道程序内部调试符号部分的大小,一般情况下使用-g进行编译会创建一个比没有-g编译的相同代码运行得慢的程序?
设置std::vector<int>范围的最佳方法是什么,例如3到16之间的所有数字?
我需要这样的东西:
void launch_task()
{
std::thread([](){ run_async_task(); });
}
Run Code Online (Sandbox Code Playgroud)
除了线程的析构函数将终止我的任务.我不需要对任务进行任何控制,也不需要返回值.它必须运行它,然后线程应该终止并且应该处理C++线程对象.我需要什么C++ 11工具?
我看过了std::async,但找不到我案例的用法示例.它似乎是一个非常复杂的系统,我需要以某种方式存储和操作std::future它或它变得同步(如果我的理解是正确的;我没有找到一篇好的明确文章std::async).