我只需要了解它们之间的区别是什么:
int *p = new int[5];
Run Code Online (Sandbox Code Playgroud)
和
int *p = new int(5);
Run Code Online (Sandbox Code Playgroud) 我很惊讶地发现,对于某些人T,这decltype(std::declval<T>())是不合法的:
#include <utility>
template<typename T>
using Alias = decltype(std::declval<T>());
// as expected
using A1 = Alias<int>;
using A2 = Alias<int(int)>;
// error: no matching function for call to 'declval<...>()'
using A3 = Alias<int(int) const>;
using A4 = Alias<int(int) volatile>;
using A5 = Alias<int(int) &>;
using A6 = Alias<int(int) &&>;
// and all combinations of the above
Run Code Online (Sandbox Code Playgroud)
cppreference似乎并不表明该错误是预期的。
还有其他declval<T>不能使用的类型吗?规范在哪里定义这些?
正如标题所说,我想知道math.log2(x). 我知道可以用 O(1) 复杂度用 C 编写这样的函数,但是我找不到有关在 Python 中实现此函数的任何信息。
stable_partition(vect.begin(), vect.end(), [](int x) { return x % 2 == 0; });
partition(vect.begin(), vect.end(), [](int x) {
return x % 2 == 0;
});
Run Code Online (Sandbox Code Playgroud)
上面的代码是为了解释两者之间的区别。
我正在编写一个简单的 C 程序,但遇到了一些困惑。下面是代码:
int main(void) {
int i, j, k;
i = 3;
j = 4;
k = 5;
printf("%d ", i < j || ++j < k);
printf("\n"); // LINE 1
printf("%d %d %d", i, j, k); // LINE 2
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,变量 j 从 4 开始。然后在第 1 行的 printf 语句中,我们将 j 的值增加 1(++j = 5)。
所以理论上,我会假设当 j 在 printf(第 2 行)中打印时,它打印为 5,因为我们在第 1 行中为 j 进行了增量。但是,每次运行代码时,第 2 行都会打印 j 的原始值,即 4,而不是 5。
有什么我想念的吗?
会using namespace ...增加编译时间还是会以某种方式影响性能?我听说这会稍微影响编译时间,但是为什么呢?
#define len(a) if (a == 8) 1 \
else if (a == 3) 0 \
else -1
Run Code Online (Sandbox Code Playgroud)
这段代码只是一个例子,我们如何使用嵌套的 if else。我不想使用三元运算符,因为在这种情况下我不能使用 else if 语句。
C++20std::span是一个非常好的编程接口。但是似乎没有一种简单的方法来获得跨度。这是我想要做的:
#include <iostream>
#include <span>
#include <string>
#include <vector>
void print(std::span<std::span<wchar_t>> matrix) {
for (auto const& str : matrix) {
for (auto const ch : str) {
std::wcout << ch;
}
std::wcout << '\n';
}
}
int main() {
std::vector<std::wstring> vec = {L"Cool", L"Cool", L"Cool"};
print(vec);
}
Run Code Online (Sandbox Code Playgroud)
这不编译。我该怎么做?
在什么情况下写可能无效 Type *p = nullptr;
而只有
class Type *p = nullptr;满足?
以下代码在 g++、clang 和 Visual Studio 上编译:
#define HEX(hex_) 0x##hex_
int main()
{
return HEX(BadC0de);
}
Run Code Online (Sandbox Code Playgroud)
与此修改一样,使用 C++14 数字分隔符:
return HEX(1'Bad'C0de);
Run Code Online (Sandbox Code Playgroud)
但这不能在 g++ 或 clang 上编译(它可以在 Visual Studio 上运行):
#define HEX(hex_) 0x##hex_
int main()
{
return HEX(A'Bad'C0de);
}
Run Code Online (Sandbox Code Playgroud)
g++ 输出:
<source>:4:1: warning: multi-character character constant [-Wmultichar]
4 | return HEX(A'Bad'C0de);
| ^
<source>: In function 'int main()':
<source>:4:17: error: expected ';' before user-defined character literal
4 | return HEX(A'Bad'C0de);
| ^~~~~~~~~
<source>:1:25: note: in definition of macro 'HEX'
1 | #define …Run Code Online (Sandbox Code Playgroud) c++ ×8
c ×2
algorithm ×1
c++14 ×1
c++20 ×1
clang++ ×1
class ×1
declval ×1
g++ ×1
logarithm ×1
parameters ×1
partition ×1
python-3.x ×1
std-span ×1
visual-c++ ×1