zoukankan      html  css  js  c++  java
  • Coursera课程笔记----C程序设计进阶----Week 5

    指针(二) (Week 5)

    字符串与指针

    • 指向数组的指针
      • int a[10]; int *p; p = a;
    • 指向字符串的指针
      • 指向字符串的指针变量
      • char a[10]; char *p; p = a;
    int main()
    {
      int a = 5;
      int *pa = &a;
      
      int b[6] = {1,2,3,4,5,6};
      int *pb = b;
      
      char c[6] = {'h','e','l','l','o',''};
      char *pc = c;
      
      cout<<a<<endl; //5
      cout<<pa<<endl; //a的地址
      
      cout<<b<<endl;//b[0]的地址
      cout<<pb<<endl;//b[0]的地址
      
      cout<<c<<endl;//hello
      cout<<pc<<endl;//hello
      
      //若不想打印字符串内容,就想打印字符串地址
      cout<<static_cast<void*>(c)<<endl;
      cout<<static_case<void*>(pc)<<endl;
      
      return 0;
    }
    
    • 字符串指针举例
    #include<iostream>
    using namespace std;
    int main()
    {
      char buffer[10] = "ABC";
      char *pc;
      pc = "hello";//学习指针之前,我们无法直接赋值,只能在定义的时候这样直接赋值
      cout<<pc<<endl; //hello
      pc++;
      cout<<pc<<endl;//ello
      cout<<*pc<<endl;//e
      pc = buffer;
      cout << pc;//ABC
      return 0;
    }
    

    指向二维数组的指针

    再谈一维数组的地址

    #include<iostream>
    using namespace std;
    int main()
    {
      int a[4] = {1,3,5,7};
      cout << a << endl; //a[0]的地址,a为指向数组首元素的指针(基类型为int)
      cout << a + 1 << endl;//a[1]的地址
      cout << &a << endl;//a[0]的地址,但相当于指向整个数组的指针(基类型为int[4])
      cout << &a + 1 << endl;//a[3]之后的地址(越界了)
      cout << *(&a) << endl;//*E,E若为指针,*E将返回E指向的内容,所以返回a[0]的地址,和打印a的结果相同
      cout << *(&a)+1 << endl;//a[1]的地址
      return 0;
    }
    
    • 数组名相当于指向数组的第一个元素的指针
    • 若a是指向数组的第一个元素的指针,即a相当于&a[0]
      • &a是“指向数组”的指针, &a+1将跨越16个字节
      • &a相当于管辖范围“上升”了一级
      • *a是数组的第一个元素a[0],即*a等价于a[0]
      • *a相当于管辖范围“下降”了一级

    指向二维数组的指针

    • 二维数组的定义
      • 二维数组a[3][4]包含3个元素:a[0],a[1],a[2]
        • 每个元素都是一个“包含4个整形元素”的数组
      • 二维数组的第一个元素是a[0]
      • a[0]是一个“包含4个整型元素”的一维数组
        • a为指向a[0]这个一维数组的指针
        • a[0]为指向第一个元素a[0][0]的指针
        • 管辖范围排序:&a>a>a[0]>a[0][0](连指针都不是,只是个量)
          • a = &a[0]
          • a[0] = &a[0][0]
          • a[0] = *a
          • a[0][0] = **a
      • 三条规律
        • 数组名相当于指向数组第一个元素的指针
        • &E相当于E⬆️
        • *E相当于E⬇️
    int main()
    {
      int a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
      
      cout<<"   a ="<<a<<endl;
      cout<<"  &a[0] ="<<&a[0]<<endl<<endl;
      
      cout<<"   a+1 ="<<a+1<<endl;
      cout<<"  &a[0]+1 ="<<&a[0]+1<<endl<<endl;
      
      cout<<"   *a ="<<*a<<endl;
      cout<<"   a[0] ="<<a[0]<<endl;
      cout<<"  &a[0][0] ="<<&a[0][0]<<endl<<endl;
      
      cout<<"   *a+1 ="<<*a+1<<endl;
      cout<<"  a[0]+1 ="<<a[0]+1<<endl;
      cout<<" &a[0][0]+1 ="<<&a[0][0]+1<<endl<<endl;
      
      ...
      
      return 0;
    }
    

    编程作业

    Quiz1 计算矩阵边缘元素之和

    #include <iostream>
    using namespace std;
    int main()
    {
        int n = 0;
        cin >> n;
        for (int i = 0; i < n; i++) {
            int a,b;
            cin >> a >> b;
            int matrix[101][101];
            int summer = 0;
            for (int j = 0; j < a; j++) {
                for (int k = 0; k < b; k++) {
                    cin >> matrix[j][k];
                    if(j == 0 || j == a-1 || k == 0 || k == b-1)
                        summer += matrix[j][k];
                }
            }
            cout << summer << endl;
        }
    
        return 0;
    }
    

    Quiz2 二维数组右上左下遍历

    #include <iostream>
    using namespace std;
    int main()
    {
        int row, col;
        cin >> row >> col;
        int shuzu[100][100];
        for (int j = 0; j < row; j++) {
            for (int o = 0; o < col; o++) {
                cin >> shuzu[j][o];
            }
        }//row 行 col 列
        int k = col + row - 1;//共有(行数+列数-1)条对角线
        for (int i = 0; i <= k; ++i) {//对每一条对角线进行处理
            cout<<"i = "<<i<<endl;
            int c = i - 1;//对角线行数列数起始点为0,每次+1;所以,刚好起始点横坐标是对角线编号-1
            for (int r = 0; r < row; r++) //同理
            {
                if (r >= 0 && r < row && c < col && c >= 0) {//通过这个,找到在范围内的。
                    cout << *(*(shuzu + r) + c) << endl;//只打印在范围内的
                }
                c--;//因为是由右边到左边,所以C--
            }
    
        }
        return 0;
    }
    //要想象到"画面外的虚线"
    

    Quiz3 文字排版

    #include <string>
    using namespace std;
    int main()
    {
        int n;
        cin >> n;
        string text;
        int flag = 80;
        for (int i = 0; i < n; i++) {
            string temp;
            cin >> temp;
            if(flag > temp.length())
            {
                text = text.append(temp);
                text = text.append(" ");
                flag -= (temp.length()+1);
            } else if (flag == temp.length()){
                text = text.append(temp);
                text = text.append("
    ");
                flag = 80;
            } else{
                text = text.append("
    ");
                flag = 80;
                text = text.append(temp);
                text = text.append(" ");
                flag -= (temp.length()+1);
            }
        }
    
        cout<<text<<endl;
    }
    //这道题我是用string解决的……感觉比较方便
    
  • 相关阅读:
    CF1174D Ehab and the Expected XOR Problem
    CF1083B The Fair Nut and Strings
    CF1088D Ehab and another another xor problem
    CF1168A Increasing by Modulo
    CF1166C A Tale of Two Lands
    CF1142A The Beatles
    CF1105D Kilani and the Game
    【uva11248】网络扩容
    【sam复习】用sam实现后缀排序
    【Educational Codeforces Round 19】
  • 原文地址:https://www.cnblogs.com/maimai-d/p/12861204.html
Copyright © 2011-2022 走看看