这是我的代码:
tell :: (Show a) => [a] -> String
tell [] = "The list is empty"
tell (x:[]) = "The list has one element: " ++ show x
tell (x:y:[]) = "The list has two elements: " ++ show x ++ " and " ++ show y
tell (x:y:_) = "Other"
Run Code Online (Sandbox Code Playgroud)
在最后一行中,为什么不能更改(x:y:_)为[x, y, _]?
全局含义是_什么?
我有这样的代码
#include <iostream>
#include <string>
int main() {
std::string str{"My short string."};
for (auto& it = str.cbegin(); it != str.cend(); ++it)
std::cout << *it;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:“对非常量引用的初始值必须是左值”
我想使用auto& it = str.cbegin(),为什么我不能这样做?我想, cbegin 返回一个迭代器的对象,我不想复制它,所以我使用 & 语法。
你能解释一下,cbegin 是如何工作的,它的返回值是什么?