美文网首页
Chapter 3 Strings, Vectors and A

Chapter 3 Strings, Vectors and A

作者: 再凌 | 来源:发表于2020-03-08 17:54 被阅读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;
    }
    

    相关文章

      网友评论

          本文标题:Chapter 3 Strings, Vectors and A

          本文链接:https://www.haomeiwen.com/subject/ckkjdhtx.html