本文介绍了使用向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 请我尝试逐行从文本文件中复制并将 文本以相反的顺序粘贴到另一个文本文件中也是逐行制作 第一行是最后一行,最后一行是第一行。 我尝试使用以下代码中的向量,但它刚赢了不要跑。 请帮助 ----------------------- ------------------- #include< string> #include< iostream> ; #include< fstream> #include< vector> 使用命名空间std; int main(){ vector< string> v; ifstream in(" mtext1.txt"); ofstream out(" mtext2.txt"); string line; while(getline(in,line)){ v.push_back(line); } int j = v.size(); for(int i = 0; i< j; i ++){ out<< v [j]; j = j-1; } } Hi all, Please I tried to copy from a text file line by line and to paste thetext in reverse order in another text file also line by line-Making thefirst line the last and the last line the first. I tried using vectors in the following codes but it just won''t run.please help ------------------------------------------ #include <string>#include <iostream>#include <fstream>#include <vector>using namespace std; int main() {vector<string> v;ifstream in("mtext1.txt");ofstream out("mtext2.txt"); string line; while (getline (in, line) ) {v.push_back(line);}int j = v.size(); for(int i=0; i < j; i++) {out << v[j] ;j=j-1;}} 推荐答案 试试...... for(int j = v.size(); j--> 0;){ 并删除j = j-1; for(int i = 0; i< j; i ++){ try ...for ( int j = v.size(); j-- > 0; ) { and remove the j=j-1; for(int i=0; i < j; i++) { [snip] 使用索引以相反的顺序遍历std :: vector可能会令人困惑。 但是没有需要这样做: 无效 反向() { typedef std ::矢量<的std :: string> t_vec; t_vec v; std :: ifstream in(" mtext1.txt"); std :: ofstream out(" ; mtext2.txt"); std :: string line; while(getline(in,line)) v.push_back(line); for(t_vec :: reverse_iterator i = v.rbegin(); i!= v.rend(); ++ i) out<< * i<< ''\ n''; } [snip] Traversing a std::vector in reverse order using indexes can be confusing.However there is no need to do it that way: voidreverse(){typedef std::vector<std::string> t_vec;t_vec v;std::ifstream in("mtext1.txt");std::ofstream out("mtext2.txt"); std::string line; while (getline(in, line))v.push_back(line); for (t_vec::reverse_iterator i = v.rbegin(); i != v.rend(); ++i)out << *i << ''\n'';} 这篇关于使用向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 08:50