dimanche 19 juillet 2020

What is the meaning of the error "no matching function for call 'begin(int [len])' " in my code?

I am pretty new to C++ coding and got a question in C++ primer 5th edition Ex 3.42 which asked me to initialize an array with the elements of a vector .

So , i wrote thus code but am not able to understand why is begin function throwing an error of no matching function to call .

I also tried to correct the code by removing the begin function and initializing *pbeg with arr ,

like this ,

int *pbeg = arr ;

And it works .
Can someone please explain why and what am i doing wrong by using begin ?

#include <iostream>
#include <vector>
#include <iterator>

using std::cin ;
using std::cout ;
using std::endl ;
using std::vector ;
using std::begin ;

int main()
{
    vector<int> ivec ;
    int i ;

    while(cin >> i)
        ivec.push_back(i) ;
    const auto len = ivec.size() ;
    int arr[len] , *pbeg = begin(arr) ;  // here it shows the error 
    for(auto c : ivec)
    {
        *pbeg = c ;
        ++pbeg ;
    }
    for(auto c : arr)
        cout << c << "," ;
    cout << endl ;
    return 0 ;
}

Aucun commentaire:

Enregistrer un commentaire