我一直认为这std::vector是"作为阵列实施的一般智慧",等等等等等等.今天我去了测试它,似乎不是这样:
这是一些测试结果:
UseArray completed in 2.619 seconds
UseVector completed in 9.284 seconds
UseVectorPushBack completed in 14.669 seconds
The whole thing completed in 26.591 seconds
Run Code Online (Sandbox Code Playgroud)
这大约慢了3-4倍!没有真正证明" vector可能会慢几纳米"的评论.
我使用的代码:
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/microsec_time_clock.hpp>
class TestTimer
{
public:
TestTimer(const std::string & name) : name(name),
start(boost::date_time::microsec_clock<boost::posix_time::ptime>::local_time())
{
}
~TestTimer()
{
using namespace std;
using namespace boost;
posix_time::ptime now(date_time::microsec_clock<posix_time::ptime>::local_time());
posix_time::time_duration d = now - start;
cout << name << " completed in " << …Run Code Online (Sandbox Code Playgroud) 我必须将复杂数据从C++传递给C函数.在C++中,数据自然存储在一个
std::vector<std::complex> c.C函数期望数据为double的数组,double a[]例如a [0] = Re(c [0]),a [1] = Im(c [0]),a [2] = Re(c [1 ])等
传递此类数据的最佳安全方法是什么?像铸造一样
(double*) (&c[0])
Run Code Online (Sandbox Code Playgroud)
惹麻烦?
很抱歉,如果这是重复的,我只能找到有关将C++复合体传递给C99复合体的相关问题的信息.