C + + STL (2)

Source: Internet
Author: User

Vector:

1#include"stdafx.h"2#include <iostream>3#include <vector>4#include <algorithm>5 using namespacestd;6 intMain ()7 {8vector<int> v = {Ten,98, the, the, the, the, - };9cout <<"array before sorting:";Ten      for(intelem:v) { Onecout << Elem <<" ";//How to Output A     } -cout <<Endl; -Stable_sort (V.begin (), V.end ());//This sort is a stable sort, suitable for the same elements in V, as in the case of 2 66 above , the                                         //This will not change the relative position of the two 66. If there are no duplicate elements, use sort (V.begin (), V.end ()) to -cout <<"sorted Array:"; -      for(intelem:v) { -cout << Elem <<" ";//How to Output +     } -cout <<Endl; +Fill (v.begin () +2, V.end (), Wu);//Fill Acout <<"the array at this time:";  at      for(intelem:v) { -cout << Elem <<" ";//How to Output -     } -cout <<Endl; -Fill_n (V.begin (),3,8);//another way to populate -cout <<"the array at this time:"; in      for(intelem:v) { -cout << Elem <<" ";//How to Output to     } +cout <<Endl; -     return 0; the}

The evolution of the above output pattern is as follows: now with (3) you can

(1)

1 for_each (intin2{3        " " ;   4 }

(2)

1 for_each (V.begin (), V.end (), [] (int2{3        " ) "  4 }

(3)

1  for (int2{3        ""; 4 }

String uppercase to lowercase:

1#include"stdafx.h"2#include <iostream>3#include <vector>4#include <string>5#include <algorithm>6 using namespacestd;7 intMain ()8 {9     stringS1 ="Hello world.";Ten     stringS2; OneTransform (S1.begin (), S1.end (), S2.begin (), toupper);//same capitalization to lowercase is tolower Acout << S1 <<Endl; -}

Two-point search:

1#include"stdafx.h"2#include <iostream>3#include <vector>4#include <string>5#include <algorithm>6 using namespacestd;7 intMain ()8 {9vector<int> v= {Ten,98, the, $, the, the, - };Ten     if(Binary_search (V.begin (), V.end (), the)) Onecout <<"exits!"<<Endl; A     Else -cout <<"didn ' t exits!"<<Endl; -}

Sequence reversal:

1#include"stdafx.h"2#include <iostream>3#include <vector>4#include <string>5#include <algorithm>6 using namespacestd;7 intMain ()8 {9vector<int> v= {Ten,98, the, $, the, the, - };Tencout <<"the sequence before reversal is:"; One      for(intelem:v) A     { -cout << Elem <<" ";  -     } thecout <<Endl; - Reverse (V.begin (), V.end ()); -cout <<"The inverted sequence is:"; -      for(intelem:v) +     { -cout << Elem <<" ";  +     } Acout <<Endl; at     return 0; -}

Conditional partitioning:

1#include"stdafx.h"2#include <iostream>3#include <vector>4#include <string>5#include <algorithm>6 using namespacestd;7 intMain ()8 {9vector<int> v = {1,2,3,4,5,6,7,8,9 };TenStable_partition (V.begin (), V.end (), [] (intelem) One     { A         returnElem%2==0; -     });  -      for(intelem:v) the     { -cout << Elem <<" "; -     } -cout <<Endl; +     return 0; -}

Operation Result:

C + + STL (2)

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.