• 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;
    }

  • 相关阅读:
    阐述:SIP协议是什么
    【SIP协议】学习初学笔记
    【协议学习】SIP基本场景分析
    电话的前世今生
    深入浅出SIP协议
    QVariant类及QVariant与自定义数据类型转换的方法
    Qt中如何根据类名来实例化对象
    模板的全特化与偏特化
    为什么c++中,有时可以用类名直接访问非静态成员函数?
    C++引用详解
  • 原文地址:https://www.cnblogs.com/whitewn/p/6589865.html
Copyright © 2020-2023  润新知