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

标签 c++ for-loop c++11 null-terminated

我有点假设基于范围的 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/

相关文章:

javascript - 循环计数器重置为-1

c++ - 重载函数不编译

c++ - 为什么定义静态枚举不会导致链接时间错误?

java - 如何使用 for 每个循环访问另一个类中的 ArrayList

python - 理解for循环的输出

c++ - 我可以使用与静态数组具有相同功能的 vector 吗?

c++ - 使用 lambda 函数签名的函数重载

c++ - 动态分配 std::unique_ptr 有什么用?

c++ - 你如何在 C++ 中模块化地构建应用程序

C++制作二维 bool 矩阵