我有一个窗口的 CGWindowID 和我的 Mac 的所有 CGDirectDisplayID。然后我想知道哪个显示窗口。我尝试获取窗口的 CGWindowInfo?但找不到有用的信息。
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, windowID);
CFArrayApplyFunction(windowList, CFRangeMake(0, CFArrayGetCount(windowList)), &WindowListApplierFunction, this);
CFRelease(windowList);
Run Code Online (Sandbox Code Playgroud) 我有一个带有构造函数的类,该构造函数带有两个参数并将字段设置为这些值。我还希望通过在传递给构造函数的块内显式设置字段来初始化此类的对象。
构造函数:
class Grammar
attr_reader :rules, :start, ...
def initialize(rules, start)
@rules = rules
@start = start
...
end
end
Run Code Online (Sandbox Code Playgroud)
传递给构造函数的参数涉及创建几个对象,这些对象仅用作参数的中间构建块rules,start并且将这些对象的存在限制为传递给构造函数的块是有意义的。在此块中,我希望首先构建中间对象,然后直接设置新对象的字段。
我希望能够Grammar像这样实例化:
grammar = Grammar.new { |g|
...
# build rules and start
...
g.start = start
g.rules = rules
}
Run Code Online (Sandbox Code Playgroud)
我应该如何修改构造函数以允许两种初始化方法?
当我研究 clone vs dup 时,我尝试复制一个如下所示的对象:
a = {'key1' => 1, 'key2' => {'key3' => 3, 'key4' => 4}}.freeze
b = a.clone
c = a.dup
a['key1'] = 32
# => RuntimeError: can't modify frozen Hash
b['key1'] = 32
# => RuntimeError: can't modify frozen Hash
c['key1'] = 32
# => 32
Run Code Online (Sandbox Code Playgroud)
大写结果是,a 和 b 值共享内存,但是当我检查变量 object_id 时,它返回不同的结果:
a.object_id ## original
=> 70219554622880
b.object_id ## clone
=> 70219554589820
Run Code Online (Sandbox Code Playgroud)
但在哈希键 object_id 中是相同的。
a['key1'].object_id ## original
=> 3
b['key1'].object_id ## clone
=> 3
c['key1'].object_id …Run Code Online (Sandbox Code Playgroud) #include <iostream>
struct X
{
bool isNull() { return this == nullptr; }
bool isNullConst() const { return this == nullptr; }
};
bool isNull(X& x) { return &x == nullptr; }
bool isNullConst(const X& x) { return &x == nullptr; }
// always false or exception.
bool isNullCopy(X x) { return &x == nullptr; }
int main()
{
X* x = nullptr;
std::cout << x->isNull() << '\n';
std::cout << (*x).isNull() << '\n';
std::cout << isNull(*x) << '\n';
// std::cout …Run Code Online (Sandbox Code Playgroud) 我正在尝试用 C++ 编写一个缩小的 FizzBuzz 程序,因为我现在正在学习它。我想知道是否有一种速记方式可以说“如果此字符串存在,则返回此字符串,否则,返回下一部分”
在 JavaScript 中,使用||运算符的工作方式与我想的一样:
function fizzbuzz() {
const maxNum = 20; // Simplified down to 20 just for example
for (let i = 1; i <= maxNum; i++) {
let output = "";
if (i % 3 == 0) output += "Fizz";
if (i % 5 == 0) output += "Buzz";
console.log(output || i); // If the output has something, print it, otherwise print the number
}
}
fizzbuzz();Run Code Online (Sandbox Code Playgroud)
当我在 C++ 中尝试这个时,
#include <iostream> …Run Code Online (Sandbox Code Playgroud) 我原来的代码是:
const DNA: [char; 4] = ['A', 'C', 'G', 'T'];
...
let mut map: HashMap<char, usize> = HashMap::new();
/* initialize the hashmap */
for d in DNA.iter() {
map.insert(*d, 0);
}
Run Code Online (Sandbox Code Playgroud)
代码已编译。然后我想用 for_each 替换 for 循环:
DNA.iter().for_each(|d| map.insert(*d, 0));
Run Code Online (Sandbox Code Playgroud)
编译错误:
error[E0308]: mismatched types
--> src/lib.rs:26:29
|
26 | DNA.iter().for_each(|d| map.insert(*d, 0));
| ^^^^^^^^^^^^^^^^^ expected `()`, found enum `Option`
|
= note: expected unit type `()`
found enum `Option<usize>`
Run Code Online (Sandbox Code Playgroud)
看来for_each和for并不完全等价?为什么 for_each 不忽略 的返回值map.insert()?
如果我有一个带有方法“a”的基类和一个重新实现方法“a”的派生类,我可以通过调用 ; 从 Derived.a 调用superBase.a。如何从不同的派生类方法调用基类“a”?
class Base
def a
puts "hi, this is Base.a"
end
end
class Derived < Base
def a
puts "hi, this is Derived.a"
end
def b
# here is where I want to call Base.a
Base.a # this doesn't work
end
end
Run Code Online (Sandbox Code Playgroud) 在file1.cpp中,我将一些数据保存到内存地址为 0x0364DF00 的内存中。
在file2.cpp中,如何访问内存地址(0x0364DF00)的内容?
我尝试添加一些代码来解决这个问题,但没有成功。
#include <iostream>
using namespace std;
int main() {
int* address = (int*)0x0364DF00;
cout << address(&address);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这只是一个实验。我不知道是否可能。
我有一个任务,我需要将指针传递给我的函数并使用“write”将其显示在我的函数中。
#include <unistd.h>
#include <stdio.h>
void ft_ft(int *nbr){
//printf("%d\n", *nbr);
char c = *nbr;
write(1, &c, 1);
}
int main(){
int nbr = 42;
ft_ft(&nbr);
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我试图将 int 值转换为 char ,因为从参数中获取指针是行不通的。另一方面,这也不是,所以你能帮助我吗?
错误消息:
[正在运行] (((所在文件夹))) && gcc (((文件名)))... 输出:* [完成] 在 0.046 秒内退出,代码=0
所以它实际上运行了代码,但它不是我想要的输出,我错过了什么?
printf() 工作正常,但我无法让“write”工作。
提前致谢