10.9
答案
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
inline void output_words(vector<string> &v)
{
for(auto iter = v.begin(); iter != v.end(); ++iter)
cout << *iter << " " ;
cout << endl;
}
void elimdups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开文件" << endl;
exit(1);
}
vector<string> v1;
string s;
while (in>>s)
v1.push_back(s);
elimdups(words);
return 0;
}
10.10
算法不会执行容器操作
答案的
泛型算法的一大优点就是“泛型”,一个算法可用于多种不同的数据类型,算法与所操作的数据结构分离。这对编程效率的提高是巨大的。
要做到分离,重要的技术手段就是使用迭代器作为俩者的桥梁。算法不操作具体的容器,从而也不会和特定的容器绑定,算法只操作迭代器,有迭代器真正实现对容器的访问。算法根本不知道容器的存在。是否要改变容器的大小时迭代器的责任
10.11
#include <isotream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
inline void output_words(const string &s)
{
for(auto iter = s.begin(); iter != s.end(); ++iter)
cout << *iter <<" " ;
cout << endl;
}
void elimdups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
stable_sort(words.begin(), words.end());
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开文件" << endl;
exit(1);
}
string word;
vector<string> v;
while (in >> word)
{
v.push_back(word);
}
elimdups(v);
for (const auto &i : s)
cout << i << " " ;
cout << endl;
return 0;
}
10.12
答案
inline bool compareisbn(const Sales_data &lhs, const Sales_data rhs)
{
return lhs.isbn() < rhs.isbn();
}
include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algoriyhm>
#include "Sales_data.h"
using namespace std;
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开文件" << endl;
exit(1)'
}
vector<Sales_data> sdv;
Sales_data sd;
while(read(in, sd))
sdv.push_back(sd);
sort(sdv.begin(), sdv.end(), compareisbn);
for (const auto &i : sdv )
{
print(cout , s);
cout << endl;
}
return 0
}
10.13
一
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
inline void input(vector<string> &s)
{
string ss;
while (cin>>ss)
s.push_back(ss);
}
inline void output(vector<string> &s)
{
for(auto iter = s.begin(); iter != s.end() ; ++iter)
cout << *iter << " " ;
cout << endl;
}
bool check(const string &s)
{
bool b = false;
if (s.size() >= 5)
b = true;
return b;
}
int main(int argc, char *argv[])
{
vector<string> s;
input(s);
/*ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开文件" << endl;
exit(1);
}*/
string s;
vector<string> vs;
while(in >>s)
vs.push_back(s);
auto end_p = partition(vs.begin(), vs.end(), check);
vs = vs.erase(end_p, vs.end);
output(vs);
return 0;
}
二
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
inline void input(vector<string> &s)
{
string ss;
while (cin>>ss)
s.push_back(ss);
}
inline void output(vector<string> &s)
{
for(auto iter = s.begin(); iter != s.end() ; ++iter)
cout << *iter << " " ;
cout << endl;
}
bool check(const string &s)
{
bool b = false;
if (s.size() >= 5)
b = true;
return b;
}
int main(int argc, char *argv[])
{
vector<string> vs;
input(vs);
output(vs);
/*ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开文件" << endl;
exit(1);
}
string s;
vector<string> vs;
while(in >>s)
vs.push_back(s);*/
auto end_p = partition(vs.begin(), vs.end(), check);
// auto iter1 = vs.end();
vs.erase(end_p, vs.end());
output(vs);
return 0;
}
答案的
跟一类似
check()
bool check(const string &s1)
{
return s1.size() >= 5;
}