鉴于以下内容:
#include <stdio.h>
class X;
class Y
{
public:
Y() { printf(" 1\n"); } // 1
// operator X(); // 2
};
class X
{
public:
X(int) {}
X(const Y& rhs) { printf(" 3\n"); } // 3
X(Y&& rhs) { printf(" 4\n"); } // 4
};
// Y::operator X() { printf(" operator X() - 2\n"); return X{2}; }
int main()
{
Y y{}; // Calls (1)
printf("j\n");
X j{y}; // Calls (3)
printf("k\n");
X k = {y}; // Calls (3) …Run Code Online (Sandbox Code Playgroud) 我有两个中型Web应用程序,我正在合并为一个.它们都是本地化的GetText并且具有大量的常见字符串,因此手动合并它们会非常烦人.合并这两个PO文件的最快方法是什么?
在现代C++中,我们可以初始化这样的向量:
std::vector<int> v = { 1, 2, 3, 4, 5 };
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试创建一个指向矢量的智能指针,这将无法编译:
auto v = std::make_shared<std::vector<int>>({ 1, 2, 3, 4, 5 });
Run Code Online (Sandbox Code Playgroud)
除了push_back在创作之后诉诸于s 之外还有其他更好的选择吗?
以下代码编译得很好:
struct B {
B(int) {}
};
struct D : B {
D() = default;
};
Run Code Online (Sandbox Code Playgroud)
直到我必须创建一个类的实例D:
D d; // error: use of deleted function 'D::D()'
Run Code Online (Sandbox Code Playgroud)
没有任何理由(用例),以允许= default进行D的构造,当它的实际工作为= delete;?
为什么仅当存在多个结构成员时才添加填充的概念,为什么在存在单个基本数据类型成员时不包括填充?
如果我们考虑在32位机器上
struct
{
char a;
} Y;
Run Code Online (Sandbox Code Playgroud)
没有填充,并且Y的大小为1个字节.
如果我们考虑这个结构
struct
{
char a;
int b;
} X;
Run Code Online (Sandbox Code Playgroud)
X的大小为8字节.
我的问题是为什么在第二种情况下填充添加?如果要通过机器进行高效访问,通常以4字节的倍数读取数据,那么为什么第一种情况下没有填充?
如果我抓住了,BaseException这还会捕获源自的异常BaseException吗?异常处理是否关心继承等,还是只匹配被捕获的确切异常类型?
class MyException {
...
};
class MySpecialException : public MyException {
...
};
Run Code Online (Sandbox Code Playgroud)
void test()
{
try {
...
}
catch (MyException &e) {
//will this catch MySpecialException?
}
}
Run Code Online (Sandbox Code Playgroud) std::unique_ptr<int[]> p(new int[10]); //ok
std::shared_ptr<int[]> p(new int[10]); //Error
shared_ptr<int> sp( new int[10],[](int *p){delete [] p;});
//Ok, writing custom deleter for
//array since shared_ptr will call
//delete by default.
Run Code Online (Sandbox Code Playgroud)
与unique_ptr相比,对于数组,shared_ptr签名是否有任何特殊原因?
如果两个api都遵循类似的签名,那会更简单.
想象一下,我有一个类,其主要职责是填充数据容器.我想将数据移出这个类.所以我有:
class cCreator
{
public:
void generate()
{
///generate the content of data_ and populate it
....
....
}
//variation 1
std::vector<int>&& getData1()
{
return std::move(data_);
}
//variation 2
std::vector<int> getData2()
{
return std::move(data_);
}
private:
std::vector<int> data_
};
Run Code Online (Sandbox Code Playgroud)
S getData()的变体1和变体2之间有什么区别?当我从函数定义中省略&&时会发生什么变化?
经过一些延迟测量测试后,我发现我需要优化在具有相当慢的FPU的嵌入式CPU上完成的毕达哥拉斯三角形计算.
问题是,如果这些计算发生,它们会有数字,这会弄乱时间.我无法减少绝对计算次数.但不知何故,他们需要加快......至少因素5: - /
我目前正在考虑预处理这些计算,因为不同值的输入范围以某种方式限制在大约300-500个排列,并且两个表条目之间的插值应该足够.但我也想知道如果使用一些条件来解决问题,也可以加快这段代码的速度:
float h = 0.f, v=0.f;
/// ...
float const d = std::sqrt( (h*h) + (v*v) );
Run Code Online (Sandbox Code Playgroud)
我尚未使用过:
我不知道是否有一些整数定点计算可用于平方根,或者该函数是否可以替换为精度较低或以某种方式使用纵横比的函数.
有任何想法吗?
谢谢!
我需要编写一个从用户那里获得5个正数的代码。这是我的代码:
cout << "Write 5 positive numbers:" << endl;
int input;
int num[5];
for(int i = 0; i <= 4; ++i){
cin >> input;
if(input < 0){
cout << "Error" << endl;
}else{
num[i] = input;
}
}
for(int i = 0; i <= 4; ++i){
cout << "Number " << i << " = " << num[i] << endl;
}
Run Code Online (Sandbox Code Playgroud)
问题在于数组只能存储正数。当我输入negative时num,它也存储它,然后打印垃圾值。例如,输入为:3 -2 1 6 8输出为:
Number 0 = 3
Number 1 = -1608404014
Number 2 = 1 …Run Code Online (Sandbox Code Playgroud)