zoukankan      html  css  js  c++  java
  • [C++] Array

    Access the elements of an Array

        unsinged scores = {1, 2, 3, 4, 5, 6};
        for ( auto i:scores){
            cout << i << " ";
        }
        cout << endl;

    Becuase the dimension is part of each array type. the system know how many elements are in scores.

    Pointers and Arrays

    In C++, pointers and array are closely intertwined. When we use an array, the compiler ordinarily convert the array to a pointer.

        string nums[] = {"one", "two", "three"};
        string *p = &nums[0];    // p points to the first element in nums

    Arrays have a special property - in most places when we use an array, the comipler automatically substitutes a pointer to the first element.

        string *p2 = nums;    // equivalent to p2 = &nums[0]

    In most expression, when we use an object of array type, we are really using a pointer to the first element in the array. 

    When we use an array as an initializer for a variable defined as auto, the deduced type is a pointer, not an array:

        int ia[] = {0, 1, 2, 3, 4, 5};
        auto ia2(ia);    //    ia is an int * that points to the first element in ia

    which is equivalent to 

        auso ia2(&ia[0]);

    Pointer in arrays have additional operations beyond basic pointers. Pointers to array elements support the same operations as iterators on vectors or strings.

        int arr[] = {0, 2, 4, 6, 8};
        int *p = arr;    // p points to the first elemnt in arr
        ++p;        // p points to arr[1]

    When we add (or substract) an integral value to(or from) a pointer, the result is a new pointer. The new pointer points to the element the given number ahead the original pointer.

        int arr[5] = {1, 2, 3, 4, 5};
        int *p = arr;    // equivalent to int *p = &arr[1]
        int *ip2 = p + 4;    // ip2 points to arr[4], the last element in arr

    Interaction between Dereference and Pointer Arithmetic

        int ia[] = {0, 2, 4, 6, 8};
        int last = *(ia + 4);    // ok; initilize last to 8, the value of ia[4]

    The expression*(ia + 4) calculate the address four elements past ia and dereference the resulting pointer. This expression is equivalent to writing ia[4].

        last = *ia + 4;    // ok; last 4. equivalent to ia[0] + 4

    Subscripts and Pointers

    When we substripts an array, we are really subscripting a pointer to an element in that array.

        int i = ia[2];    // ia is converted to a pointer to the first element in ia.
                          // ia[2] fetchs the elements to which (ia + 2) points
        int *p = ia;      // p points to the first element in ia;
        i = *(p + 2)      // equivalent to ia[2]
        int *p = &ia[2];    // p points to the element indexed by 2
        int j = p[1];       // p[1] is equivalent to *(p + 1). 
                            // p[1] is the same element as ia[3]
        int k = p[-2];      // p[-2] is the same element as ia[0]

    The library types force the index used with a subscript to be an unsigned value. The built-in subscript operator does not. The index used with the built-in subscript operator can be negatie value.

    C-style stirngs are not  type. Instead, they are a convention for how to represent and use character strings. Strings that follow convertion are stored in character arrays and are null terminated('').

    Although C++ supports C-style strings, they should not be used by C++ programs. C-style strings are a surpringly rich source of bus and are the root cause of many security problems.

    The pointer passed to these routins must point to null-terminated arrays.

        char ca[] = {'C', '+', '+'};    // not null-terminated
        cout << strlen(ca) << endl;    // disaster; ca isn't null-terminated

    In this case, ca is an array of char but is not null-terminated. The result is undefined. The most likely effect of this call is that strlen will keep looking through the memory that follows ca until it encounter a null character.

    For most application, in additions to being safter, it is also more efficient to use library strings rather then C-style strings.

    Mixing library strings and C-style strings

        string s("hello world");
        char *str = s;                // error, cannot initialize a char * from a string
        const char *str = s.c_str();    // ok

    If a program need continue uses the content of the array returned by c_str(); the program must copy the array return by c_str();

    We cannot initilize a built-in array from another array. Nor can we initliaze an array from vector. However, we can use an array to initliaze a vector.

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

    Pointers and arrays are surprisingly error-prone.

    Modern C++ programs should use vectors and iteractor instead of built-in arrays and pointers. and use strings rather then C-style based character strings.

    Reference:

    C++ Primer, Fifth Edition, 3.5. Arrays

  • 相关阅读:
    1、1、2、3、5、8、13、21、34...... 求第X位数是多少,2种实现方式
    数据库设计的三大范式
    Parse与TryParse的区别
    常用Lambda范例
    【转载】说说JSON和JSONP,
    Lambda表达式
    Winform部署时强名称签名mshtml.dll无效的解决方案
    .net面试问答(大汇总)
    Asp.net MVC3 一语道破
    在asp.net中使用 log4net
  • 原文地址:https://www.cnblogs.com/TonyYPZhang/p/6436102.html
Copyright © 2011-2022 走看看