将指针转换为 STL 中的反向向量迭代器

发布于 2024-08-06 17:47:52 字数 78 浏览 2 评论 0原文

我有

sort(arr, arr+n, pred);

如何按相反顺序排序?

I have

sort(arr, arr+n, pred);

How do I sort in reverse order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

猫九 2024-08-13 17:47:52

似乎也有可能使用反向迭代器......除了使用反向谓词可能更容易,除非类型没有实现 operator> :)

#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
    int arr[4] = { 3, 2, 5, 4 };
    std::sort(std::reverse_iterator<int*>(arr + 4), std::reverse_iterator<int*>(arr));
}

There also seems to be a possibility to use reverse iterators ... except using the reversed predicate might be easier, except perhaps when the type doesn't implement operator> :)

#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
    int arr[4] = { 3, 2, 5, 4 };
    std::sort(std::reverse_iterator<int*>(arr + 4), std::reverse_iterator<int*>(arr));
}
独闯女儿国 2024-08-13 17:47:52

如果你得到了 pred (即你无法进入其中来反转顺序),类似于:

std::sort(arr, arr+n, boost:bind<bool>(pred, _2, _1));

If you're given pred (i.e. you can't get inside it to reverse the order), something like:

std::sort(arr, arr+n, boost:bind<bool>(pred, _2, _1));
南城追梦 2024-08-13 17:47:52

您可以使用标准库中的greater,它会自动为您要排序的类型调用operator>

#include <funcitonal>
.....
sort(arr, arr+n, greater<Type>()); // Type could be double for example

You could use greater from the standard library which calls operator> automatically for the type you want to sort.

#include <funcitonal>
.....
sort(arr, arr+n, greater<Type>()); // Type could be double for example
半城柳色半声笛 2024-08-13 17:47:52

pred的返回值求反。

Negate the return value of pred.

甜嗑 2024-08-13 17:47:52

正如阿尔拉迪所说,你应该提供一个反向谓词。如果由于某些原因(例如纯粹的懒惰)而不能,您总是可以先排序然后反转:

sort(arr, arr+n, pred);
reverse( arr, arr+n );

这对计算机来说会做更多的工作,但它很清楚并且可以完成工作。如果您需要这种排序的速度性能,请使用反向谓词解决方案。

As alrady said, you should provde a reversed predicate. If you can't for some reasons (like pure laziness), you can always first sort then reverse :

sort(arr, arr+n, pred);
reverse( arr, arr+n );

That would be more work for the computer but it's clear and does the job. If you need speed performance for this sort, use the reversed predicate solution.

爱已欠费 2024-08-13 17:47:52

我看起来很简单

std::sort(myVec.rbegin(),myVec.rend());


int main() 
{
    typedef std::vector<int> vecType;
    vecType myVec;
    for(int a=0;a<20;a++)
    {
        myVec.push_back((rand()%100));
    }
    std::copy(myVec.begin(), myVec.end(), std::ostream_iterator<int>(std::cout, "\n"));
    cout<<"\n---------------------------------------------------\n";
    std::sort(myVec.rbegin(),myVec.rend());
    std::copy(myVec.begin(), myVec.end(), std::ostream_iterator<int>(std::cout, "\n"));
    return 0;   
}

Quite Easy i seems

std::sort(myVec.rbegin(),myVec.rend());


int main() 
{
    typedef std::vector<int> vecType;
    vecType myVec;
    for(int a=0;a<20;a++)
    {
        myVec.push_back((rand()%100));
    }
    std::copy(myVec.begin(), myVec.end(), std::ostream_iterator<int>(std::cout, "\n"));
    cout<<"\n---------------------------------------------------\n";
    std::sort(myVec.rbegin(),myVec.rend());
    std::copy(myVec.begin(), myVec.end(), std::ostream_iterator<int>(std::cout, "\n"));
    return 0;   
}
云醉月微眠 2024-08-13 17:47:52
sort(arr, arr+n, std::not1(pred));

请参阅:http://www.cplusplus.com/reference/std/function/not1 /

sort(arr, arr+n, std::not1(pred));

See: http://www.cplusplus.com/reference/std/functional/not1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文