vendredi 8 novembre 2019

The for loop does not end only at a declared function, not main

I wrote the code below

I intend the for loop at find_period is ended when iter meet arr.end()

But it didn't.

But when I moved the for loop to the main(), it worked properly.

I don't know why this thing happens.

At first, I think it is related with the concept of pointer. Therefore, I modified the code that use pointer int find_period(vector<int> *arr). But it didn't work too.

Could you give me a little hint?

Thanks a lot!

int find_period(vector<int> arr){
    vector<int>::iterator iter;
    for(iter=arr.begin(); iter != arr.end(); iter++){
        cout << *iter << endl;
    }
}

int main() {
    vector<int> input = {2,2,3,4,5,6,7,8,9};
    vector<int>::iterator iter;

    //for(iter=input.begin(); iter != input.end(); iter++){
    //  cout << *iter << endl;
    ///}

    cout << find_period(input) << endl;
}

Aucun commentaire:

Enregistrer un commentaire