none
一个STL问题 RRS feed

  • 问题

  • #include<list>
    #include<iostream>
    #include <algorithm>
    #include <functional> 

    #if _MSC_VER > 1020
    using namespace std;
    #endif

    int main()
    {
     list<int> c1,c2,c3;
     list<int>::iterator c1_Iter,c2_Iter,c3_Iter;
     c1.push_back(3);
     c1.push_back(6);
     c2.push_back(2);
     c2.push_back(4);
     c3.push_back(5);
     c3.push_back(1);
     
     cout<<"c1 = ";
     for(c1_Iter = c1.begin();c1_Iter != c1.end();c1_Iter++)
      cout<<" "<<*c1_Iter;
     cout<<endl;
     cout<<"c2 = ";
     for(c2_Iter = c2.begin();c2_Iter != c2.end();c2_Iter++)
      cout<<" "<<*c2_Iter;
     cout<<endl;
      c2.merge(c1);
      cout<<"After merging c1 with c2 and sorting with>: c2 = ";
     for(c2_Iter = c2.begin();c2_Iter != c2.end();c2_Iter++)
      cout<<" "<<*c2_Iter;
     cout<<endl;
     cout<<"c3 = ";
     for(c3_Iter = c3.begin();c3_Iter != c3.end();c3_Iter++)
      cout<<" "<<*c3_Iter;
     cout<<endl;
     c2.merge(c3,greater<int>());

     cout<<"After merging c3 with c2 according to the '>' comparison relation: c2 = ";
     for(c2_Iter = c2.begin();c2_Iter != c2.end();c2_Iter++)
      cout<<" "<<*c2_Iter;
     cout<<endl;
     
     ::getchar();
     return 0;
    }

     

    上面得代码在Debug下 调试 报了一个assert的错误,在release下调试通过。不知道该怎么改,或者需不需要该

    2010年3月22日 8:20

答案

  • 你抄MSDN 的例子没有抄对。 在c2.merge(c1); 一行后面需要加入   c2.sort( greater<int>( ) );

     

    因为当前你的c2 经过merge以后是乱序的,因此只有在排序之后才能继续order。

    2010年3月22日 9:05
    版主