Cannot use .begin() or .end() on an array

后端 未结 6 1281
陌清茗
陌清茗 2020-12-05 10:01

The error reads:

request for member \'begin\', \'end\' in \'arr\' which is non class type int[5], unable to deduce from expression error.

6条回答
  •  离开以前
    2020-12-05 10:32

    In C++, arrays are not classes and therefore do not have any member methods. They do behave like pointers in some contexts. You can take advantage of this by modifying your code:

    #include 
    using namespace std;
    
    int main()
    {
        int * mypointer;
    
        const int SIZE = 5;
        int arr[SIZE] = {1,3,5,7,9};
    
        mypointer = arr;
    
        for(auto it = arr; it != arr + SIZE; ++it) {
            cout<<*mypointer<

    Of course, this means that mypointer and it both contain the same address, so you don't need both of them.

提交回复
热议问题