练习9.20
#include<iostream>
#include<list>
#include<deque>
int main()
{
using namespace std;
list<int> ilist;
deque<int>ideq1;
deque<int>ideq2;
int temp;
while (cin >> temp) {
ilist.push_back(temp);
}
auto it = ilist.begin();
for(;it!=ilist.end();++it)
{
if (*it % 2 == 0)
ideq1.push_back(*it);
else
ideq2.push_back(*it);
}
for (auto i : ideq1)
{
cout << i<<'\n';
}
for (auto j : ideq2)
{
cout << j << '\n';
}
return 0;
}
练习9.26
#include<iostream>
#include<list>
#include<vector>
int main()
{
using namespace std;
vector<int> vec;
list<int> l;
int ia[] = { 0,1,1,2,3,5,8,13,21,55,89 };
vec.assign(ia, ia + 11);
l.assign(ia, ia + 11);
for(auto beg = vec.begin();beg != vec.end();)
{
if (*beg % 2 == 0)
beg = vec.erase(beg);
else
++beg;
}
for (auto beg = l.begin(); beg != l.end(); )
{
if (*beg % 2==1)
beg = l.erase(beg);
else
++beg;
}
cout << "奇数:" << endl;
for (auto i : vec)
{
cout <<i<<'\n';
}
cout << "偶数" << endl;
for (auto j : l)
{
cout << j << '\n';
}
return 0;
}
练习9.27
#include<iostream>
#include<forward_list>
#include<vector>
int main()
{
using namespace std;
vector<int>vec;
int temp;
while (cin >> temp)
{
vec.push_back(temp);
}
forward_list<int> flist(vec.begin(),vec.end());
auto prev = flist.before_begin();
auto curr = flist.begin();
while(curr!=flist.end())
{
if (*curr % 2 == 1)
curr = flist.erase_after(prev);
else
{
prev = curr;
++curr;
}
}
return 0;
}
练习9.47
#include<iostream>
#include<string>
int main()
{
using namespace std;
string str("ab2c3d7R4E6");
string number("0123456789");
int pos = 0;
while ((pos = str.find_first_of(number, pos)) != string::npos)
{
cout << "found number at index:" << pos << "element is:" << str[pos] << endl;
++pos;
}
pos = 0;
while ((pos = str.find_first_not_of(number, pos)) != string::npos)
{
cout << "found character at index:" << pos << "element is:" << str[pos] << endl;
++pos;
}
return 0;
}
练习9.49
#include<iostream>
#include<string>
#include<fstream>
int main()
{
using namespace std;
ifstream ifile("shishang.txt");
string str("acemnorsuvwxz");
string word, maxword;
string::size_type maxsize = 0;
while (ifile >> word)
{
if ((word.find_first_not_of(str) == string::npos) && (word.size() > maxsize))
{
maxsize = word.size();
maxword = word;
}
}
ifile.close();
cout << word << endl;
return 0;
}