10.26
back_inserter ,创建一个使用push_back的迭代器
front_inserter创建一个使用push_front的迭代器
inserter创建一个使用inserter的迭代器
10.27
int main(){
vector<int > beCopy = {1,2,3,4,4,5,6,7,5,6,7,8};
list<int > list_0(beCopy.size());
sort(beCopy.begin(),beCopy.end());
unique_copy(beCopy.begin(),beCopy.end(),list_0.begin());
for_each(list_0.cbegin(), list_0.cend(),[](int i){ cout << i << endl;});
for (;;);
return 0;
}
10.28
inserter 和 back_inserter为123456789
front_inserter为987654321
10.29
int main(){
ifstream file("test_2.txt");
istream_iterator<string > in(file), eof;
vector<string > vec;
while (in != eof)
{
vec.push_back(*in++);
}
for_each(vec.cbegin(),vec.cend(),[](const string& s){cout << s << endl;});
for (;;);
return 0;
}
10.30
10.31
10.32
略
10.33
ostream_iterator输出完之后,一定要刷新缓冲区!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
否则无法正确写入
void writeOddAndEven(ofstream& oddStream, ofstream& evenStream, ifstream& in){
istream_iterator<string > isin(in),eof;
ostream_iterator<string > oOs(oddStream, " ");
ostream_iterator<string > eOs(evenStream, " ");
while (isin != eof)
{
if(stoi(*isin) %2){
*oOs++ = *isin++;
}else{
*eOs++ = *isin++;
}
}
oddStream << flush;
evenStream << flush;
}
int main(){
ifstream inFile("val.txt");
ofstream odd("odd.txt");
ofstream even("even.txt");
writeOddAndEven(odd, even, inFile);
for (;;);
return 0;
}
10.34
int main(){
vector<int > vec = {1,2,3,4,5,6,7,8};
for(vector<int >::const_reverse_iterator it = vec.crbegin(); it != vec.crend(); it++){
cout << *it << ends;
}
for (;;);
return 0;
}
10.35
int main(){
vector<int > vec = {1,2,3,4,5,6,7,8};
for(vector<int >::iterator it = vec.end()-1; ; --it){
cout << *it << ends;
if(it == vec.begin()){
break;
}
}
for (;;);
return 0;
}
10.36
int main(){
list<int > listInt = {1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1,0,12};
list<int >::const_reverse_iterator it = find(listInt.crbegin(),listInt.crend(),0);
cout << *it << endl;
for (;;);
return 0;
}
10.37
int main(){
vector<int > vecInt = {1,2,3,4,5,6,7,8,9,10};
list<int > listInt(4);
copy(vecInt.rbegin()+3, vecInt.rbegin()+7, listInt.begin());
for_each(listInt.cbegin(),listInt.cend(),[]( const int & i){cout << i << ends;});
for (;;);
return 0;
}