在以下代码中:
class SomeClass {
vector<int> i;
vector<bool> b;
public:
int& geti() {return i[0];}
bool& getb() {return b[0];}
};
Run Code Online (Sandbox Code Playgroud)
如果你注释掉getb(),代码编译得很好.显然,将引用返回到int存储在向量中的引用是没有问题的,但是你不能用a做bool.
为什么是这样?
我不明白这里发生了什么.
#include <iostream>
#include <random>
#include <chrono>
using namespace std;
unsigned number_in_range(unsigned, unsigned, default_random_engine);
int main()
{
time_t now = chrono::system_clock::to_time_t(chrono::system_clock::now());
default_random_engine rng(now);
//
// Print out 10 random numbers
//
for (int i = 0; i < 10; i++)
{
uniform_int_distribution<int> dist(0, 100);
cout << dist(rng) << endl;
}
cout << endl;
//
// Do the same thing, but get the numbers from `number_in_range()`
//
for (int i = 0; i < 10; i++)
{
cout << number_in_range(0, 100, …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
class SomeClass {};
class SomeOtherClass {
SomeClass& someObj;
public:
SomeOtherClass() {someObj = SomeClass();}
};
Run Code Online (Sandbox Code Playgroud)
而我得到的构造的错误SomeOtherClass说法Constructor for 'SomeOtherClass' must explicitly initialize the reference member 'someObj'.
所以我想我的问题非常紧张.你如何初始化参考?
我有一组小物件.这些对象中的每一个都指向其他对象.这些指针可以实现为实际指针,也可以作为对象数组或其他内容的索引.它可能是一个这样的指针数组,其长度可能会改变.可能存在指向相同类型和其他类型的对象的指针,但这在编译时是已知的.
所以,例如:我有一个人类.这个人有两个指向其父人的指针.还有另一个班级.每个人都有一个指向他/她访问过的所有地方的指针列表.
与实际的家谱相反,我可能希望通过删除/插入一些人来不时更改树.
为此目的,C++标准库(C++ 11)中是否有容器,还是应该更好地寻找专用的内存管理类?
我将不得不将数据传递给C接口,为什么我更喜欢基于可访问(只读)线性数组的存储方法.
我正在制作一个抽象Font类,我想强制子类声明一个带有单个int参数(字体大小)的构造函数.我尝试这样做如下.
public abstract class Font {
...
public abstract Font(int size);
...
}
Run Code Online (Sandbox Code Playgroud)
但我的编译器声明:
Error:(20, 19) java: <path omitted>/Font.java:20: modifier abstract not allowed here
Run Code Online (Sandbox Code Playgroud)
这不是世界末日 - 这不是绝对必要的,我只是希望Java编译器强迫我记住实现该构造函数.我只是想知道为什么不允许这样做?
在尝试编写Conway的《人生游戏》时,我正在与借阅检查器进行搏斗。我有两个for循环,对进行可变借用self.cell_states,这是一个Vec<Vec<CellState>>(CellState是一个枚举),以便可以更新每个单元格的Alive或Dead状态。
为了确定一个细胞是应该存活还是死亡,它需要查看周围有多少个细胞存活。这是我遇到问题的地方。我可以检查单元格是否存在的唯一方法是执行一条match语句,但是显然match借用了该值,这是不允许的,因为我已经进行了可变借用。在我看来,我只能复制该值并对照复制的值进行检查,因此我尝试了match self.cell_states[i+x-1][j+y-1].clone() {...},但无济于事。不借钱怎么办比赛?
error[E0502]: cannot borrow `cell_states` as immutable because it is also borrowed as mutable
--> src/main.rs:18:27
|
11 | for (i, row) in cell_states.iter_mut().enumerate() {
| ----------------------------------
| |
| mutable borrow occurs here
| mutable borrow used here, in later iteration of loop
...
18 | match cell_states[i+x-1][j+y-1] {
| ^^^^^^^^^^^ immutable borrow occurs here
Run Code Online (Sandbox Code Playgroud)
我的代码如下(Playground):
error[E0502]: cannot …Run Code Online (Sandbox Code Playgroud) 我想*在 zsh 中打印重复的字符。这个答案有一个解决方案,适用于bash:
printf '*%.0s' {1..50}
Run Code Online (Sandbox Code Playgroud)
但是,当我在 中运行它时zsh,我得到以下输出:
**************************************************%
Run Code Online (Sandbox Code Playgroud)
尾随%标志颜色反转的地方。这对我来说很神秘,我想知道为什么会发生这种情况,我该如何避免?
文档Split说它实现了DoubleEndedIterator应该有一个next_back()获取最后一个元素的方法。
但是当我这样做时:
fn get_file_ext(file_name: &str) -> Option<String> {
if let Some(ext) = file_name.split(".").next_back() {
return Some(ext.to_owned());
}
None
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
error[E0599]: no method named `next_back` found for struct `std::str::Split<'_, &str>` in the current scope
--> src/lib.rs:2:45
|
2 | if let Some(ext) = file_name.split(".").next_back() {
| ^^^^^^^^^ method not found in `std::str::Split<'_, &str>`
|
= note: the method `next_back` exists but the following trait bounds were not satisfied:
`std::str::pattern::StrSearcher<'_, '_>: std::str::pattern::DoubleEndedSearcher<'_>`
which is required …Run Code Online (Sandbox Code Playgroud) 是否有格式说明符会截断类似字符串的类型?我有一个不是字符串的类型,但确实实现了显示。不过,我想将其限制为 7 个字符长,而我知道如何做到这一点的唯一方法是先将其转换为字符串。
let sha1 = format!("{}", branch.commit_id);
let formatted = format!("{} ({})\n", branch.name, &sha1[0..7]));
Run Code Online (Sandbox Code Playgroud)
是否有一些格式说明符可以让我一步完成?就像是:
let formatted = format!("{} ({<something 7>})\n", branch.name, branch.commit_id);
Run Code Online (Sandbox Code Playgroud)
不幸的是,因为它不是字符串,所以我不能只做&branch.commit_id[0..7],这就是为什么我希望有一个格式说明符语法。