使用向量 [英] Using Vectors

查看:63
本文介绍了使用向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


请我尝试逐行从文本文件中复制并将

文本以相反的顺序粘贴到另一个文本文件中也是逐行制作

第一行是最后一行,最后一行是第一行。


我尝试使用以下代码中的向量,但它刚赢了不要跑。

请帮助


----------------------- -------------------


#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 the
text in reverse order in another text file also line by line-Making the
first 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;
}
}

推荐答案

edward_nig写道:
edward_nig wrote:
大家好,

请我尝试逐行从文本文件中复制并粘贴
文本以相反的顺序在另一个文本文件中逐行 - 使第一行成为最后一行,最后一行是第一行。

我尝试使用以下代码中的向量但它只是不会跑。
请帮助

---------------------------- --------------

#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();


int j = v.size() - 1;

for(int i = 0; i< j; i ++){


for语句毫无意义。

out<< v [j];
j = j-1;
}
}
Hi all,

Please I tried to copy from a text file line by line and to paste the
text in reverse order in another text file also line by line-Making the
first 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();
int j = v.size() - 1;

for(int i=0; i < j; i++) {
the for statement makes no sense.
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++) {





它仍然不会工作。它将文本逐行传输到第二个

文件,但它仍然没有反过来。

Hi,

It still won''t work. It transfers the texts line by line to the second
file, but it still wasn''t done in reverse other.




" edward_nig" < ED *********** @ gmail.com>在消息中写道

新闻:11 ********************** @ i39g2000cwa.googlegr oups.com ...

"edward_nig" <ed***********@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
大家好,

请我尝试逐行从文本文件中复制并将
文本以相反顺序粘贴到另一个文本文件中 - 也可以逐行
第一行是最后一行,最后一行是第一行。

我尝试使用以下代码中的向量,但它不会运行。
请帮助
Hi all,

Please I tried to copy from a text file line by line and to paste the
text in reverse order in another text file also line by line-Making the
first 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



[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:

void
reverse()
{
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'';
}


这篇关于使用向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆