鉴于此结构:
struct Foo {
std::array<int, 8> bar;
};
Run Code Online (Sandbox Code Playgroud)
bar如果我没有实例,如何获取数组元素的数量Foo?
我有一个类似于这样的代码:
class AClass {
public:
struct AStruct { };
AClass(){}
private:
const AStruct m_struct;
};
int main() {
AClass a;
}
Run Code Online (Sandbox Code Playgroud)
error: constructor for 'AClass' must explicitly initialize
the const member 'm_struct'
Run Code Online (Sandbox Code Playgroud)
如果我指定一个C++ 11默认构造函数struct AStruct,我会得到同样的错误:
struct AStruct {
AStruct() = default;
};
Run Code Online (Sandbox Code Playgroud)
但是,这可以通过编写一个空体的构造函数来解决:
struct AStruct {
AStruct(){} // fixed
};
Run Code Online (Sandbox Code Playgroud)
为什么我需要指定一个空构造函数?它是不是通过结构的公共访问自动创建的?
为什么C++ 11默认构造函数不解决问题?
当我尝试编译两个不同的程序时,我遇到了似乎是同样的问题.他们每个人首先创建一个静态库,然后创建链接该库的主应用程序.我正在使用gcc 4.7.2在Mac OS Mavericks上工作.
计划1
这是我跑步时发生的事情make:
首先,库libfeat.a已创建,但我收到警告:
ar rc ../lib/libfeat.a imgfeatures.o utils.o sift.o kdtree.o minpq.o xform.o
ranlib ../lib/libfeat.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: warning for library: ../lib/libfeat.a the table of contents is empty (no object file members in the library define global symbols)
Run Code Online (Sandbox Code Playgroud)
然后,在编译应用程序时,它表示它不能使用该库,因为它不是为相同的体系结构(x86_64)构建的:
gcc -O3 -I../include `pkg-config --cflags opencv` `pkg-config --cflags gtk+-3.0` `pkg-config --cflags gsl` siftfeat.c -o ../bin/siftfeat -L../lib -lfeat `pkg-config --libs opencv` `pkg-config --libs gtk+-3.0` `pkg-config --libs gsl`
ld: warning: ignoring file ../lib/libfeat.a, file was built for archive …Run Code Online (Sandbox Code Playgroud) 根据cppreference,函数std::set和std::map emplace函数返回a std::pair<iterator,bool>,并带有一个bool值来说明插入是否实际发生.
但是,如果插入没有发生,则emplace_hint返回iterator插入元素或集合或映射中的现有元素.这里没有bool价值.
这些类似功能的界面是否有任何原因造成这种差异?
更新
仅当未提供提示时,函数才insert返回bool值.这与一致的行为emplace和emplace_hint.那么问题是:bool当给出提示时,是否有任何理由不返回?
我只能认为可能存在一些性能原因,因为用户通常在lower_bound/ upper_bound操作后提供提示,因此确保插入将会发生.
特定
std::list<std::vector<float>> foo;
std::vector<float> bar;
Run Code Online (Sandbox Code Playgroud)
如何bar在foo不复制数据的情况下移动到最后?
这个可以吗?
foo.emplace_back(std::move(bar));
Run Code Online (Sandbox Code Playgroud) 我需要计算表示为char数组的位集之间的汉明距离.这是一项核心操作,因此必须尽可能快.我有这样的事情:
const int N = 32; // 32 always
// returns the number of bits that are ones in a char
int countOnes_uchar8(unsigned char v);
// pa and pb point to arrays of N items
int hamming(const unsigned char *pa, const unsigned char *pb)
{
int ret = 0;
for(int i = 0; i < N; ++i, ++pa, ++pb)
{
ret += countOnes_uchar8(*pa ^ *pb);
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
在分析之后,我注意到在ints上运行更快,所以我写道:
const int N = 32; // …Run Code Online (Sandbox Code Playgroud) 我有一个代码如下:
void function(int parameter)
{
for( ... ) // a big loop
{
double a = ...;
for( ... ) // a big loop
{
double b = ...;
double value;
if(parameter == 1)
value = some_math_expression_1(a, b);
else if(parameter == 2)
value = some_math_expression_2(a, b);
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是,根据参数,我想将一些数学表达式应用于a和b.这个函数执行很多次并且必须很快,我想知道每次迭代的那些条件分支是否会引入我可以节省的开销.
现在,我写了这样的代码:
void function(int parameter)
{
if(parameter == 1)
function1();
else if(parameter == 2)
function2();
else
...
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我在每个代码中重复代码,我可以直接应用数学表达式functionX().显而易见的问题是,当我想要更改某些代码时,我必须多次执行此操作(现在我有大约10个数学表达式).
我可以使用什么方法来避免任何开销function?
如果我一个指针传递给函数some_math_expression_X …
我有两个与此类似的课程:
class Foo {
public:
void bar() {
std::lock_guard<std::mutex> lock(m_mutex);
m_data.push_back('x');
}
private:
std::string m_data;
std::mutex m_mutex;
};
class Pool {
public:
static std::shared_ptr<Foo> Create(int index) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_pool.size() > 10) {
m_pool.erase(m_pool.begin());
}
std::shared_ptr<Foo>& ptr = m_pool[index];
if (!ptr) ptr.reset(new Foo);
return ptr;
}
private:
static std::mutex m_mutex;
static std::map<int, std::shared_ptr<Foo>> m_pool;
};
Run Code Online (Sandbox Code Playgroud)
和几个运行此代码的线程:
void parallel_function(int index) {
// several threads can get the same index
std::shared_ptr<Foo> foo = Pool::Create(index);
foo->bar();
}
Run Code Online (Sandbox Code Playgroud)
所有成员函数(包括复制构造函数和复制赋值)都可以由共享_ptr 的不同实例上的多个线程调用,而无需额外的同步,即使这些实例是同一对象的副本并共享所有权。如果多个执行线程在没有同步的情况下访问同一个shared_ptr,并且这些访问中的任何一个使用shared_ptr的非常量成员函数,那么就会发生数据竞争;原子函数的shared_ptr重载可用于防止数据竞争。
两个问题: …
我采取一个allocator的std::map和std::set在C++ 14.分配器必须提供一次pointer allocate(size_type n)为n项目分配空间的功能.
经过一些测试,我已经看到std::map并且std::set总是allocate(1)在我的平台上做,我还没有看到任何n > 1.如果我考虑内部树表示,对我来说是有意义的.
标准是否保证了这种行为?或者我可以安全地信任n == 1任何特定平台吗?
在OpenCV中,cv::FeatureDetector通过提供功能的名称来创建a是很常见的:
cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create("SURF");
Run Code Online (Sandbox Code Playgroud)
这是一个工厂模式,是cv::FeatureDetector一个抽象类.
然后,给定一个类型的变量cv::Ptr<cv::FeatureDetector>,是否可以检索该功能的名称?这是"SURF"我的例子.