在页146中Algorithms Fourth Edition,给出了快速联合算法的最差时间复杂度的证明.它说
假设具有i个节点的树的高度具有log(i)的最大高度.
给定i + j = k,i <= j,当i和j节点的树被联合时,树的高度= 1 + log(i)= log(i + i)<= log(i + j) = log(k).
我不明白为什么1 + log(i) = log(i + i).
为什么会出现T::T(T&)时,有T::T(const T&)哪个更适合copy?
(可能它用于实现move语义???)
原始描述(证明是错误的melpomene):
在C++11,支持一种新形式的复制构造函数T::T(T&)(来自cppreferene/Copy构造函数隐式声明的复制构造函数),我想知道它的实际用途是什么?
我尝试编译以下代码(最小例子,请参阅整个代码的编辑):
// a.c
#include <stdio.h>
#define _XOPEN_SOURCE
#include <unistd.h>
int main(int argc, char* argv[])
{
puts((const char*) crypt("AAAA", "$6$2222"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用clang-7 -lcrypt a.c并发出以下警告:
minimum.c:8:24: warning: implicit declaration of function 'crypt' is invalid in C99 [-Wimplicit-function-declaration]
puts((const char*) crypt("AAAA", "$6$2222"));
^
minimum.c:8:10: warning: cast to 'const char *' from smaller integer type 'int' [-Wint-to-pointer-cast]
puts((const char*) crypt("AAAA", "$6$2222"));
^
2 warnings generated.
Run Code Online (Sandbox Code Playgroud)
但./a.out似乎确实有效:
$6$2222$6GKY4KPtBqD9jAhwxIZGDqEShaBaw.pkyJxjvSlKmtygDXKQ2Q62CPY98MPIZbz2h6iMCgLTVEYplzp.naYLz1
Run Code Online (Sandbox Code Playgroud)
我发现如果我删除#include <stdio.h>并puts喜欢这个:
// new_a.c
#define _XOPEN_SOURCE
#include …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
template <class T>
bool value = typename T::type::value;
Run Code Online (Sandbox Code Playgroud)
哪里type是成员类型T,并value应该是一个静态数据成员type.
我编译clang++-6.0 -std=c++14并发出:
a.cc:2:37: error: expected '(' for function-style cast or type construction
bool value = typename T::type::value;
~~~~~~~~~~~~~~~~~~~~~~~^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
怎么纠正这个?
#include <cstdio>
#include <cstdint>
#include <cassert>
int main() {
std::uint64_t ui;
char c;
auto ret = std::sscanf("111K", "%lu64%[B, K, M, G]", &ui, &c);
assert(ret == 2);
assert(ui == 111);
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用从一个字符串sscanf读取 auint64_t和 a ,但每次我尝试此操作时char它都只读取它ui(断言失败)。ret == 2
鉴于:
//C++17
#include <string>
struct Foo {
int i;
std::string str;
};
int main() {
Foo foo{1, std::string("Hello, world!")};
}
Run Code Online (Sandbox Code Playgroud)
可以Foo::i直接Foo::str从1和std::string(...)初始化,而不是复制到其中,并解释为什么可以/不能使用 C++17 标准(可能是一些用于测试目的的代码)?
如果不能,需要多少份?
似乎我无法在 rust 中重新绑定参考变量
let mut arr = [1, 2, 3, 4];
let & mut a0 = & mut arr[0];
println!("a0 = {}", a0);
let mut i = 0;
a0 = &mut i; // mismatched types: expected integer, found `&mut {integer}`
Run Code Online (Sandbox Code Playgroud)
但我可以在里面重新绑定一个引用struct:
#[derive(Debug)]
struct Ref<'a, T> {
val: &'a T
}
let mut arr = [1, 2, 3, 4];
let mut r = Ref {
val: & arr[0]
};
println!("r = {:#?}", r);
r.val = & arr[1];
Run Code Online (Sandbox Code Playgroud)
我不能重新绑定引用变量是真的吗?
如果它不能,那么为什么 …