Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17911243
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类: C/C++

2008-05-27 10:39:43

这里主要是尝试使用了vector的一些基本方法以及将vector作为vector的模版来使用的两种情况,程序也很简单。如下:
(因为使用哪个“插入代码”功能实在等不下去了,就直接粘贴了)
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    vector v;
    for (int i = 0; i<5 ;i++ )
    {
        v.push_back(i);
    }
    // copy the vector to the screen, from first to last.
    copy(v.begin(),v.end(),ostream_iterator(cout," "));
    cout<    // get the top element of th vector
    int i=v.back();
    cout<    // // get the bottom element of th vector
    i=v.front();
    cout<    // copy the vector to the screen from last ro first
    copy(v.rbegin(),v.rend(),ostream_iterator(cout," "));
    cout<    // get the element at specific place
    cout<
    return 0;
}
该程序运行结果如下:
0 1 2 3 4
4
0
4 3 2 1 0
2

另外一个使用vector作为vector的模版的程序:
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
   
    vector> vv;

    vector vi;
    vi.push_back(4);
    vi.push_back(6);
    vv.push_back(vi);

    vector vi1;
    vi1.push_back(1);
    vi1.push_back(2);
    vv.push_back(vi1);

    int c = 0;
    // get the vector's capacity. If using size(), it should be as follows:
    // int s = vv.size();
    // while (c    // note: "while(c    while (c    {
        /* first, get the top element via back(), copy its elements to screen;
         * then pop up it to make the top the next element.
         */
        vector v = vv.back();
        copy(v.begin(),v.end(),ostream_iterator(cout," "));
        cout<        vv.pop_back();
        c++;
    }
   
    return 0;
}
该程序结果如下:
1 2
4 6

我相信,只要努力学习,总会有收获的。
阅读(295) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~