C++ϵͳѧϰ֮ËÄ£ºÊý×é

2018-08-10 11:12:08À´Ô´£º²©¿ÍÔ° ÔĶÁ ()

ÐÂÀÏ¿Í»§´ó»ØÀ¡,ÔÆ·þÎñÆ÷µÍÖÁ5ÕÛ

ÓëvectorµÄÒìͬ

  • Ïàͬ£º¶¼ÊÇ´æ·ÅÀàÐÍÏàͬ¶ÔÏóµÄÈÝÆ÷
  • ²»Í¬£ºÊý×éµÄ´óСȷ¶¨²»±ä£¬²»ÄÜËæÒâÏòÊý×éÖÐÔö¼ÓÔªËØ

1¡¢¶¨ÒåºÍ³õʼ»¯ÄÚÖÃÊý×é

¡¡¡¡Êý×éÖÐÔªËصĸöÊýÒ²ÊôÓÚÊý×éÀàÐ͵ÄÒ»²¿·Ö£¬±àÒëµÄʱºòά¶ÈÓ¦¸ÃÊÇÒÑÖªµÄ£¬Ò²¾ÍÊÇ˵£¬Î¬¶È±ØÐëÊÇÒ»¸ö³£Á¿±í´ïʽ¡£

¡¡¡¡Ä¬ÈÏÇé¿öÏ£¬Êý×éµÄÔªËر»Ä¬Èϳõʼ»¯¡£

NOTE£º

  • ¶¨ÒåÊý×éµÄʱºò±ØÐëÖƶ¨Êý×éÀàÐÍ£¬²»ÔÊÐíÓÃauto
  • Êý×éÔªËØӦΪ¶ÔÏ󣬲»ÄÜÊÇÒýÓÃ

ÏÔʽ³õʼ»¯Êý×éÔªËØ

¡¡¡¡¿ÉÒÔ¶ÔÊý×éÔªËؽøÐÐÁбí³õʼ»¯£¬´ËʱÔÊÐíºöÂÔÊý×éµÄά¶È¡£µ±Ö¸¶¨ÁËά¶È£¬Ôòά¶ÈÓ¦±ÈÁбí³õʼֵµÄÊýÁ¿¶à£¬µ±Î¬¶È±È³õʼ»¯ÁбíµÄÊýÁ¿´óʱ£¬¶àµÄ²¿·ÖĬÈϳõʼ»¯¡£

×Ö·ûÊý×éµÄÌØÊâÐÔ

¡¡¡¡×Ö·ûÊý×éÔÊÐíÓÃ×Ö·û´®×ÖÃæÖµÀ´½øÐгõʼ»¯£¬µ«Êý×éµÄά¶È±ØÐë±È×Ö·û´®×ÖÃæÖµ´ó1£¬ÓÃÀ´Ê¢·ÅÌí¼ÓµÄ¿Õ×Ö·û'/0'£»

char a[]="hi";
char a1[2]="hi";    //´íÎó£¬×îºóµÄ¿Õ×Ö·û'/0'ûµØ·½·Å

²»ÔÊÐí¿½±´ºÍ¸³Öµ

¡¡¡¡²»Äܽ«Êý×éµÄÄÚÈÝ¿½±´¸øÆäËûÊý×é×÷Ϊ³õʼֵ£¬Ò²²»ÄÜÓÃÊý×éΪÆäËûÊý×鸳ֵ

int a[]={0,1,2};
int b[]=a;     //²»ÔÊÐíʹÓÃÒ»¸öÊý×é³õʼ»¯ÁíÒ»¸öÊý×é       
int b=a;        //²»ÔÊÐí°ÑÊý×éÖ±½Ó¸³Öµ¸øÁíÒ»Êý×é

Àí½â¸´ÔÓµÄÊý×éÉùÃ÷

¡¡¡¡Êý×é±¾ÉíÒ²ÊǶÔÏó£¬Òò´Ë¿ÉÒÔ¶¨ÒåÖ¸ÏòÊý×éµÄÖ¸ÕëºÍÒýÓá£

int *ptrs[10];    //º¬ÓÐ10¸öÕûÐÍÖ¸ÕëµÄÊý×é
int &refs[10]=a;    //´íÎ󣬲»´æÔÚÒýÓõÄÊý×飬Êý×éµÄÔªËرØÐëÊǶÔÏó
int (*Parray)[10]=&arr;    //ParrayÖ¸ÏòÒ»¸öº¬ÓÐ10¸öÕûÊýµÄÊý×é
int (&arrRef)[10]=arr;    //arrRefÒýÓÃÒ»¸öº¬ÓÐ10¸öÕûÊýµÄÊý×é

2¡¢·ÃÎÊÊý×éÔªËØ

¡¡¡¡¿ÉÒÔʹÓ÷¶Î§forÓï¾ä»òϱêÀ´·ÃÎÊÊý×éÔªËØ

¡¡¡¡ÔÚʹÓÃϱêµÄʱºò£¬Í¨³£½«Æ䶨ÒåΪsize_tÀàÐÍ¡£size_tÀàÐÍÊÇÒ»ÖÖ»úÆ÷Ïà¹ØµÄÎÞ·ûºÅÀàÐÍ£¬Ëü±»Éè¼ÆµÃ×ã¹»´óÒÔ±ãÄܱíʾÄÚ´æÖÐÈÎÒâ¶ÔÏóµÄ´óС¡£ÔÚcstddefÍ·ÎļþÖж¨ÒåÁËsize_tÀàÐÍ¡£

¡¡¡¡±éÀúÊý×éËùÓÐÔªËØ£¬×îºÃµÄ·½·¨ÊÇ·¶Î§forÓï¾ä¡£

¼ì²éϱêµÄÖµ

3¡¢Ö¸ÕëºÍÊý×é

¡¡¡¡ÔںܶàÓõ½Êý×éÃû×ֵĵط½£¬±àÒëÆ÷¶¼»á×Ô¶¯µØ½«ÆäÌ滻Ϊһ¸öÖ¸ÏòÊý×éÊ×ÔªËصÄÖ¸Õë¡£

¡¡¡¡¶ÔÊý×éµÄÔªËØʹÓÃÈ¡µØÖ··û¾ÍÄܵõ½Ö¸Ïò¸ÃÔªËصÄÖ¸Õë¡£

Ö¸ÕëÒ²Êǵü´úÆ÷

¡¡¡¡Ö¸ÏòÊý×éÔªËصÄÖ¸Õë¿ÉÒÔÖ´Ðеü´úÆ÷Ò»ÑùµÄ²Ù×÷¡£

    int a[10] = { 0 };
    int n = 0;
    for (auto &i : a)
    {
        i = n;
        n++;
    }
    int *p1 = a;    //Ï൱ÓÚµü´úÆ÷µÄbegin
    int *p2 = &a[10];    //Ï൱ÓÚµü´úÆ÷µÄend
    for (int *p3 = p1; p3 != p2; p3++)
    {
        cout << *p3 << endl;
    }

±ê×¼¿âº¯ÊýbeginºÍend

¡¡¡¡ÉÏÊöʹÓÃa[10]µÄµØÖ·À´±íʾβºóÖ¸ÕëºÜÈÝÒ׳ö´í£¬Òò´Ë±ê×¼¿â¶¨ÒåÁËÊý×éÓõÄbeginºÍendº¯ÊýÀ´µÃµ½Êý×éµÄÊ×Ö¸ÕëºÍβºóÖ¸Õ룬ÆäÔÚiteratorÍ·ÎļþÖС£

int a[10] = { 0 };
    int n = 0;
    for (auto &i : a)
    {
        i = n;
        n++;
    }
    /*int *p1 = a;    
    int *p2 = &a[10];*/
    int *p1 = begin(a);
    int *p2 = end(a);
    for (int *p3 = p1; p3 != p2; p3++)
    {
        cout << *p3 << endl;
    }

¶þÕßͬÑùµÄЧ¹û¡£

Ö¸ÕëÔËËã

¡¡¡¡¸øÖ¸Õë¼ÓÉÏÒ»¸öÕûÊý£¬µÃµ½µÄÐÂÖ¸ÕëÈÔÐèÖ¸ÏòͬһÊý×éµÄÆäËûÔªËØ£¬»òÕßÖ¸ÏòͬһÊý×éµÄβԪËصÄÏÂһλÖá£

½âÒýÓúÍÖ¸ÕëÔËËãµÄ½»»¥

¡¡¡¡Ö¸Õë¼ÓÉÏÒ»¸öÕûÊýµÃµ½µÄ½á¹ûÈÔÊÇÒ»¸öÖ¸Õ룬Òò´Ë¿ÉÒÔ½âÒýÓøÃÖ¸Õë¡£

ϱêºÍÖ¸Õë

¡¡¡¡¶ÔÊý×éÖ´ÐÐϱêÔËËãÆäʵÊǶÔÖ¸ÏòÊý×éÔªËصÄÖ¸ÕëÖ´ÐÐϱêÔËËã¡£

int i=a[2];
//ÉÏÃæµÄϱêÒýÓÃÆäʵÊÇÏÂÃæµÄ¹ý³Ì
int *p=a;
i=*(p+2);

¡¡¡¡ËäÈ»±ê×¼¿âÀàÐÍvectorºÍstringÒ²ÄÜÖ´ÐÐϱêÔËË㣬µ«ÊÇÊý×éÓëËüÃÇÏà±È»¹ÊÇÓÐËù²»Í¬¡£±ê×¼¿âÀàÐÍÏÞ¶¨Ê¹ÓõÄϱê±ØÐëÊÇÎÞ·ûºÅÀàÐÍ£¬¶øÄÚÖõÄϱêÔËËãÎÞ´ËÒªÇó¡£

int *p=&a[2];
int j=p[1];    //ʵ¼ÊÊÇa[3]
int k=p[-2];    //ʵ¼ÊÉÏÊÇa[0]

Óë¾É´úÂëµÄ½Ó¿Ú

»ìÓÃstring¶ÔÏóºÍC·ç¸ñ×Ö·û´®

¡¡¡¡C·ç¸ñ×Ö·û´®£ºÒÔ¿Õ×Ö·û½áβµÄ×Ö·ûÊý×é¡£

¡¡¡¡stringÌṩһ¸öÃûΪc_str()µÄ³ÉÔ±º¯ÊýÀ´½«stringת»»ÎªC·ç¸ñ×Ö·û´®£¬·µ»ØµÄÊÇconst char*ÀàÐÍ¡£

ʹÓÃÊý×é³õʼ»¯vector¶ÔÏó

¡¡¡¡¡¡Ö¸Ã÷Òª¿½±´ÇøÓòµÄÊ×ÔªËصØÖ·ºÍβºóµØÖ·¼´¿É¡£

int a[]={0,1,2,3,4,5};
vector<int> v(begin(a),end(a));

NOTE£º²»ÄÜÓÃvector³õʼ»¯Êý×é¡£

  • ¾¡Á¿²»Ê¹ÓÃÊý×éºÍÖ¸Õ룬¶øʹÓÃvectorºÍµü´úÆ÷
  • ¾¡Á¿²»Ê¹ÓÃC·ç¸ñ×Ö·û´®£¬¶øʹÓÃstring

¶àάÊý×é

¡¡¡¡C++ÓïÑÔÖÐûÓжàάÊý×飬ͨ³£Ëù˵µÄ¶àάÊý×éÆäʵÊÇÊý×éµÄÊý×é¡£

int a[3][4];¡¡¡¡//´óСΪ3µÄÊý×飬ÿ¸öÔªËØÊǺ¬ÓÐ4¸öÕûÊýµÄÊý×é

int b[10][20][30];

¶àάÊý×éµÄ³õʼ»¯

¡¡¡¡Ê¹Óû¨À¨ºÅÀ¨ÆðÀ´µÄÒ»×éÖµ³õʼ»¯¶àάÊý×飬ºÍÆÕͨÊý×é³õʼ»¯Ò»Ñù£¬Ö»ÊÇÊý×éµÄÔªËØÒ²ÊÇÊý×é¶øÒÑ¡£

int a[3][4]={
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}};

int a[3][4]={0,,1,2,3,4,5,6,7,8,9,10,11};    //ºÍÉÏÃæµÄЧ¹ûÒ»Ñù

int a[3][4]={{0},{1},{3}};    //ÆäÓàµÄĬÈϳõʼ»¯

¶àάÊý×éµÄϱêÒýÓÃ

¡¡¡¡±í´ïʽº¬ÓеÄϱêÔËËã·ûÊýÁ¿ºÍÊý×éµÄά¶ÈÒ»Ñù¶à£¬¸Ã±í´ïʽµÄ½á¹û½«ÊǸø¶¨ÀàÐ͵ÄÔªËØ£»µ±±ÈÊý×éµÄ ά¶ÈÊýÁ¿Ð¡Ê±£¬±íʾµÄÊÇÄÚ²ãÊý×é¡£

ʹÓ÷¶Î§forÓï¾ä´¦Àí¶àάÊý×é

size_t cnt=0;
for(auto &row:a)
   for(auto &col:row)
        col=cnt;
        cnt++;
}

NOTE:

¡¡¡¡Ê¹Ó÷¶Î§forÓï¾ä´¦Àí¶àάÊý×飬³ýÁË×îÄÚ²ãµÄÑ­»·Í⣬ÆäËûËùÓÐÑ­»·µÄ¿ØÖƱäÁ¿¶¼Ó¦ÓÃÊÇÒýÓÃÀàÐÍ¡£ÎªÁË·ÀÖ¹±àÒëÆ÷½«auto¿ØÖƱäÁ¿×ª»»³ÉÊý×éÊ×ÔªËصÄÖ¸Õë¡£

Ö¸ÕëºÍ¶àάÊý×é

¡¡¡¡µ±³ÌÐòʹÓöàάÊý×éµÄÃû×Öʱ£¬Ò²»á×Ô¶¯½«Æäת»»³ÉÖ¸ÏòÊý×éÊ×ÔªËصÄÖ¸Õë¡£

¡¡¡¡Ò²¿ÉÒÔͨ¹ýʹÓÃauto»òdecltypeÀ´±éÀú¶àάÊý×é

    int a[3][4] = {};
    int cnt = 0;
    for (auto p = a; p != a + 3; p++)
    {
        for (auto q = *p; q != *p + 4; q++)
        {
            *q = cnt;
            cnt++;
        }
    }
    for (auto p = a; p != a + 3; p++)
    {
        for (auto q = *p; q != *p + 4; q++)
        {
            cout << *q << " | ";
        }
        cout << endl;
    }    

Ò²¿ÉÒÔʹÓÃbeginºÍendÀ´¼ò»¯

        int a[3][4] = {};
    int cnt = 0;
    for (auto p = begin(a); p != end(a); p++)
    {
        for (auto q = begin(*p); q != end(*p); q++)
        {
            *q = cnt;
            cnt++;
        }
    }
    for (auto p = begin(a); p != end(a); p++)
    {
        for (auto q = begin(*p); q != end(*p); q++)
        {
            cout << *q << " | ";
        }
        cout << endl;
    }

ÀàÐͱðÃû¼ò»¯¶àάÊý×éµÄÖ¸Õë

    using int_array=int[4];
    int a[3][4] = {};
    int cnt = 0;

    for (auto p = begin(a); p != end(a); p++)
    {
        for (auto q = begin(*p); q != end(*p); q++)
        {
            *q = cnt;
            cnt++;
        }
    }
    for (int_array *p = a; p != a + 3; p++)
    {
        for (int *q = *p; q != *p + 4; ++q)
        {
            cout << *q << " | ";
        }
        cout << endl;
    }    

 

±êÇ©£º

°æȨÉêÃ÷£º±¾Õ¾ÎÄÕ²¿·Ö×ÔÍøÂ磬ÈçÓÐÇÖȨ£¬ÇëÁªÏµ£ºwest999com@outlook.com
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¬±¾Õ¾ËùÌṩµÄÉãÓ°ÕÕƬ£¬²å»­£¬Éè¼Æ×÷Æ·£¬ÈçÐèʹÓã¬ÇëÓëÔ­×÷ÕßÁªÏµ£¬°æȨ¹éÔ­×÷ÕßËùÓÐ

ÉÏһƪ£ºopenCVÅäÖÃ

ÏÂһƪ£ºÈݳâÎÊÌâ