C # STL (Part 1) Compared with C ++)

Source: Internet
Author: User

Algorithms are a huge topic. STL contains more than 100 algorithms. It is very painful to remember the algorithm name. So here I will narrow down the scope: I will mainly discuss the traversal, sorting, and searching algorithms in STL algorithms. Common Traversal Algorithms include for_each, transform, and copy. For_each accepts an operation. If the parameter of this operation is passed using the reference method, it can change the elements it traverses, but not vice versa. Transform is a variable algorithm that uses an operation to return the modified parameters. It is more flexible than for_each and can operate on the elements of the three containers at the same time. The copy algorithm traverses the given range and copies the elements in the range to the element space pointed by the iterator of another specified container, the size of the target container must be greater than or equal to the size of the source container. Below is a general Writing of these three algorithms: # include <iostream> # include <vector> # include <list> # include <map> # include <set> # include <algorithm> # include <iterator> # include <string> using namespace std; template <typename Container> void PrintElements (Container c) {typedef typename iterator_traits <Container: iterator >:: value_type; // extracts its typedef Container: value_type; // obtain the cout <endl; ostream_iterator <type> OS (cout, "\ T"); copy (c. begin (), c. end (), OS) ;}template <typename T> void PrintElement (T value) {cout <value <"\ t ";} template <typename T> T DoubleParameter (T value) {return 2 * value;} int main () {vector <int> vecSource; list <int> lstDest; for (int I = 0; I <10; ++ I) {vecSource. push_back (I);} for_each (vecSource. begin (), vecSource. end (), PrintElement <int>); // make sure that the lstDest has enough space. resize (vecSource. s Ize (); transform (vecSource. begin (), vecSource. end (), lstDest. begin (), DoubleParameter <int>); PrintElements (lstDest);} the Sorting Algorithm is represented by sort, and sort is sorted by operator by default. If you want to specify the sorting rule, you must customize a sorting method. The sample code is as follows: # include <iostream> # include <vector> # include <list> # include <map> # include <set> # include <algorithm> # include <iterator> # include <string> # include <functional> # include <ctime> using namespace std; template <typename Container> void PrintElements (Container c) {typedef Container: value_type; // obtain the cout type <endl; ostream_iterator <type> OS (cout, "\ t"); copy (c. begin (), c. end (), OS);} int main (){ Srand (unsigned) time (NULL); vector <int> vecSource; for (int I = 0; I <50; ++ I) {vecSource. push_back (rand () % 10000);} cout <"Before sorting:"; PrintElements (vecSource); sort (vecSource. begin (), vecSource. end (); cout <endl <"after default sorting:"; PrintElements (vecSource); // here, we do not use the default less <int> sorting, change to greater <int> sort (vecSource. begin (), vecSource. end (), greater <int> (); cout <endl <"after reverse sorting:"; PrintE The find algorithm is used as an example. We know that there are different search strategies for different element emission sequences. For example, for a list of sorted elements, binary lookup is the fastest. If the list is not sorted, you can only traverse the list one by one. In STL, some containers are sorted internally, such as map and set. If you want to find an element, you 'd better first check whether the search algorithm is provided for the container. If not, use the common find algorithm. The sample code is as follows: # include <iostream> # include <vector> # include <list> # include <map> # include <set> # include <algorithm> # include <iterator> # include <string> # include <functional> # include <ctime> using namespace std; int main () {srand (unsigned) time (NULL); vector <int> vecSource; set <int> st; int element = 0; for (int I = 0; I <500; ++ I) {element = rand () % 1000; vecSource. push_back (element); st. insert (element) ;}Vector <int >:: const_iterator iterVec = find (vecSource. begin (), vecSource. end (), 111); if (iterVec! = VecSource. end () {cout <"found" <* iterVec <endl;} else {cout <"not found in vector" <endl ;} set <int >:: const_iterator iterSt = st. find (222); if (iterSt! = St. end () {cout <"found" <* iterSt <endl;} else {cout <"not found in set" <endl ;}}

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.