C++20提供了range來簡化對迭代器的使用,可以認(rèn)為range是一個封裝了begin和end的對象。
template<?class?T?>
concept range?=?requires(?T&?t?)?{
??ranges::begin(t);?// equality-preserving for forward iterators
??ranges::end??(t);};文章來源:http://www.zghlxwxcb.cn/news/detail-542103.html
而對于end,range將它認(rèn)為是迭代器哨位,并不要求end一定是一個跟begin相同類型的迭代器,只要可以通過end哨位進(jìn)行比較就可以:文章來源地址http://www.zghlxwxcb.cn/news/detail-542103.html
#include <algorithm>
#include <iostream>
using namespace std;
struct SpaceIsEnd {
bool operator==(auto pos) const {
return *pos == ' ';
}
};
class CommasIsEnd {
public:
bool operator==(auto pos) const {
return *pos == ',';
}
};
int main()
{
const char *str =
到了這里,關(guān)于C++(20):range的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!