为什么#if满足以下代码中的条件:
#include <iostream>
#define VALUE foo
int main() {
#if VALUE == bar
std::cout << "WORKS!" << std::endl;
#endif // VALUE
}
Run Code Online (Sandbox Code Playgroud) std::list 是一个循环双向链表吗?使用它我可以执行以下操作:
#include <list>
int main(){
std::list<int> l = {10, 11, 12};
std::list<int>::iterator it = l.end();
it++;
std::cout << *it << std::endl; // prints 10
}
Run Code Online (Sandbox Code Playgroud) 我有这样的课:
class Car {
int x;
public:
Car() {cout << "Init car" << endl;}
Car(const Car & c) { cout << "Copy car" << endl;}
Car(const Car && c) { cout << "Move car" << endl;}
};
Run Code Online (Sandbox Code Playgroud)
当我想对 class 的对象进行值初始化时Car:
Car c = Car();
Run Code Online (Sandbox Code Playgroud)
仅调用默认构造函数。为什么没有调用复制构造函数或移动构造函数,因为存在赋值?
我有以下代码:
#include <iostream>
using namespace std;
struct A
{
A() {}
A(const A&) { cout << "copy const" << endl; }
A(A&) { cout << "copy non const" << endl; }
};
A f(A a)
{
return a;
}
int main() {
A a1 = f(A());
}
Run Code Online (Sandbox Code Playgroud)
该A(A&)拷贝构造函数被调用。为什么A(const A&)不调用,因为我传递了一个临时对象?当我注释掉A(const A&)复制构造函数时,程序不会编译。
为什么在下面的代码中:
short a = 4;
char b = 2;
cout << sizeof(a/b);
Run Code Online (Sandbox Code Playgroud)
sizeof(a/b) 是 4?为什么不是 2 作为短的尺寸?
std::map 是否将元素存储为std::pair?迭代地图看起来像这样:
#include <map>
int main() {
std::map<int, char> m;
m[1] = 'A';
m[2] = 'B';
m[3] = 'C';
std::map<int, char>::iterator it;
for(it = m.begin(); it != m.end(); it++)
{
std::cout << "Key: " << it->first;
std::cout << " Value: " << it->second;
std::cout << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud) 为什么在下面的非 POD 类中 x 被初始化为零?
class test {
public:
void print() {
cout << x << endl;
}
private:
int x;
};
int main(int argc, char** argv)
{
test * tst = new test();
tst->print();
cout << is_pod<test>::value << endl;
}
Run Code Online (Sandbox Code Playgroud)
tst->print() 和 is_pod() 都返回 0
将要
#include "filename"
Run Code Online (Sandbox Code Playgroud)
如果在当前目录中找不到或某些编译器可能找不到,则始终在标准包含目录中搜索文件?
是否可以使用此指令关闭在标准包含目录中搜索文件,例如 gcc?
我有以下代码:
class Rectangle
{
protected:
int a, b;
public:
Rectangle(int a, int b) : a(a), b(b) {}
int area() { return a*b; }
Rectangle operator+(const Rectangle & other)
{
Rectangle temp(0, 0);
temp.a = a + other.a;
temp.b = b + other.b;
return temp;
}
void operator=(const Rectangle & other)
{
a = other.a;
b = other.b;
}
};
class Square : public Rectangle
{
public:
Square(int a) : Rectangle(a, a) {}
};
int main()
{
Square s1(3);
Square s2(1); …Run Code Online (Sandbox Code Playgroud) 我们可以这样说auto并且decltype是泛型编程的一部分吗?
我们可以说这constexpr是元编程的一个特性吗?