C++ 迭代器与ptrdiff的比较
查看下面的代码C++ 迭代器与ptrdiff的比较,c++,algorithm,sorting,templates,bubble-sort,C++,Algorithm,Sorting,Templates,Bubble Sort,查看下面的代码 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
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
}
}
}
我不允许将迭代器与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;
}
}
模板
constexpr自动冒泡排序(Itr优先,Itr最后)
{
for(;first!=std::prev(last);std::advance(first,1))
{
自动交换{false};
for(autoj{first};j!=(std::prev(last)-first;std::advance(j,1))
{
if(*j>*std::next(j))//Pred
{
标准:国际热核聚变实验堆交换(j,标准:下一步(j));
交换=真;
}
}
如果(!交换)中断;
}
}
如果您想为前向迭代器编写方法bubble\u sort,那么它可以按照下面的演示程序所示的方式进行。不需要ptrdiff\u 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;
}
在
j!=(标准::上一个(最后一个)-第一个)
?你没有真正解释你想做什么,因为你已经更新了问题,如果你需要更多信息,请再次评论。“更小的子集”哪个更小的子集?你再次没有准确解释你想要实现什么。你的foo
函数应该做什么?我又更新了一次这个问题。你可能想从迭代中分离出排序的
优化
#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