close

template<typename Cont>
typename Cont::const_iterator findNull(const Cont &c)
{
    //typename Cont::const_iterator it;
    auto it = c.begin() ;
    for ( ; it != c.end(); ++it)
        if (*it == 0)
            break;
    return it;
}

int main()
{

    int a = 1000, b = 2000, c = 3000;

    vector<int *> v2 {&a,&b,&c} ;
    vector<int *>::const_iterator cit2 = findNull(v2);
    if (cit2 == v2.end())
        cout << "no null pointers in v2" << endl;
    else
    {
        vector<int *>::difference_type pos = cit2 - v2.begin();
        cout << "null pointer found at pos. " << pos << endl;
    }

    array<const char*,6>  names {"M1","M2","M3","M4","M5",0 } ;

    vector<const char *> v3 { names[0] , names[2] ,names[5] } ;
    vector<const char *>::const_iterator cit3 = findNull(v3);
    if (cit3 == v3.end())
        cout << "no null pointers in v3" << endl;
    else
    {
        vector<int *>::difference_type pos = cit3 - v3.begin();
        cout << "null pointer found at pos. " << pos << endl;
    }

    cout << "========================" << endl ;
    array<int,5> ai { 10,20,30,40,50 } ;
    list<int> li( ai.begin(),ai.end() ) ;
    for(auto it=li.begin();it != li.end();it++)
        cout << *it << endl ;
}

 

arrow
arrow
    全站熱搜

    hedgezzz 發表在 痞客邦 留言(0) 人氣()