程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++ Primer 學習筆記_44_STL實踐與分析(18)

C++ Primer 學習筆記_44_STL實踐與分析(18)

編輯:C++入門知識

STL實踐與分析

--再談迭代器【下】


 

三、反向迭代器【續:習題】

//P355 習題11.19
int main()
{
    vector iVec;
    for (vector::size_type index = 0; index != 10; ++index)
    {
        iVec.push_back(index);
    }

    for (vector::reverse_iterator r_iter = iVec.rbegin();
            r_iter != iVec.rend(); ++r_iter)
    {
        cout << *r_iter << endl;
    }
}
//習題 11.20
int main()
{
    vector iVec;
    for (vector::size_type index = 0; index != 10; ++index)
    {
        iVec.push_back(index);
    }

    for (vector::iterator iter = iVec.end()-1;iter >= iVec.begin(); --iter)
    {
        cout << *iter << endl;
    }
}

//習題 11.21
int main()
{
    int ia[] = {0,1,2,3,4,5,6,0,7,8,9,0,10};
    vector iVec(ia,ia + sizeof(ia)/sizeof(*ia));

    vector::reverse_iterator r_iter = find(iVec.rbegin(),iVec.rend(),0);
    if (r_iter != iVec.rend())
    {
        cout << "element after the last 0 : " << *(-- r_iter) << endl;
    }
    else
    {
        cout << "Not found!" << endl;
    }
}

//習題11.22
int main()
{
    int ia[] = {0,1,2,3,4,5,6,0,7,8};
    vector iVec(ia,ia + sizeof(ia)/sizeof(*ia));

    list iList;

    vector::reverse_iterator rbeg = iVec.rbegin(),rend;

    for (int i = 0; i != 3; ++i)
        ++ rbeg;

    rend = rbeg;
    for (int i = 0; i != 5; ++i)
        ++rend;

    copy(rbeg,rend,back_inserter(iList));

    for (list::iterator iter = iList.begin(); iter != iList.end(); ++iter)
    {
        cout << *iter << endl;
    }
}

四、const迭代器

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved