Would Iterate Vector Given Const Int First Element Method First Pointer Beginning Vector L Q37023843

How would I iterate through a vector when given a const int* tothe first element.

In this method, first is the pointer to the beginning of avector, last is a pointer to its end.

Ex: node toTree(const int* first, const int* last){

}


Answer


void toTree(const int*first, const int*last)

{

    // point trav to first

    int *trav =first;

   

    // traverse the vector

    while( trav!= last )

    {

       printf(“%d “,*trav);

       

        // go to nextelement

       trav++;

    }

}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.