设为首页 加入收藏

TOP

C++ vector MSDN简单入门解释(二)
2012-12-02 23:00:10 】 浏览:1542
Tags:  vector  MSDN 简单 入门 解释


    // The debugger can't handle symbols more than 255 characters long.
    // STL often creates symbols longer than that.
    // When symbols are longer than 255 characters, the warning is disabled.
    #pragma warning(disable:4786)
    #include <iostream>
    #include <vector>
    using namespace std ;
    typedef vector<int> INTVECTOR;
    const int ARRAY_SIZE = 10;
    void ShowVector(INTVECTOR &theVector);
    int _tmain(int argc, _TCHAR* argv[])
    {
    // Dynamically allocated vector begins with 0 elements.
    INTVECTOR theVector;
    // Intialize the vector to contain the numbers 0-9.
    for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
    theVector.push_back(cEachItem);
    // Output the contents of the dynamic vector of integers.
    ShowVector(theVector);
    // Using void iterator erase(iterator Iterator) to
    // delete the 6th element (Index starts with 0).
    theVector.erase(theVector.begin() + 5);
    // Output the contents of the dynamic vector of integers.
    ShowVector(theVector);
    // Using iterator erase(iterator First, iterator Last) to
    // delete a range of elements all at once.
    theVector.erase(theVector.begin(), theVector.end());
    // Show what's left (actually, nothing).
    ShowVector(theVector);
    }
    // Output the contents of the dynamic vector or display a
    // message if the vector is empty.
    void ShowVector(INTVECTOR &theVector)
    {
    // First see if there's anything in the vector. Quit if so.
    if (theVector.empty())
    {
    cout << “theVector is empty.” << endl;
    return;
    }
    // Iterator is used to loop through the vector.
    INTVECTOR::iterator theIterator;
    // Output contents of theVector.
    cout << “theVector [ ” ;
    for (theIterator = theVector.begin(); theIterator != theVector.end();
    theIterator++)
    {
    cout << *theIterator;
    if (theIterator != theVector.end()-1) cout << “, ”;
    // cosmetics for the output
    }
    cout << “ ]” << endl ;
    }

      

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++ 连接Oracle 下一篇C++中调用R Language的类库

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目