在C++ 11中,我们鼓励使用auto作为变量类型,
这在初始化类如class和vector时是否也适用?
我的意思是我们应该写下面的内容:
auto a = 10;
auto b = MyClass();
auto c = vector<int>{1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
代替:
auto a = 10;
MyClass b;
vector<int> c = {1, 2, 3};
Run Code Online (Sandbox Code Playgroud) 在C++ 11中,使用lambda/for_each,我们如何从end迭代一个数组?
我尝试了以下,但都导致无限循环:
for_each (end(A), begin(A), [](int i) {
....
});
for_each (A.rend(), A.rbegin(), [](int i) {
...
});
Run Code Online (Sandbox Code Playgroud)
任何的想法?谢谢.
在C++/C++ 11中,我们如何声明std :: array的别名?
我的意思是这样的:
template<size_t N> using Array = array<int, N>;
int get(Array A, int index) {
return A[index];
}
Run Code Online (Sandbox Code Playgroud)
但这导致编译错误:数组不是类型.
什么是正确的方法?非常感谢.
为什么在C++中使用std :: swap()我不能交换地址?
例如,具有以下内容
int x = 1, y = 2;
std::swap(&x, &y);
Run Code Online (Sandbox Code Playgroud)
我收到编译错误:
错误:没有匹配函数来调用'swap(int*,int*)'
但如果我做以下事情:
int *px = &x, *py = &y;
std::swap(px, py);
Run Code Online (Sandbox Code Playgroud)
它工作正常.所以有一个swap(int*,int*)!
知道第一个版本有什么问题吗?提前致谢.