"C + + Academy" 0830-LAN/STL expression algorithm-operational data

Source: Internet
Author: User

Blue non-reach expression
#include <iostream> #include <vector> #include <algorithm>//algorithm lambda expression, not just for arrays, Also applicable to Vectorvoid main1 () {std::vector<int> myvector;myvector.push_back (one); Myvector.push_back (22); Myvector.push_back, Myvector.push_back (3); Myvector.push_back (4); Myvector.push_back (5); int res=0;//result//& Res directly operates on a variable, res is equivalent to a return value, X represents a parameter, each time it acts as an iterator pointing to an element, the curly brace is code Std::for_each (Myvector.begin (), Myvector.end (), [&res] (int x) {res + = x;}); Std::cout << res;std::cin.get ();} void Main () {std::vector<int> myvector (5);//Allocate 5 spaces, default initialized to 0myvector.push_back (1);//Increase myvector.push_back (11); Myvector.push_back (111); Myvector.push_back (1111); Myvector.push_back (2); Myvector.pop_back ();//Popup an element, Delete the last Myvector.insert (Myvector.begin () +1, 999);//INSERT, Myvector.erase (Myvector.begin () +5);//depending on the position of the iterator// Myvector.clear ();//delete all elements for (int i = 0; i < myvector.size (); i++) {if (1) {//query, modify}std::cout << myvector.at (i) &lt ;< Std::endl;} System ("Pause");} void main123123 () {//can implement dynamic irregular array management std::vector<int>Myvetor1;myvetor1.push_back (Myvetor1.push_back); Myvetor1.push_back (+);std::vector<int> Myvetor2; Myvetor2.push_back (;std::vector<int>) myvetor3;myvetor3.push_back (+); Myvetor3.push_back (PNS); std:: Vector<std::vector<int>> Allvecor;allvecor.push_back (Myvetor1); Allvecor.push_back (MYVETOR2); Allvecor.push_back (Myvetor3); for (int i = 0; i < allvecor.size (), i++) {for (int j = 0; J < Allvecor[i].size (), j + +) { Std::cout << "<< allvecor[i][j";} Std::cout << "\ n";} Std::cin.get ();}

STL algorithm - Operating data

Vector

#include <iostream> #include <vector> #include <algorithm> #include <list> #include <set> #include <string>using namespace Std;void main1 () {vector<int> myv;myv.push_back (1); Myv.push_back (2); Myv.push_back (3); Myv.push_back (one); Myv.push_back (+); myv.push_back; For_each (Myv.begin (), Myv.end (), [] (int v) {cout << v << Endl;}); Auto i = Find (Myv.begin (), Myv.end (), +), if (I==myv.end ()) {std::cout << "23 play Missing";} else{std::cout <<*i;} Cin.get ();} void Main2 () {vector<int> myv;myv.push_back (1); Myv.push_back (2); Myv.push_back (3); Myv.push_back (one); Myv.push _back (Myv.push_back); For_each (Myv.begin (), Myv.end (), [] (int v) {cout << v << Endl;}); /auto i = find_if (Myv.begin (), Myv.end (), [] (int v)->bool{return (V > 4);}); Auto i = Find_if_not (Myv.begin (), Myv.end (), [] (int v)->bool{return (V > 4);}); if (i = = Myv.end ()) {std::cout << "play Missing";} Else{std::cout << *i;} Cin.get ();}

List

Template <class T>class show{public:void  operator () (t &t) {cout << T << "  ";}}; void Main3 () {list <int >  list1;vector <int > V1;list1.push_back (121); list1.push_back; list1.push_ Back (122); List1.push_back (V1.push_back); V1.push_back (121); V1.push_back (n); 122 (v1.push_back);//sort; ( List1.begin (), List1.end ());//Sort//sort (V1.begin (), V1.end ());//Sort, simple sort//algorithm relies on data structure (chained, linear), different data structures, algorithm not the same    fill (V1.begin () + 3, V1.end (), 3);//fill, specify location data for initialization for_each (List1.begin (), List1.end (), show<int> ()), cout << "\ n "; For_each (V1.begin (), V1.end (), show<int> ()); Cin.get ();

Multiset

void Main4 () {multiset<int > Myset;myset.insert (3); Myset.insert (1); Myset.insert (2); Myset.insert (1); Myset.insert (2); Myset.insert (1); Myset.insert (2); Myset.insert (1); int i = 0;for (Auto IB = Myset.begin (), ie = Myset.end () ; Ib! = ie;ib++,i++) {}cout << i << endl;int num = count (Myset.begin (), Myset.end (), 1);//statistics 1 Number of nodes cout << num<< endl;cin.get ();} void Main5 () {multiset<int > Myset;myset.insert (3); Myset.insert (1); Myset.insert (2); Myset.insert (1); Myset.insert (0); Myset.insert (1); Myset.insert (2); Myset.insert (1); For_each (Myset.begin (), Myset.end (), show<    Const int> ()); Auto It=adjacent_find (Myset.begin (), Myset.end ()) cout << "\ n" << *it << endl;it++;cout << "\ n" &  lt;< *it << endl;it++;cout << "\ n" << *it << endl;it++;cout << "\ n" << *it << Endl;it = Adjacent_find (it, myset.end ());//Find the same data, you can determine the location cout << "\ n" << *it << endl;it++;cout <& Lt "\ n" << *it &Lt;< endl;cin.get ();} 

Messing up the data, playing cards shuffle

Template <class T>class show{public:void  operator () (t &t) {cout << T << "  ";}};/ /scrambled Data void  main () {vector<int> v1;for (int i = 0; i < 10;i++) {v1.push_back (i);} For_each (V1.begin (), V1.end (), show<int> ()), cout << "\ n"; Random_shuffle (V1.begin (), V1.end ()); For_each ( V1.begin (), V1.end (), show<int> ()) cout << "\ n"; Random_shuffle (V1.begin (), V1.end ()); For_each (V1.begin ( ), V1.end (), show<int> ()), cout << "\ n"; Random_shuffle (V1.begin (), V1.end ()), For_each (V1.begin (), V1.end ( ), show<int> ()); Cin.get ();}

Rotate data

BOOL isOK (int num) {return (num = = 8);} Rotation data void  main7 () {vector<int> v1;for (int i =10; i>0; i--) {v1.push_back (i);} For_each (V1.begin (), V1.end (), show<int> ()),//partition (V1.begin (), V1.end (), isok);//division//rotation, head, tail, for fast sorting method Intermediate rotate (V1.begin (), V1.begin () +8, V1.end ()),//v1.begin (), V1.begin () +8 data moved to the back cout << "\ n"; For_each ( V1.begin (), V1.end (), show<int> ()),/*rotate (V1.begin (), V1.begin () +1, V1.end ()), cout << "\ n"; For_each ( V1.begin (), V1.end (), show<int> ()), Rotate (V1.begin (), V1.begin () + 1, v1.end ()), cout << "\ n"; For_each ( V1.begin (), V1.end (), show<int> ()), Rotate (V1.begin (), V1.begin () + 1, v1.end ()), cout << "\ n"; For_each ( V1.begin (), V1.end (), show<int> ()); */cin.get ();

Sort and show steps

void  Main8 () {int a[4] = {2, 1, 3, ten};d o {cout << a[0] << "<< a[1] <<" "<< a[2] &l t;<a[3]<< "\ n";} while (Prev_permutation (a,a+4));//Sort and show step Cin.get ();}

Local sort

void  Main10 () {vector<char>one;one.push_back (' B '); One.push_back (' A '); One.push_back (' C '); one.push_back (' Y '); One.push_back (' Z '); One.push_back (' X '); For_each (One.begin (), One.end (), show<char> ()); cout << "\ n "; For_each (One.begin (), One.end (), show<char> ()); Cin.get ();}

Remove 2 students with the lowest score

#include <iostream> #include <vector> #include <algorithm> #include <list> #include <set> #include <string>using namespace Std;class student{public:string name;int score;public:student (string str, int num): Name (str), score (num) {}bool operator < (const student &S1) Const{return This->score < s1.score;}};/ /Go to score the worst 2 people void  Main () {vector<student> ss;{ Student S1 ("Silver City A", 106); Ss.push_back (S1);} {Student S1 ("Silver City B", 101); Ss.push_back (S1);} {Student S1 ("Silver City C", 103); Ss.push_back (S1);} {Student S1 ("Silver City D"), Ss.push_back (S1);} Partial_sort (Ss.begin (), Ss.begin () + 2,ss.end ());//partial sort for (int i = 0; i < 4;i++) {std::cout << ss[i].name <&L T Ss[i].score << "\ n";} Cin.get ();}








"C + + Academy" 0830-LAN/STL expression algorithm-operational data

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.