ITÊýÂë ¹ºÎï ÍøÖ· Í·Ìõ Èí¼þ ÈÕÀú ÔĶÁ ͼÊé¹Ý
TxTС˵ÔĶÁÆ÷
¡ýÓïÒôÔĶÁ,С˵ÏÂÔØ,¹ÅµäÎÄѧ¡ý
ͼƬÅúÁ¿ÏÂÔØÆ÷
¡ýÅúÁ¿ÏÂÔØͼƬ,ÃÀŮͼ¿â¡ý
ͼƬ×Ô¶¯²¥·ÅÆ÷
¡ýͼƬ×Ô¶¯²¥·ÅÆ÷¡ý
Ò»¼üÇå³ýÀ¬»ø
¡ýÇáÇáÒ»µã,Çå³ýϵͳÀ¬»ø¡ý
¿ª·¢: C++֪ʶ¿â Java֪ʶ¿â JavaScript Python PHP֪ʶ¿â È˹¤ÖÇÄÜ Çø¿éÁ´ ´óÊý¾Ý Òƶ¯¿ª·¢ ǶÈëʽ ¿ª·¢¹¤¾ß Êý¾Ý½á¹¹ÓëËã·¨ ¿ª·¢²âÊÔ ÓÎÏ·¿ª·¢ ÍøÂçЭÒé ϵͳÔËά
½Ì³Ì: HTML½Ì³Ì CSS½Ì³Ì JavaScript½Ì³Ì GoÓïÑÔ½Ì³Ì JQuery½Ì³Ì VUE½Ì³Ì VUE3½Ì³Ì Bootstrap½Ì³Ì SQLÊý¾Ý¿â½Ì³Ì CÓïÑÔ½Ì³Ì C++½Ì³Ì Java½Ì³Ì Python½Ì³Ì Python3½Ì³Ì C#½Ì³Ì
ÊýÂë: µçÄÔ ±Ê¼Ç±¾ ÏÔ¿¨ ÏÔʾÆ÷ ¹Ì̬ӲÅÌ Ó²ÅÌ ¶ú»ú ÊÖ»ú iphone vivo oppo СÃ× »ªÎª µ¥·´ ×°»ú ͼÀ­¶¡
 
   -> C++֪ʶ¿â -> c++ STL ѧϰ±Ê¼Ç -> ÕýÎÄÔĶÁ

[C++֪ʶ¿â]c++ STL ѧϰ±Ê¼Ç

c++ STL ѧϰ±Ê¼Ç

for Ñ­»·µÄËÄÖÖÓ÷¨

int nArray[] = {0, 1, 2, 3, 4, 5};  
vector<int> vec(nArray, nArray + 6);  
  1. ÓÃϱê

    for (int i = 0; i < vec.size(); ++i)
            cout << vec[i] << " ";
    
  2. Óõü´úÆ÷

    for (auto it = vec.begin(); it != vec.end(); ++it)
            cout << (*it) << " ";
    
    for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
            cout << (*it) << " ";
    
  3. STLº¯Êý

    // [](int item) { cout << item << " "; } Ϊlambda±í´ïʽ,Ò²¾ÍÊÇ˵for_eachµÄµÚÈý¸ö²ÎÊýΪһ¸öº¯Êý
    for_each(vec.begin(), vec.end(), [](int item) { cout << item << " "; });
    
    • lambda ±í´ïʽ

      • »ù±¾Óï·¨

        //Ö±½Óµ÷ÓÃ
        [] { cout << "hello lambda1" << endl; }();
        
        //´«µÝ¸ø¶ÔÏó
        auto l = [] { cout << "hello lambda2" << endl; };
        l();
        
      • Lambda ¿ÉÒÔ·µ»ØijÎï¡£µ«²»ÐèÒªÖ¸Ã÷·µ»ØÀàÐÍ

        int a = [] { return 42; }();
        
      • ·½À¨ºÅÄÚ,¿ÉÒÔÖ¸Ã÷Ò»¸öcaptureÓÃÀ´´¦ÀíÍⲿ×÷ÓÃÓòÄÚδ±»´«µÝΪʵ²ÎµÄÊý¾Ý

        int x = 1, y = 42;
        auto q = [x, &y] {
            y++;
            cout << "x: " << x << endl;
            cout << "y: " << y << endl;    
        };
        x = y = 77;
        q();//Êä³ö½á¹û x:1 y:78
        /
        int x = 1, y = 42;
        x = y = 77;
        auto q = [x, &y] {
            y++;
            cout << "x: " << x << endl;
            cout << "y: " << y << endl;   
        };
        q();//Êä³ö½á¹û x:77 y:78
        
  4. ÐÂÔöÌØÐÔ

    for (int item : vec)
            cout << item << " ";
    

³£Óú¯Êý(#include <Algorithom>)

  1. sort(start, end, cmp)

    • µÚÒ»¸ö²ÎÊýÊÇÒªÅÅÐòµÄÊý×éµÄÆðʼµØÖ·¡£

    • µÚ¶þ¸ö²ÎÊýÊǽáÊøµÄµØÖ·¡£

    • µÚÈý¸ö²ÎÊýÊÇÅÅÐòµÄ·½·¨(¿ÉÒÔ²»Ð´,ĬÈÏ´ÓСµ½´ó)

      • stlÄÚÖÃÁ˼¸¸öcmp,Ò»¸öÊÇgreater<int>(),Ò»¸öÊÇless/()

        sort(a, a + 5, greater<int>());
        sort(a, a + 5, less<int>());
        
      • Ò²¿ÉÒÔ×Ô¼º¶¨Òåcmp

        bool up(int a,int b){
        	return a>b;
        }
        
  2. lower_bound(start, end, val, cmp)

    • µÚËĸö²ÎÊýÊDZȽϷ½·¨,¿ÉÒÔÊ¡ÂÔ

    • ×¢Òâ:ʹÓÃlower_bound()±ØÐëÌáÇ°ÅÅÐò¡£

    • ÔÚ [start, end) ÇøÓòÄÚ²éÕÒ²»Ð¡ÓÚ(>=) value µÄÔªËØ

      int a[6] = {1, 3, 5, 7, 9, 11};
      int* i = lower_bound(a, a + 6, 10);
      cout << "ÊýֵΪ:" << (*i);   // 11
      cout << "ϱíΪ:" << i - a;  // 5
      
    • ÔÚ [start, end) ÇøÓòÄÚ²éÕÒµÚÒ»¸ö²»·ûºÏ cmp ¹æÔòµÄÔªËØ

      int a[6] = {1, 3, 5, 7, 9, 11};
      int* i = lower_bound(a, a + 6, 6, [](int x, int y) { return x <= y; });
      cout << "ÊýֵΪ:" << (*i);   // 7
      cout << "ϱíΪ:" << i - a;  // 3
      
    • ·µ»ØÖµ:Èç¹ûÕÒµ½·µ»ØÕÒµ½ÔªËصĵØÖ··ñÔò·µ»ØendµÄµØÖ·¡£(ÕâÀï×¢ÒâÓпÉÄÜÔ½½ç)

      int a[6] = {1, 3, 5, 7, 9, 11};
      int* i = lower_bound(a, a + 6, 12);
      cout << "ÊýֵΪ:" << (*i); // 数值为ï¼?
      
  3. upper_bound(start, end, val, cmp)

    • Óëlower_bound()ͬÀí,¿ÉÒÔÀí½âΪ;upper_bound()ÊÇ>,¶ølower_boundÊÇ>=

      int a[6] = {1, 3, 5, 7, 9, 11};
      int* i = upper_bound(a, a + 6, 7);
      cout << "ÊýֵΪ:" << (*i) << endl;  // 9
      cout << "ϱíΪ:" << i - a;         // 4
      
  4. next_permutation(start, end, cmp)

    • µÚËĸö²ÎÊýÊDZȽϷ½·¨,¿ÉÒÔÊ¡ÂÔ

    • ÇóÒ»¸öÅÅÐòµÄºóÃæÅÅÁеĺ¯Êý

      int a[3] = {1, 3, 2};
      do {
          for (int x : a) {
              cout << x << " ";
          }
          cout << endl;
      } while (next_permutation(a, a + 3));
      /* 1 3 2 
      2 1 3
      2 3 1
      3 1 2
      3 2 1*(ûÓÐ1 2 3)/
      
    • cmpÖ¸¶¨ÅÅÐò·½·¨

      bool Compare(int x, int y) {
          if (x > y)
              return true;
          else
              return false;
      }
      
      int a[3] = {1, 3, 2};
      do {
          for (int x : a) {
              cout << x << " ";
          }
          cout << endl;
      } while (next_permutation(a, a + 3,Compare));
      /* 1 3 2 
      1 2 3*/
      
  5. prev_permutation(start, end ,cmp)

    • ºÍ next_permutation º¯ÊýÒ»Ñù,Ö»ÊÇÇóÒ»¸öÅÅÐòµÄÇ°ÃæÅÅÁеĺ¯Êý
  6. unique(start, end)

    • ʹÓøú¯ÊýÇ°,Ò»¶¨ÒªÏȶÔÐòÁнøÐÐÅÅÐò

    • unique()½«²»Öظ´µÄÔªËطŵ½ÈÝÆ÷µÄÇ°Ãæ,·µ»ØÖµÊÇÈ¥ÖØÖ®ºóµÄβµØÖ·¡£

      int a[4] = {1, 2, 3, 3};
      int k = unique(a, a + 4)-a;
      for (int i = 0; i < k;i++){
          cout << a[i];
      }// 123
      

³£ÓÃÈÝÆ÷

  1. string

    ³£Ó÷½·¨:

    • size()¡¢length()¡ª¡ª·µ»Ø×Ö·û´®³¤¶È

    • empty()¡ª¡ªÅжÏ×Ö·û´®ÊÇ·ñΪ¿Õ

    • clear()¡ª¡ªÇå¿Õ×Ö·û´®

    • substr(start, size)¡ª¡ª·µ»Ø×Ó´®

      • start ÊÇÒª»ñÈ¡µÄ×Ó´®µÄÆðʼµØÖ·,sizeÊÇÒª»ñÈ¡µÄ³¤¶È
      string a = "abcd";
      string b = a.substr(1, 2);
      cout << b;
      //bc
      
    • c_str()¡ª¡ª·µ»Ø×Ö·û´®ËùÔÚ×Ö·ûÊý×éµÄÆðʼµØÖ·

      string a = "abcd";
      const char* b = a.c_str();
      cout << b;
      
  2. queue<T>(ÏȽøÏȳö)

    ³£Ó÷½·¨:

    • size()¡ª¡ª·µ»Ø¶ÓÁдóС

    • empty()¡ª¡ªÅж϶ÓÁÐÊÇ·ñΪ¿Õ

    • push()¡ª¡ª·ÅÈëÔªËØ

    • front()¡ª¡ª·µ»Ø¶ÓÍ·ÔªËØ

    • back()¡ª¡ª·µ»Ø¶ÓβԪËØ

    • pop()¡ª¡ªµ¯³ö¶ÓÍ·ÔªËØ,ûÓзµ»ØÖµ

  3. priority_queue<T>(ÓÅÏȶÓÁÐ)

    ¶¨Òå:priority_queue<Type, Container, Functional>

    • TypeÊÇ´æ´¢Êý¾ÝµÄÀàÐÍ

    • ContainerÊÇ´æ´¢Êý¾ÝµÄÈÝÆ÷µÄÀàÐÍ(ĬÈÏΪvctor)

    • FunctionalÊDZȽϵķ½·¨(ÒªÓּ¯ÊýµÄÐÎʽʵÏÖ,ĬÈÏΪless(ÉýÐòÅÅÁÐ))

      • ·Âº¯Êý²»ÊǺ¯Êý,ËüÊÇÒ»¸öÀà
      • ·Âº¯ÊýÖØÔØÁË()ÔËËã·û,ʹµÃËüµÄ¶ÔÄã¿ÉÒÔÏñº¯ÊýÄÇÑù×Óµ÷ÓÃ
      class Cmp {
          public:
           bool operator()(int x, int y) { return x > y; }
      };
      
      Cmp cmp;
      cout << cmp(1, 2);//0
      

    ³£Ó÷½·¨:

    • ×Ô¶¨ÒåÅÅÐò

      priority_queueÒª×Ô¶¨ÒåÅÅÐòÓÐÁ½ÖÖ·½·¨

      • Ò»ÊÇÖ±½Ó½« < ÔËËã·ûÖØÔØ

        struct node {
            //pair½«Ò»¶ÔÖµ(T1ºÍT2)×éºÏ³ÉÒ»¸öÖµ
            //Á½¸öÖµ¿ÉÒÔ·Ö±ðÓÃpairµÄÁ½¸ö¹«Óк¯ÊýfirstºÍsecond·ÃÎÊ¡£
            pair<int, int> a;
            //×¢ÒâÕâÀïµÄconstÒ»¶¨Òª´øÉÏ,¾Þ¿Ó!!
            bool operator<(node b) const {
                if (a.first == b.a.first) {
                    return a.second < b.a.second;
                } else {
                    return a.first < b.a.second;
                }
            }
        };
        
        priority_queue<node> q;
        for (int i = 0; i < 5; i++) {
            pair<int, int> x = {i + 1, i - 1};
            node x1 = {x};
            q.push(x1);
        }
        while (!q.empty()) {
            cout << q.top().a.first << ' ' << q.top().a.second << endl;
            q.pop();
        }
        /*5 3
        3 1
        2 0
        4 2
        1 -1*/
        
      • ¶þÊÇÖØд·Âº¯Êý

        struct Cmp {
            bool operator()(pair<int, int> a, pair<int, int> b) {
                if (a.first == b.first)
                    return a.second > b.second;
                else
                    return a.first > b.first;
            }
        };
        
        priority_queue<pair<int, int>> q;
        for (int i = 0; i < 5; i++) {
            q.push(pair<int, int>{i + 1, i - 1});
        }
        while (!q.empty()) {
            cout << q.top().first << ' ' << q.top().second << endl;
            q.pop();
        }
        /*5 3
        4 2
        3 1
        2 0
        1 -1*/
        
    • push()¡ª¡ª²åÈëÔªËØ

    • top()¡ª¡ª·µ»Ø¶Ñ¶¥ÔªËØ

    • pop()¡ª¡ªµ¯³ö¶Ñ¶¥ÔªËØ(×¢ÒâÇø±ðÓÚqueueµÄfront())

  4. stack(ÏȽøºó³ö)

    ³£Ó÷½·¨:

    • size()¡ª¡ª·µ»Ø³¤¶È
    • empty()¡ª¡ªÅпÕ
    • push()¡ª¡ªÌí¼ÓÔªËØ
    • pop()¡ª¡ªµ¯³öÕ»¶¥ÔªËØ,ÎÞ·µ»ØÖµ
    • top()¡ª¡ª·µ»ØÕ»¶¥ÔªËØ
  5. map

    • ʹÓÃ[]½øÐвåÈë

      map<char, int> a;
      a['a'] = 1;
      
    • insert(pos, value)

      map<char, int> a;
      a.insert({'a', 1});
      
    • at(key)¡ª¡ª·µ»ØkeyµÄvalue,Èç¹ûûÓеĻ°±¨´í

      map<char, int> a;
      a.insert({'a', 1});
      cout << a.at('a');//1
      //cout << a['a']; Èç¹ûûÓеĻ°²»»á±¨´í,»á·µ»Ø0
      
    • eraze(key)¡ª¡ªÉ¾³ýÖ¸¶¨¼üÖµ¶Ô,ɾ³ý³É¹¦·µ»Ø1,·ñÔò0

      map<char, int> a;
      a.insert({'a', 1});
      cout << a.erase('a') << endl;
      cout << a['a'] << endl;//1 0
      
  6. unordered_map

    • mapÊÇÓÐÐòµÄ,µ«ÊÇunordered_mapÊÇÎÞÐòµÄ

    • ÄÚÖùþÏ£,ЧÂʺܸߡ£

  7. vector

    Ö§³ÖÊý×éÐÎʽֱ½Ó·ÃÎÊ

    ³£Óú¯Êý:

    • size()
    • empty()
    • clear()
    • front()
    • back()
    • push_back()
    • pop_back()
    • begin()
    • end()
  C++֪ʶ¿â ×îÐÂÎÄÕÂ
¡¾C++¡¿ÓÑÔª¡¢Ç¶Ì×Àà¡¢Òì³£¡¢RTTI¡¢ÀàÐÍת»»
ͨѶ¼µÄ˼·ÓëʵÏÖ£¨CÓïÑÔ)
C++PrimerPlus µÚÆßÕ º¯Êý-C++µÄ±à³ÌÄ£¿é£¨
Problem C: Ëã·¨9-9~9-12£ºÆ½ºâ¶þ²æÊ÷µÄ»ù±¾
MSVC C++ UTF-8±à³Ì
C++½ø½× ¶à̬ԭÀí
¼òµ¥stringÀàc++ʵÏÖ
ÎÒµÄÄê¶È×ܽá
¡¾CÓïÑÔ¡¿ÒÔÉîºñµØ»ùÖþΰ°¶¸ßÂ¥-»ù´¡Æª£¨Áù
cÓïÑÔ³£¼û´íÎóºÏ¼¯
ÉÏһƪÎÄÕ      ÏÂһƪÎÄÕ      ²é¿´ËùÓÐÎÄÕÂ
¼Ó:2021-10-18 17:12:27  ¸ü:2021-10-18 17:15:26 
 
¿ª·¢: C++֪ʶ¿â Java֪ʶ¿â JavaScript Python PHP֪ʶ¿â È˹¤ÖÇÄÜ Çø¿éÁ´ ´óÊý¾Ý Òƶ¯¿ª·¢ ǶÈëʽ ¿ª·¢¹¤¾ß Êý¾Ý½á¹¹ÓëËã·¨ ¿ª·¢²âÊÔ ÓÎÏ·¿ª·¢ ÍøÂçЭÒé ϵͳÔËά
½Ì³Ì: HTML½Ì³Ì CSS½Ì³Ì JavaScript½Ì³Ì GoÓïÑÔ½Ì³Ì JQuery½Ì³Ì VUE½Ì³Ì VUE3½Ì³Ì Bootstrap½Ì³Ì SQLÊý¾Ý¿â½Ì³Ì CÓïÑÔ½Ì³Ì C++½Ì³Ì Java½Ì³Ì Python½Ì³Ì Python3½Ì³Ì C#½Ì³Ì
ÊýÂë: µçÄÔ ±Ê¼Ç±¾ ÏÔ¿¨ ÏÔʾÆ÷ ¹Ì̬ӲÅÌ Ó²ÅÌ ¶ú»ú ÊÖ»ú iphone vivo oppo СÃ× »ªÎª µ¥·´ ×°»ú ͼÀ­¶¡

360ͼÊé¹Ý ¹ºÎï Èý·á¿Æ¼¼ ÔĶÁÍø ÈÕÀú ÍòÄêÀú 2024Äê12ÈÕÀú -2024/12/29 19:12:43-

ͼƬ×Ô¶¯²¥·ÅÆ÷
¡ýͼƬ×Ô¶¯²¥·ÅÆ÷¡ý
TxTС˵ÔĶÁÆ÷
¡ýÓïÒôÔĶÁ,С˵ÏÂÔØ,¹ÅµäÎÄѧ¡ý
Ò»¼üÇå³ýÀ¬»ø
¡ýÇáÇáÒ»µã,Çå³ýϵͳÀ¬»ø¡ý
ͼƬÅúÁ¿ÏÂÔØÆ÷
¡ýÅúÁ¿ÏÂÔØͼƬ,ÃÀŮͼ¿â¡ý
  ÍøÕ¾ÁªÏµ: qq:121756557 email:121756557@qq.com  ITÊýÂë
Êý¾Ýͳ¼Æ