zoukankan      html  css  js  c++  java
  • c++第五版练习9.19 9.20

    练习9.19 编写程序,从标准输入中读取string 序列,存入一个list中,编写循环,用迭代器打印list中的元素

    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;
    int main()
    {
    string word;
    list<string>lst;
    auto iter = lst.begin();
    while (cin >> word)
    {
    lst.emplace_back(word);

    }
    for (auto it = lst.begin(); it != lst.end(); ++it)
    {
    cout << *it << endl;
    }
    return 0;
    }

    练习9.20 编写程序,从一个list<int>拷贝元素到两个deque中,值为偶数的所有元素都拷贝到一个deque中,而奇数值元素都拷贝到另一个deque中。

    #include <iostream>
    #include<list>
    #include<deque>

    using namespace std;
    int main()
    {
    int data;

    list<int>lst;
    deque<int>even;
    deque<int>odd;
    while (cin>>data)
    {
    lst.emplace_back(data);
    }

    /*for (auto it = lst.begin(); it != lst.end(); ++it)//验证lst ok
    {
    cout << *it << endl;
    }*/
    for (auto it = lst.begin(); it != lst.end(); ++it)
    {
    if (*it % 2 == 0)
    {
    even.emplace_back(*it);
    }
    else
    {
    odd.emplace_back(*it);
    }
    }
    cout << "Please printf is even number:" << endl;
    /*for(auto it = even.begin(); it != even.end(); ++it)
    {
    cout << *it << endl;
    }*/
    for (auto it : even)
    {
    cout << it << endl;
    }

    cout << "Please printf is odd number:"<<endl;
    /*for (auto it = odd.begin(); it != odd.end(); ++it)
    {
    cout << *it << endl;
    }*/

    for (auto it : odd)
    {
    cout << it << endl;
    }


    return 0;
    }

  • 相关阅读:
    Git上手:四种常见的Git协同工作方式
    Git上手:Git扫盲区
    理解web缓存
    浅谈对技术债的理解
    保护个人隐私,从我做起
    cookie注意事项
    了解JavaScript核心精髓(二)
    简单实现nodejs爬虫工具
    浅谈我所见的CSS组织风格
    利用正则表达式清除多余的空行
  • 原文地址:https://www.cnblogs.com/whitewn/p/6589865.html
Copyright © 2011-2022 走看看