[VC] [STL source code] comparison and entry into the usage of vector, deque and sort

Source: Internet
Author: User
# Include <stdio. h>
// Sort Definition
# Include <algorithm>
// Vector Definition
# Include <vector>
// Deque Definition
# Include <deque>
// Similarly, introduce the STD namespace
Using namespace STD;
// Save with an Array
Int friends [1000];
// Save with Vector
Vector <int> vtfriends;
// Save with deque
Deque <int> dqfriends;

Int main ()
...{
Int T;
Scanf ("% d", & T );
Int icase;
For (icase = 1; icase <= T; icase ++)
...{
// Enter the vector and deque fields each time.
Vtfriends. Clear ();
Dqfriends. Clear ();
Printf ("Scenario # % d:", icase );
Int N, F, TN;
Scanf ("% d", & N, & F );
// Because we need to find three times later, we need to save the N value first.
Tn = N;
Int I;
For (I = 0; I <F; I ++)
...{
// Save it to an array
Scanf ("% d", & Friends [I]);
// Save to Vector
Vtfriends. push_back (Friends [I]);
// Save it to deque
Dqfriends. push_back (Friends [I]);
}
// Sort. If the third parameter is not specified, the default value is in ascending order.
// Sort implements fast sorting, Which is unstable sorting,
// Stable_sort is used for stable sorting. The usage is exactly the same as that of sort.
// Sort the Array
Sort (friends, friends + F );
// Sort the Vector
Sort (vtfriends. Begin (), vtfriends. End ());
// Sort deque
Sort (dqfriends. Begin (), dqfriends. End ());
// Search in the array
For (I = f-1; I> = 0; I --)
...{
N-= friends [I];
If (n <= 0)
Break;
}
// Search in Vector
// Restore n first
N = tn;
For (I = f-1; I> = 0; I --)
...{
N-= vtfriends. at (I );
If (n <= 0)
Break;
}
// Search in deque
// Restore n first
N = tn;
For (I = f-1; I> = 0; I --)
...{
// Deque's at method is less efficient than that of vector,
// Because deque elements are not completely continuous in the memory
N-= dqfriends. at (I );
If (n <= 0)
Break;
}
If (I <0)
Puts ("impossible ");
Else
Printf ("% d", f-I );
Puts ("");
}
Return 0;
}

I used a school written by a cool, here we introduce the usage of vector, deque and sort, these are STL content, give you a website, http://www.stlchina.org /;
However, it seems that we do not advocate the use of STL in the program from the very beginning. Those algorithms are the best to write on their own and will be further understood when they are learned!

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.