gpt4 book ai didi

c++ - 基于空终止字符串的for循环范围

转载 作者:IT老高 更新时间:2023-10-28 22:35:25 25 4
gpt4 key购买 nike

我有点假设基于范围的 for 循环将支持 C 风格的字符串

void print_C_str(const char* str)
{
for(char c : str)
{
cout << c;
}
}

但事实并非如此,标准 [stmt.ranged] (6.5.4) 表示基于范围的 for 可以在以下三种可能性之一中使用:

  1. 范围是一个数组
  2. 范围是一个具有可调用 beginend 方法的类
  3. 在关联的命名空间(加上 std 命名空间)中有可访问的 ADL

当我在全局命名空间中为 const char* 添加 beginend 函数时,我仍然遇到错误(来自 VS12 和 GCC 4.7 )。

有没有办法让基于范围的 for 循环与 C 风格的字符串一起工作?

我尝试将重载添加到 namespace std 并且这有效,但据我了解,将重载添加到 namespace std 是非法的(这是正确的吗?)

最佳答案

如果您为空终止字符串编写一个简单的迭代器,您可以通过在返回特殊范围的指针上调用函数来做到这一点,而不是将指针本身视为范围。

template <typename Char>
struct null_terminated_range_iterator {
public:
// make an end iterator
null_terminated_range_iterator() : ptr(nullptr) {}
// make a non-end iterator (well, unless you pass nullptr ;)
null_terminated_range_iterator(Char* ptr) : ptr(ptr) {}

// blah blah trivial iterator stuff that delegates to the ptr

bool operator==(null_terminated_range_iterator const& that) const {
// iterators are equal if they point to the same location
return ptr == that.ptr
// or if they are both end iterators
|| is_end() && that.is_end();
}

private:
bool is_end() {
// end iterators can be created by the default ctor
return !ptr
// or by advancing until a null character
|| !*ptr;
}

Char* ptr;
}

template <typename Char>
using null_terminated_range = boost::iterator_range<null_terminated_range_iterator<Char>>;
// ... or any other class that aggregates two iterators
// to provide them as begin() and end()

// turn a pointer into a null-terminated range
template <typename Char>
null_terminated_range<Char> null_terminated_string(Char* str) {
return null_terminated_range<Char>(str, {});
}

用法如下:

for(char c : null_terminated_string(str))
{
cout << c;
}

我不认为这会失去任何表现力。其实我觉得这个比较清楚。

关于c++ - 基于空终止字符串的for循环范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14477581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com