Using list container in C + + to realize fast sort __c++

Source: Internet
Author: User
Tags data structures

The list container corresponds to a list of common data structures. The list container supports fast random inserts, deletes, but does not support fast random access elements; This is not the same as the vector container, vector containers do not support fast random inserts, deletes, but support fast with

Machine Access element.

The properties of the list container determine that the insertion order can be implemented without additional storage space, and that the time complexity is O (n^2), the same as the time complexity of the array implementation of the fast row.

File processing in C + + is also convenient, just declare a FStream object, and then the same as the standard device input.

#include <iostream>
#include <fstream>
#include <list>
using namespace std;

Reading data from the file, by inserting the sort, completes the list from small to large stable sort.
int main ()
{
	list<int>mylist;
	Ifstream input;
	Input.open ("In.txt");
	int ele;
	while (Input>>ele)
	{
		if (input.eof ()) break
			;
		if (Input.bad ()) break
			;
		List<int>::iterator Iter=mylist.begin ();
		while (Iter!=mylist.end () && (*iter) <=ele)
	     	++iter;
		  Mylist.insert (Iter,ele);
	}
	Input.close ();

	Ofstream output;
	Output.open ("OUT.txt");
	For (List<int>::iterator Iter=mylist.begin (); Iter!=mylist.end (); ++iter)
	{
		cout<< (*iter) <<endl;
		output<< (*iter) << "   ";
	}
	Output.close ();
	return 0;
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.