Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 迭代器和反向迭代器的区别_C++_Algorithm_Stl_Comparison_Stable Sort - Fatal编程技术网

C++ 迭代器和反向迭代器的区别

C++ 迭代器和反向迭代器的区别,c++,algorithm,stl,comparison,stable-sort,C++,Algorithm,Stl,Comparison,Stable Sort,以下两段代码之间的区别是什么 vector<int> a; // initialization code sort( a.rbegin(), a.rend() ); 为了举例说明,下面的代码给出了SPOJ问题,而这段代码给出了SPOJ问题。 和之间唯一的区别是使用了sort()的版本。顾名思义,反向迭代器以相反的顺序访问集合。如果要求STLsort()。。。范围从a.begin()到a.end(),顺序由这些迭代器定义 那么,如果您要求它对从a.rbegin()到a.rend()的

以下两段代码之间的区别是什么

vector<int> a;
// initialization code
sort( a.rbegin(), a.rend() );
为了举例说明,下面的代码给出了SPOJ问题,而这段代码给出了SPOJ问题。
和之间唯一的区别是使用了sort()的版本。

顾名思义,反向迭代器以相反的顺序访问集合。如果要求STL
sort()。。。范围从
a.begin()
a.end()
,顺序由这些迭代器定义


那么,如果您要求它对从
a.rbegin()
a.rend()
的范围进行排序,会发生什么情况呢?它将结果放入。。。从
a.rbegin()
a.rend()
的范围,按照这些迭代器定义的顺序。

rbegin
为您提供一个迭代器,它指向列表的末尾,并在您要求它向前移动时向后移动。类似地,
rend
为列表开头提供了一个迭代器

sort(a.begin(), a.end(), comp);

这里的第三个参数用于定义您自己的排序顺序。如果不指定一个,则将使用该对象的默认值。

它们都完成相同的任务。在第一个版本中,您颠倒迭代器的顺序以获得从高到低的排序。在第二个版本中,您颠倒了比较的意义,也得到了从高到低的排序。

迭代器从第一个到最后一个运行,反向迭代器从最后一个到第一个运行。因此,
sort(a.begin(),a.end())
将元素按顺序放在[first,last]范围内;
sort(a.rbegin(),a.rend())
将元素按顺序放在[last,first]范围内,产生与第一个版本相反的顺序。

反向迭代器简单迭代器以正常迭代器相反的方向进行迭代

因此,这两个代码段都将按升序对[first,last]范围内的所有内容进行排序。 区别在于第一个函数使用<运算符进行比较,第二个函数使用给定函数

详细地说,第一种方法实际上是按非升序排序,但由于您也会反转比较,因此它会再次反转,从而抵消效果

注意:相互比较相等的元素不保证保持其原始相对顺序

有用的参考资料:, ,
两个函数调用不会给出相同的答案,因为is不是一个稳定的算法,也就是说,它不会在它们的相对顺序中保持相同的元素。下面的示例中,
std::pair
的元素按其第一个元素排序。使用相反的com型坯函数不会产生完全相同的序列。对型坯函数进行同样的操作会产生完全相同的结果

#include <algorithm>
#include <iostream>
#include <ios>
#include <vector>

int main()
{
    typedef std::pair<int, int> Element;
    std::vector<Element> v;

    v.push_back( Element(1,1) );
    v.push_back( Element(-1,1) );
    v.push_back( Element(1,2) );
    v.push_back( Element(-1,2) );
    v.push_back( Element(1,3) );
    v.push_back( Element(-1,3) );
    v.push_back( Element(1,4) );
    v.push_back( Element(-1,4) );
    v.push_back( Element(1,5) );
    v.push_back( Element(-1,5) );
    v.push_back( Element(1,6) );
    v.push_back( Element(-1,6) );
    v.push_back( Element(1,16) );
    v.push_back( Element(-1,16) );
    v.push_back( Element(1,22) );
    v.push_back( Element(-1,22) );
    v.push_back( Element(1,33) );
    v.push_back( Element(-1,33) );
    v.push_back( Element(1,44) );
    v.push_back( Element(-1,44) );
    v.push_back( Element(1,55) );
    v.push_back( Element(-1,55) );
    v.push_back( Element(1,66) );
    v.push_back( Element(-1,66) );

    for (auto it = v.begin(); it != v.end(); ++it) {
        std::cout << "(" << it->first << "," << it->second << ")" << " ";
    }
    std::cout << "\n";

    auto w1 = v;
    std::sort(w1.begin(), w1.end(), [](Element const& e1, Element const& e2){ 
       return e1.first < e2. first;
    });
    auto w2 = v;
    std::sort(w2.rbegin(), w2.rend(), [](Element const& e1, Element const& e2) {
       return e1.first > e2.first;
    });
    std::cout << std::boolalpha << std::equal(w1.begin(), w1.end(), w2.begin()) << "\n";

    auto w3 = v;
    std::stable_sort(w3.begin(), w3.end(), [](Element const& e1, Element const& e2){ 
       return e1.first < e2. first;
    });
    auto w4 = v;
    std::stable_sort(w4.rbegin(), w4.rend(), [](Element const& e1, Element const& e2) {
       return e1.first > e2.first;
    });
    std::cout << std::boolalpha << std::equal(w3.begin(), w3.end(), w4.begin()) << "\n";

}

#include

您是否尝试直接查看排序的输出?听起来像是家庭作业。您尝试了什么?@Philipp当我尝试两个排序版本时,给出了相同的输出,但我不明白为什么一个版本提供AC,而另一个版本提供WASPOJ@moderators为什么反向迭代器的第一个元素上没有排序对的标记不会给出完全相同的结果,因为你需要
stable\u sort
@rhalbersma,因为问题写出来时并不重要,因为完全相同的
int
s的顺序是不相关的。我承认我没有链接到示例,它们显然更复杂。
sort(a.begin(), a.end(), comp);
#include <algorithm>
#include <iostream>
#include <ios>
#include <vector>

int main()
{
    typedef std::pair<int, int> Element;
    std::vector<Element> v;

    v.push_back( Element(1,1) );
    v.push_back( Element(-1,1) );
    v.push_back( Element(1,2) );
    v.push_back( Element(-1,2) );
    v.push_back( Element(1,3) );
    v.push_back( Element(-1,3) );
    v.push_back( Element(1,4) );
    v.push_back( Element(-1,4) );
    v.push_back( Element(1,5) );
    v.push_back( Element(-1,5) );
    v.push_back( Element(1,6) );
    v.push_back( Element(-1,6) );
    v.push_back( Element(1,16) );
    v.push_back( Element(-1,16) );
    v.push_back( Element(1,22) );
    v.push_back( Element(-1,22) );
    v.push_back( Element(1,33) );
    v.push_back( Element(-1,33) );
    v.push_back( Element(1,44) );
    v.push_back( Element(-1,44) );
    v.push_back( Element(1,55) );
    v.push_back( Element(-1,55) );
    v.push_back( Element(1,66) );
    v.push_back( Element(-1,66) );

    for (auto it = v.begin(); it != v.end(); ++it) {
        std::cout << "(" << it->first << "," << it->second << ")" << " ";
    }
    std::cout << "\n";

    auto w1 = v;
    std::sort(w1.begin(), w1.end(), [](Element const& e1, Element const& e2){ 
       return e1.first < e2. first;
    });
    auto w2 = v;
    std::sort(w2.rbegin(), w2.rend(), [](Element const& e1, Element const& e2) {
       return e1.first > e2.first;
    });
    std::cout << std::boolalpha << std::equal(w1.begin(), w1.end(), w2.begin()) << "\n";

    auto w3 = v;
    std::stable_sort(w3.begin(), w3.end(), [](Element const& e1, Element const& e2){ 
       return e1.first < e2. first;
    });
    auto w4 = v;
    std::stable_sort(w4.rbegin(), w4.rend(), [](Element const& e1, Element const& e2) {
       return e1.first > e2.first;
    });
    std::cout << std::boolalpha << std::equal(w3.begin(), w3.end(), w4.begin()) << "\n";

}