小编use*_*317的帖子

为什么模式匹配中不允许[x,y,_]

这是我的代码:

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, _]

全局含义是_什么?

haskell function clause pattern-matching compiler-warnings

4
推荐指数
2
解决办法
173
查看次数

cbegin 函数的返回值

我有这样的代码

#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 是如何工作的,它的返回值是什么?

c++ iterator

1
推荐指数
1
解决办法
43
查看次数