问题描述
解决方案
#include<iostream>#include<vector>using namespace std;int main(){ vector<double> vec_num; vec_num.push_back(1.1); vec_num.push_back(1.5); vec_num.push_back(1.2); vector<double>::reverse_iterator re_it; for(re_it = vec_num.rbegin(); re_it != vec_num.rend(); re_it++) cout << *re_it << endl; system(""pause"");}
反向迭代器reverse_iterator,从rbegin(=end)到rend(=begin-1)反向遍历,仍然通过++.
解决方案二:
#include<iostream>#include<fstream>#include<string>#include<vector>#include<iterator>using namespace std;void WriteTxt(const char *in_filename const char *out_filename_1 const char *out_filename_2){ ifstream ifs(in_filename); ofstream out_file_1(out_filename_1); ofstream out_file_2(out_filename_2); istream_iterator<string> ibeg(ifs); istream_iterator<string> iend; vector<string> text; copy(ibeg iend back_inserter(text)); vector<string> out_text_1; vector<string> out_text_2; vector<string>::iterator it; for(it = text.begin(); it != text.end(); it++) { if(atoi((*it).c_str()) % 2 == 1) out_text_1.push_back(*it + ""n""); else out_text_2.push_back(*it + "" ""); } ostream_iterator<string> os_1(out_file_1 ""); ostream_iterator<string> os_2(out_file_2 ""); copy(out_text_1.begin() out_text_1.end() os_1); copy(out_text_2.begin() out_text_2.end() os_2);}int main(){ WriteTxt(""E:\data.txt""E:\out_1.txt""E:\out_2.txt""); system(""pause"");}
我的E:data.txt里面有数字
13
22
5
24
解决方案三:
这家伙真的太懒了,字都不码一下
时间: 2024-09-10 18:04:08