C++ 迭代器使用小例

/********************************************************************

    created:   2012/08/15   9:19

    fileName:  main.cpp

    author:       Toby

   

    purpose:   迭代器:

              1.输入流迭代器,对应实例:testIstreamFun*

              2.输出流迭代器,对应实例:testOstreamFun*

              3.插入迭代器

              的使用小例子

*********************************************************************/

#include <iostream>

#include <iomanip>

#include <string>

#include <map>

#include <vector>

#include <numeric>   ///< for accumulate() 求和

#include <sstream>   /// typedef basic_istringstream<char>

#include <algorithm>///< for copy()

#include <list>

 

using std::cout;

using std::cin;

using std::endl;

using std::vector;

using std::string;

 

void testIstreamFun0(){

    /************************************************************************/

    /*                     *读入整数,输出全部读入的整数的和*                    */

    /************************************************************************/

    cout<<"Enter integers separated by spaces then a letter to end: "<<endl;

    ///< 注意:上面的提示文字应放在定义输入流迭代器的前面,否则将看不到提示文字。

    std::istream_iterator<int> numbersIn(cin) , numbersEnd;

 

    vector<int> numbers;

    while(numbersIn != numbersEnd)

       numbers.push_back(*numbersIn++);

 

    cout<<"The sum of the input values that you entered is "

       <<accumulate(numbers.begin(),numbers.end(),0)<<endl;

 

}

 

void testIstreamFun1(){

    /************************************************************************/

    /*                        *记录并输出输入的单词的个数*                     */

    /************************************************************************/

    typedef std::map<string,int>::const_iterator Iter;

    std::map<string,int> words;

    cout<<"Enter some text and press Enter followed by Ctrl + Z to end: "

       <<endl<<endl;

    std::istream_iterator<string> begin(cin);

    std::istream_iterator<string> end;///< 调用默认构造,返回end-of-stream迭代器

 

    while(begin != end)      ///< 其实只要了解*begin++返回一个string类型的key

       words[*begin++]++;   ///< 就可以很容易理解这个循环完成的计数操作了

                        

    cout<<endl<<"Here are the word counts for the text you entered: "

       <<endl;

    for(Iter iter = words.begin(); iter != words.end(); ++iter)

       cout<<std::setw(5)<<iter->second<<" "<<iter->first<<endl;

}

 

void testIstreamFun2(){

    /************************************************************************/

    /*使用basic_istringstream<char>访问流缓冲区中的数据的对象类型,如string对象。

    在头文件sstreamistringstream定义为basic_istringstream<char>,它将是char类型

    的字符流。我们可以从string对象中构造一个istringstream对象,这意味着从string对象中

    读取数据,就像从cin中读取数据那样。因为istringstream<T>是一个流,所以可以将它传递

    给一个输入迭代器构造函数,并用该迭代器访问底层流缓冲区中的数据。*/

    /************************************************************************/

    string data("2.4 2.5 3.6 2.1 6.7 6.8 94 95 1.1 1.4 32");

    cout<<"Current data string is:"<<endl

       <<data<<endl;

    std::istringstream input(data);///< 如果未使用using命令,则不要忘记前面的std::

    std::istream_iterator<double> begin(input),end;

    cout<<"The sum of the values from the data string is "

       <<accumulate(begin,end,0.0)<<endl;

}

 

void testOstreamFun0(){

    using std::list;

 

    int data[] = {1,2,3,4,5,6,7,8,9};

    vector<int> numbers(data,data+9);

 

    list<int> numsbak;///< 将输入插入到容器前段,vector容器不可以插入前端

    copy(numbers.begin(),numbers.end(),std::front_inserter(numsbak));

 

    std::ostream_iterator<int> out(cout);

    copy(numsbak.begin(),numsbak.end(),out);

    cout<<endl;

}

 

void testOstreamFun1(){

    int data[] = {1,2,3,4,5,6,7,8,9};

    vector<int> numbers(data,data+9);

    std::ostream_iterator<int> out(cout,", ");

    copy(numbers.begin(),numbers.end(),out);

    cout<<endl;

}

 

void testOstreamFun2(){

    vector<int> numbers;

 

    cout<<"Enter a series of integers separated by spaces"

       <<" followed by Ctrl+Z or a letter:"<<endl;

    std::istream_iterator<int> input(cin),end;

    copy(input,end,std::back_inserter(numbers));///< 将输入插入到容器尾部

 

    cout<<"You entered the following values:"<<endl;

    std::ostream_iterator<int> out(cout," ");

    copy(numbers.begin(),numbers.end(),out);

 

    cout<<endl

       <<"The sum of the values is "

       <<accumulate(numbers.begin(),numbers.end(),0)<<endl;

}

 

int main(void)

{

    //testIstreamFun0();

    //testIstreamFun1();

    //testIstreamFun2();

 

    //testOstreamFun0();

    //testOstreamFun1();

    testOstreamFun2();

 

    system("pause");

    return 0;

}

 

转载于:https://my.oschina.net/imtoby/blog/72885

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值