可以基于范围的循环知道结束

Can range-based for loops be aware of the end?

本文关键字:循环 结束 范围 于范围      更新时间:2023-10-16

给定最小C++11 STL示例:

set<int> S = {1,2,3,4};
for(auto &x: S) {    
   cout << x;
   cout << ",";
}

有没有办法检查x是否就是结尾前的那个?本例的目标是输出1,2,3,4,而不是末尾的最后一个逗号。目前,我使用了一个带有两个迭代器的标准循环,

set<int>::const_iterator itr;
set<int>::const_iterator penultimate_end_itr = --S.end();
for(itr=S.begin(); itr!=penultimate_end_itr;++itr) 
    cout << (*itr) << ',';
cout << (*penultimate_end_itr);

这很有效,但非常麻烦。有没有办法在基于for循环的范围内进行检查?

编辑:问题的重点是而不是打印出逗号分隔的列表。我想知道基于范围的for循环是否知道列表中倒数第二个元素(即,它是否在末尾之前)。给出了一个最小的例子,所以我们都有一个共同的代码块可以讨论。

基于范围的for循环的目的就是忘记迭代器。因此,它们只允许您访问当前值,而不允许您访问迭代器。下面的代码能帮你吗?

set<int> S = {1,2,3,4};
std::string output;
for(auto &x: S) {    
   if (!output.empty())
       output += ",";
    output += to_string(x);
  }
cout << output;

编辑

另一种解决方案:您可以比较值的地址,而不是比较迭代器(就像对循环的"正常"所做的那样):

set<int> S = {1,2,3,4};
auto &last = *(--S.end());
for (auto &x : S)
{
    cout << x;
    if (&x != &last)
        cout << ",";
}

Boost.Range可以在这里提供帮助:

if (std::begin(S) != std::end(S)) {
    std::cout << *std::begin(S);
    for (const auto &x: boost::make_iterator_range(std::next(std::begin(S)), std::end(S))) {
        std::cout << ", " << x;
    }
}

一种更灵活的方法是使用boost::adaptors::indexed(自Boost 1.56以来)对范围进行索引:

for (const auto &element: boost::adaptors::index(S)) {
    std::cout << (element.index() ? ", " : "") << element.value();
}

在1.56之前的Boost版本中,boost::adaptors::indexed不起作用,但你可以很容易地写出类似的作品:

template <typename... T>
auto zip(const T&... ranges) -> boost::iterator_range<boost::zip_iterator<decltype(boost::make_tuple(std::begin(ranges)...))>>
{
    auto zip_begin = boost::make_zip_iterator(boost::make_tuple(std::begin(ranges)...));
    auto zip_end = boost::make_zip_iterator(boost::make_tuple(std::end(ranges)...));
    return boost::make_iterator_range(zip_begin, zip_end);
}
template<typename T>
auto enumerate(const T &range) -> boost::iterator_range<boost::zip_iterator<boost::tuple<
    boost::counting_iterator<decltype(boost::distance(range))>, decltype(std::begin(range))>>>
{
    return zip(boost::make_iterator_range(boost::make_counting_iterator(0),
        boost::make_counting_iterator(boost::distance(range))), range);
}
for (const auto &tup: enumerate(S)) {
    std::cout << (tup.get<0>() ? ", " : "") << tup.get<1>();
}

这是在使用c++11的Sequence-zip函数中的zip函数吗?