【问题标题】:Comparing iterator with ptrdiff将迭代器与 ptrdiff 进行比较
【发布时间】:2020-07-20 20:25:18
【问题描述】:

看下面的代码

template <typename Itr>
constexpr auto foo(Itr first, Itr last)
{
    for (; first != std::prev(last); std::advance(first, 1))
    {
        for (auto j{ first }; j != (std::prev(last) - first); std::advance(j, 1)) // Error here
        {
            // Do stuff
        }
    }
}

在第二个 for 循环中出现错误:

no operator found which takes a right-hand operand of type '__int64' (or there is no acceptable conversion)

我不允许将迭代器与 ptrdiff_t 进行比较。那么我怎样才能完成这个任务呢?我已经尝试对 j 和 ptrdiff_t 使用所有可用的演员表 - 但不允许使用任何演员表。

我需要这个的原因是,我只希望内部循环为外部循环的每次迭代迭代容器的较小子集。

我需要这个的一个例子是在冒泡排序算法的实现中

template <typename Itr>
constexpr auto bubble_sort(Itr first, Itr last)
{
    for (; first != std::prev(last); std::advance(first, 1))
    {
        auto swapped{ false };
        for (auto j{ first }; j != (std::prev(last) - first); std::advance(j, 1))
        {
            if (*j > *std::next(j)) // Pred
            {
                std::iter_swap(j, std::next(j));
                swapped = true;
            }
        }
        if (!swapped) break;
    }
}

【问题讨论】:

  • 您到底想在j != (std::prev(last) - first) 中比较什么?你没有真正解释你想要完成什么
  • 我已经更新了问题,如果您需要更多信息,请再次评论。
  • “更小的子集”哪个更小的子集?
  • 您再次没有准确解释您想要实现的目标。你的foo 函数应该做什么?
  • 我再次更新了问题

标签: c++ algorithm sorting templates bubble-sort


【解决方案1】:

如果你想为前向迭代器编写方法bubble_sort,那么它可以如下面的演示程序所示。 ptrdiff_t 都不是必需的。

你来了。

#include <iostream>
#include <functional>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <ctime>

template <typename ForwardIterator>
void bubble_sort( ForwardIterator first, ForwardIterator last )
{
    for ( auto sorted = last; first != last && std::next( first ) != last; last = sorted  )
    {
        for ( auto next = sorted = first, prev = next++; next != last; ++next, ++prev )
        {
            if ( *next < *prev )
            {
                std::iter_swap( prev, next );
                sorted = next;
            }
        }
    }
}

template <typename ForwardIterator, typename Comparison>
void bubble_sort( ForwardIterator first, ForwardIterator last, Comparison cmp )
{
    for ( auto sorted = last; first != last && std::next( first ) != last; last = sorted  )
    {
        for ( auto next = sorted = first, prev = next++; next != last; ++next, ++prev )
        {
            if ( cmp( *next, *prev ) )
            {
                std::iter_swap( prev, next );
                sorted = next;
            }
        }
    }
}

int main() 
{
    const size_t N = 10;
    int a[N];

    std::srand( ( unsigned int )std::time( nullptr ) );

    for ( auto &item : a ) item = std::rand() % N;

    for ( const auto &item : a ) std::cout << item << ' ';
    std::cout << '\n';

    bubble_sort( std::begin( a ), std::end( a ) );

    for ( const auto &item : a ) std::cout << item << ' ';
    std::cout << '\n';

    bubble_sort( std::begin( a ), std::end( a ), std::greater<>() );

    for ( const auto &item : a ) std::cout << item << ' ';
    std::cout << '\n';

    return 0;
}

程序输出可能看起来像

1 5 4 4 0 9 7 3 3 1 
0 1 1 3 3 4 4 5 7 9 
9 7 5 4 4 3 3 1 1 0 

【讨论】:

  • 可能想从迭代中分离出sorted优化
猜你喜欢
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 2011-01-17
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
相关资源
最近更新 更多