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

  • 相关阅读:
    tmux 的基本使用
    ffmpeg(1) 基础框架
    VUE页面跳转方式
    nextcloud 中文乱码解决方案
    mysql8 navicat远程链接失败
    prometheus+grafana实现服务监控
    sqlalchemy ————关联表
    Python flask自定义异常信息,返回json格式的异常
    sqlalchemy 查询结果转json个人解决方案
    Linux添加字体
  • 原文地址:https://www.cnblogs.com/whitewn/p/6589865.html
Copyright © 2011-2022 走看看