zoukankan      html  css  js  c++  java
  • 算法之美--2.2数组

    2016-11-30   00:00:12

    里面有几个数组使用特别需要注意的问题。

    1.数组访问越界的问题;

    2.数组首地址为指针常量,不能a++;

    3. *p++,*(p++),*(++p)的区别。

    /*!
     * file 算法之美--数组.cpp
     *
     * author ranjiewen
     * date 2016/11/29 21:18
     *
     * 
     */
    
    #include <iostream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        //一维数组的操作,用下标访问元素
        int a[10] = {1,32,65,2,100,34,33,21,10,1};
        int sum = 0;
        for (int i = 0; i < 10;i++)
        {
            sum += a[i];
        }
        cout << "数组中元素的和为:" << sum << endl;
    
        //C++不提供对数组边界的检查,越界访问
        int b[5] = {6,7,8,9,10};
        cout << b[6] << endl;   //输出-858993460,程序并不会崩溃
    
        //二维数组的操作
        int c[][3] = { { 0, 1, 2 }, { 3, 4, 5 }, {6,7,8} }; //第二维的长度不能省略
    
        //使用指针高效的访问一,二维数据元素并操作
        int *p = &a[0];
        cout << a[0] << endl;
        cout << *p << endl;
        cout << a << endl;
        cout << &a << endl; //注意就是a的地址
        cout << &a[0] << endl;
        cout << p << endl;
        cout << *(p + 4) << endl;  //指针变量的偏移操作
        cout << *(a + 4) << endl;
        p++;
        cout << p << endl;
        (*p)++;
        cout << *p << endl;
    
        //区别*p++,*(p++),*(++p
        //*和++的运算优先级相同,结合方向为从右向左。
        p = a;  //p可以++操作,但是a不能++操作,a为数组元素的首地址,为常量指针
        cout << *p++ << endl;  
        cout << p << endl;
        cout << *(p++) << endl;
        cout << *(++p) << endl;
    
        return 0;
    }
  • 相关阅读:
    comparator接口与Comparable接口的区别
    heap和stack有什么区别
    聚集索引和非聚集索引(整理)
    SQL里的EXISTS与in、not exists与not in
    SQL中CONVERT转化函数的用法
    GCC 对C语言的扩展
    C++宏定义详解
    How to Find Processlist Thread id in gdb !!!!!GDB 使用
    Netdata----Linux 性能实时监测工具
    java开发C语言编译器
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/6116001.html
Copyright © 2011-2022 走看看