10.29
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, cjar * argv[])
{
vector<string> vs;
ifstream in(argv[1]);
istream_iterator string_in(in), eof;
while(string_in != eof)
vs.push_back(*string_in++);
return 0;
}
答案
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iterator>
include <algorithm>
using namespace std;
int main(int argc, cjar * argv[])
{
vector<string> words;
ifstream in(argv[1]);
if (!in)
{ cerr << "无法打开文件" << endl;
exit(1);
}
istream_iterator string_in(in), eof;
while(string_in != eof)
vs.push_back(*string_in++);
for (auto word : words)
cout << word << ",";
cout << endl;
return 0;
}
10.30
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
istream_iterator<int> in(cin), eof;
vector<int> vi;
while(in != eof)
vi.push_back(*in++);
sort(vi.begin(), vi.end());
//不会用copy函数输出啊!!!!!!!
copy(
答案(懂了
#include <iostream>
#include <fstream> //么意思,加上fstream
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char *argv[])
{
istream_iterator<int> in(cin), eof;
vector<int> vi;
while(in != eof)
vi.push_back(*in++);
sort(vi.begin(), vi.end());
//不会用copy函数输出啊!!!!!!!
ostream_iterator<int> out_iter(cout, ",");
copy(vi.begin(), vi.end(), out_iter);
return 0;
}
10.31
把copy换成unique_copy即可
10.32
10.33
答案
include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
if (argc != 4)
{
cout << "用法:execise.exe in_file"
"out_file1 out_file2" <<endl;
return -1;
}
ifstream in(argv[1])
{
cerr << "无法打开输入文件" << endl;
exit(1);
}
ofstream out1(argv[2])
if (!out1)
{
cout<<"打开输出文件1失败" <<endl;
exit(1);
}
ofstream out2(argv[3])
if (!out2)
{
cout<<"打开输出文件2失败" <<endl;
exit(1);
}
istream_iterator<int> in_iter(in), eof;
ostrema_iterator<int> out_iter1(out1," ");
ostrema_iterator<int> out_iter2(out2,"\n");
while(in_iter != eof)
{
if(*in_iter & 1)
*out_iter1++ = *in_iter;
else
*out_iter2++ = *in_iter;
++in_iter;
}
return 0;
}
10.34 10.35
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
void output_words(const vector<string> &words)
{
cout << "普通逆序打印" <<endl;
for (auto iter = words.end()-1 ; iter >= words.begin() ; --iter)
cout << *iter << ";";
cout <<endl;
cout << "逆向迭代器逆向打印" <<endl;
for (auto iter = words.rbegin(); iter != words.rend() ; ++iter)
cout <<*iter<<",";
cout <<endl;
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "无法打开文件" <<endl;
exit(1);
}
vector<string> words;
string word;
while(in>>word)
words.push_back(word);
output_words(words);
return 0;
}
答案
在普通迭代器逆序打印中,它的for循环为:
for (auto iter = words.cend ; iter != words.begin(); )
cout << *(--iter) << " "; //先减减,后解引用,神奇
cout << endl;
10.36
逆序查找呗
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
list<int> l = {1, 2 , 0,3, 4, 5, 0, 5 , 9 , 8 ,7};
auto iter = find(l.crbegin(), l.crend(), 0);
if(iter != l.crend())
cout << *iter << endl;
else
cout << "无" <<endl;
return 0;
}
答案