小编jua*_*nza的帖子

是否有在C/C++中复制数组的函数?

我是一名学习C/C++的Java程序员.所以我知道Java有一个类似System.arraycopy()的函数; 复制数组.我想知道C或C++中是否有复制数组的函数.我只能通过使用for循环,指针等找到复制数组的实现.有没有我可以用来复制数组的函数?

c c++ arrays

73
推荐指数
9
解决办法
31万
查看次数

matplotlib:我可以创建AxesSubplot对象,然后将它们添加到Figure实例中吗?

查看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)

python matplotlib

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

初始化初始化列表中unique_ptrs的容器因GCC 4.7而失败

我试图以一种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++ compiler-errors initialization c++11

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

什么是Python的"in"运算符的C++等价物?

检查元素是否包含在数组/列表中的C++方法是什么,类似于inPython中的运算符?

if x in arr:
    print "found"
else
    print "not found"
Run Code Online (Sandbox Code Playgroud)

与Python的in运算符相比,C++等价物的时间复杂度如何?

c++ arrays

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

返回对本地或临时变量的引用

看下面的代码.我知道它不返回局部变量的地址,但为什么它仍然有效并将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)

c++

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

如何自动否定std :: atomic_bool?

天真的布尔否定

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)

c++ multithreading atomic c++11

40
推荐指数
1
解决办法
8366
查看次数

C++ 11的长期真的至少是64位吗?

在这里这里说这种类型long long至少和a一样长,int并且不少于64位.我一直在看C++ 11标准§3.9.1基本类型部分,我找不到任何64位的引用.我能找到的只是它至少与它一样长long int,至少与它一样长int.标准long long列为标准整数类型,而不是扩展类型,因此我想知道这个long long至少保持64位的断言是否为真.如果是,它在哪里陈述?请注意,我long long只是在讨论C++ 11标准.

c++ c++11

38
推荐指数
1
解决办法
4699
查看次数

用-g gcc标志编译的程序是否比没有-g编译的同一程序慢?

我正在使用-O3编译程序以获得性能,使用-g编译调试符号(如果发生崩溃,我可以使用核心转储).有一件事困扰我,-g选项会导致性能下降吗?当我查看使用和不使用-g的编译输出时,我看到没有-g的输出比使用-g的编译输出小80%.如果额外的空间用于调试符号,我不关心它(我猜)因为在运行时没有使用这部分.但是如果没有-g的编译输出中的每条指令,我需要在编译输出中用-g做更多4条指令,我当然更愿意停止使用-g选项,即使代价是无法处理核心转储.

如何知道程序内部调试符号部分的大小,一般情况下使用-g进行编译会创建一个比没有-g编译的相同代码运行得慢的程序?

c++ debugging optimization gcc

36
推荐指数
1
解决办法
9135
查看次数

将std :: vector <int>设置为范围

设置std::vector<int>范围的最佳方法是什么,例如3到16之间的所有数字?

c++ stl stdvector iota c++11

34
推荐指数
3
解决办法
3万
查看次数

什么是C++ 11启动异步任务并忘掉它的方法?

我需要这样的东西:

void launch_task()
{
    std::thread([](){ run_async_task(); });
}
Run Code Online (Sandbox Code Playgroud)

除了线程的析构函数将终止我的任务.我不需要对任务进行任何控制,也不需要返回值.它必须运行它,然后线程应该终止并且应该处理C++线程对象.我需要什么C++ 11工具?

我看过了std::async,但找不到我案例的用法示例.它似乎是一个非常复杂的系统,我需要以某种方式存储和操作std::future它或它变得同步(如果我的理解是正确的;我没有找到一篇好的明确文章std::async).

c++ multithreading c++11

28
推荐指数
2
解决办法
6277
查看次数