Loop through array via iterator

#include <iostream>
#include <iterator>

using namespace std;

void arrayPointer3();

int main()
{
    arrayPointer3();
    return 0;
}

void arrayPointer3()
{
    int arr[] = {12, 24, 5456, 65767, 45643, 2356, 453245, 3452345, 56456};
    int *ptr = arr; 
    for (auto it = std::begin(arr); it != std::end(arr); ++it)
    {
        cout<<*it<<"\t";
    }
    cout << endl
         << "Finished in arrayPointer3()" << endl;
}
g++ -g -std=c++2a -I. h2.cpp -o h1 -luuid

 

 

 

 

 

void loopArray4();

int main()
{
    loopArray4();
    return 0;
}

void loopArray4()
{
    int arr[] = {12, 24, 5456, 65767, 45643, 2356, 453245, 3452345, 56456};
    int *ptr = arr; 
    int *endPtr=std::end(arr);
    while(ptr!=endPtr)
    {
        cout<<*ptr<<"\t";
        ptr++;
    }
    cout<<endl<<"Finished in loopArray4()"<<endl;
}

 

void array5();

int main()
{
    array5();
    return 0;
}

void array5()
{ 
    int *ptr=new int[9]{12, 24, 5456, 65767, 45643, 2356, 453245, 3452345, 56456};
    while(*ptr)
    {
        cout<<"V="<<*ptr<<",pointer="<<ptr<<endl;
        ptr++;
    }
    cout<<"Finished in array5()"<<endl;
}

 

posted @ 2022-02-07 17:44  FredGrit  阅读(21)  评论(0编辑  收藏  举报