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