Chapter 3 Strings, Vectors and A

2020-03-08  本文已影响0人  再凌

1

Ex1: For int ia[3][4], please print every value of the element of ia with three methods: for-range, subscript and pointer. DO NOT USE auto.

#include<iostream>
using namespace std;
int main()
{
    int ia[3][4] = { 0 };
    auto count = 0;
    for (int (&i)[4]:ia)
    {
        for (int &j :i)
        {
            j = count++;
        }
    }
    for (size_t i = 0; i < 3;i++)
    {
        for (size_t j = 0; j < 4; j++)
            cout << ia[i][j];
        cout << endl;
    }
    for (int(*p1)[4] = begin(ia);p1 != end(ia);p1++)
    {
        for (int *p2 = begin(*p1);p2 != end(*p1);p2++)
            cout << *p2;
        cout << endl;
    }
    return 0;
}

Ex2: Please use using or typedef to modify the above example.

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    using myrefer = int(&)[4];
    typedef int myrefer2;
    int ia[3][4] = { 0 };
    auto count = 0;
    for (myrefer i:ia)
    {
        for (myrefer2 &j :i)
        {
            j = count++;
        }
    }
    using mypointer = int(*)[4];
    using mypointer2 = int*;
    for (mypointer p1 = begin(ia);p1 != end(ia);p1++)
    {
        for (mypointer2 p2 = begin(*p1);p2 != end(*p1);p2++)
            cout << *p2;
        cout << endl;
    }
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读