1. 程式人生 > >std::list 一些函式簡介(merge)

std::list 一些函式簡介(merge)

// list::merge
//如果合併的 兩個連結串列是有序的那麼合併之後也是有序的
//強調特別在vs10下合併的連結串列必須是有序的
#include <iostream>
#include <list>
// compare only integral part:
bool mycomparison (double first, double second)
{ return ( int(first)<int(second) ); }


int main ()
{
std::list<double> first, second;


first.push_back (3.1);
first.push_back (2.2);
first.push_back (2.9);


second.push_back (3.7);
second.push_back (7.1);
second.push_back (1.4);


 first.sort();
 second.sort();


first.merge(second);

// (second is now empty)
second.push_back(1.3);
first.merge(second);
std::cout << "first contains:";
for (std::list<double>::iterator it=first.begin(); it!=first.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';


system("pause");
return 0;
}