Chinaunix首页 | 论坛 | 博客
  • 博客访问: 117073
  • 博文数量: 29
  • 博客积分: 1215
  • 博客等级: 中尉
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-05 16:29
文章分类
文章存档

2010年(29)

我的朋友

分类: C/C++

2010-12-20 23:50:52

本文通过一个程序来演示容器元素的初始化。

程序清单:

#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <deque>

using namespace std;

int main(int argc,char *argv[])
{
    char *words[] = {"first","second","third","fourth","fifth"};
    /*calculate how many elements in words*/
    size_t words_size = sizeof(words)/sizeof(char *);
    /*将数组长度加到指向第一个元素的指针上就可以得到指向超出数组末端的下一个位置
      的指针。通过指向第一个元素的指针words和指向数组中最后一个元素的下一个位置
      的指针,实现了words1和words2的初始化。第二个指针提供停止复制的条件,其
      所指向的位置上存放的元素并没有复制*/

    /*initialize words1*/
    vector<string> words1(words,words+words_size);
    /*initialize words2*/
    list<string> words2(words,words+words_size);

    cout<<"The elements of words1 are below:"<<endl;
    for(vector<string>::iterator it=words1.begin();
        it!=words1.end();++it)
    {
        cout<<*it<<" ";
    }
    cout<<endl;

    cout<<"The elements of words2 are below:"<<endl;
    for(list<string>::iterator it=words2.begin();it!=words2.end();
        ++it)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    
    /*find midpoint int the vector--words1*/
    vector<string>::iterator mid = words1.begin() + words1.size()/2;
    
    /*initialize vfront with first half of words1*/
    vector<string> vfront(words1.begin(),mid);
    /*initialize vback with second half of words1*/
    vector<string> vback(mid,words1.end());
 
    /*不能直接将一种容器中的元素复制给另一种容器,但系统允许通过一对迭代器间接
      实现该功能*/

    /*initialize dfront with first half of words1*/
    deque<string> dfront(words1.begin(),mid);
    /*initialize dback with second half of words1*/
    deque<string> dback(mid,words1.end());

    cout<<"The elements of vfront are below:"<<endl;
    for(vector<string>::iterator it=vfront.begin();it!=vfront.end();
        ++it)
    {
        cout<<*it<<" ";
    }
    cout<<endl;

    cout<<"The elements of vback are below:"<<endl;
    for(vector<string>::iterator it=vback.begin();it!=vback.end();
        ++it)
    {
        cout<<*it<<" ";
    }
    cout<<endl;

    cout<<"The elements of dfront are below:"<<endl;
    for(deque<string>::iterator it=dfront.begin();it!=dfront.end();
        ++it)
    {
         cout<<*it<<" ";
    }
    cout<<endl;

    cout<<"The elements of dback are below:"<<endl;
    for(deque<string>::iterator it=dback.begin();it!=dback.end();
        ++it)
    {
        cout<<*it<<" ";
    }
    cout<<endl;

    /*25 strings ,each is "love"*/
    list<string> slist(25,"love");
    int count = 0;
    cout<<"The elements of slist are below:"<<endl;
    for(list<string>::iterator it=slist.begin();it!=slist.end();
        ++it)
    {
        cout<<*it<<" ";
        ++count;
        if(count%5 == 0)
        {
            cout<<endl;
        }
    }
// cout<

    return 0;
}

编译并运行程序后的执行结果:
The elements of words1 are below:
first   second   third   fourth   fifth   
The elements of words2 are below:
first   second   third   fourth   fifth   
The elements of vfront are below:
first   second   
The elements of vback are below:
third   fourth   fifth   
The elements of dfront are below:
first   second   
The elements of dback are below:
third   fourth   fifth   
The elements of slist are below:
love   love   love   love   love   
love   love   love   love   love   
love   love   love   love   love   
love   love   love   love   love   
love   love   love   love   love  

阅读(2412) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-12-22 14:33:27

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com