vector< string> .clear()现在正在工作 - 请帮助 [英] vector<string>.clear() now working -- please help

查看:52
本文介绍了vector< string> .clear()现在正在工作 - 请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我有一个基本上反转文件内容的程序,除了

第一行。


它编译得很好,但在运行时给了我核心转储。如果我发表评论

temp.clear()它运行正常,但我需要它来清除每个文件

的临时矢量。


*********************代码*******************

#include< fstream>

#include< string>

#include< vector>

#include< iostream>


使用命名空间std;


//这个程序只是反转tickers.csv文件execpt第一行

int main(){


string ticker,line;

字符串输入,输出;

ifstream Tickers(" tickers。 txt",ios :: in);

ifstream Input_File;

ofstream Output_File;

vector< string> temp;


while(Tickers>> ticker){

input =" tempdata /" + ticker +" .csv";

output =" tempdata /" + ticker +" 1.csv";

Input_File.open(input.c_str(),ios :: in);

Output_File.open(output.c_str( ),ios :: app);

while(Input_File>> line){

temp.push_back(line);

}

Output_File<< temp [0]<< endl;

for(unsigned i =(temp.size() - 1); i> 0; i--)

Output_File<< temp [i]<< endl;

Input_File.close();

Output_File.close();

temp.clear();

}

返回0;

}

******************* ********************


tickers.txt包含部分文件名。 tempdata是一个子目录。我想要在tempdata中存在要倒置的文件。


提前谢谢。

Hello

I have a program that basically inverts the contents of files except
first line.

It compiles fine but gives me core dump on running. If i comment
temp.clear() it runs fine, but i need it to clear the temp vector for
each file.

********************* code *******************
#include <fstream>
#include <string>
#include <vector>
#include <iostream>

using namespace std;

// This program just inverts the tickers.csv files execpt first line
int main(){

string ticker, line;
string input,output;
ifstream Tickers( "tickers.txt", ios::in);
ifstream Input_File;
ofstream Output_File;
vector <string> temp;

while(Tickers>>ticker){
input = "tempdata/" + ticker + ".csv";
output = "tempdata/" + ticker + "1.csv";
Input_File.open( input.c_str(), ios::in);
Output_File.open ( output.c_str(), ios::app);
while ( Input_File >> line ){
temp.push_back(line);
}
Output_File << temp[0] << endl;
for ( unsigned i = ( temp.size() - 1 ); i > 0; i--)
Output_File << temp[i] << endl;
Input_File.close();
Output_File.close();
temp.clear();
}
return 0;
}
***************************************

tickers.txt contains part of file name. tempdata is a subdirectory. i
have the files to be inverted present in tempdata.

thank you in advance.

推荐答案

Gaurav写道:
Gaurav wrote:
你好

我有一个基本上反转文件内容的程序,除了第一行。

它编译得很好但是在运行时给了我核心转储。如果我评论
temp.clear()它运行正常,但我需要它来清除每个文件的临时矢量。

********* ************代码*******************
#include< fstream>
#include< string>
#include< vector>
#include< iostream>
#include< algorithm>
使用命名空间std;

//这个程序只是反转tickers.csv文件execpt第一行
int main(){

字符串自动收录器,行;
字符串输入,输出;
ifstream Tickers(" tickers.txt",ios :: in);
ifstream Input_File;
ofstream Output_File;
vector< string> temp;

while(Tickers>> ticker){
input =" tempdata /" + ticker +" .csv";
output =" tempdata /" + ticker +" 1.csv";
Input_File.open(input.c_str(),ios :: in);
Output_File.open(output.c_str(),ios :: app);
while(Input_File>> line){
while(std :: getline(Input_File,line))

temp.push_back(line);
}
Output_File<< temp [0]<< ENDL;
for(unsigned i =(temp.size() - 1); i> 0; i--)
Output_File<< temp [i]<< ENDL;
std :: copy(temp.rbegin(),temp.rend() - 1,std :: ostream_iterator(Output_File));

Input_File.close();
Output_File.close();
temp.clear();
}
返回0;
}
************* **************************

tickers.txt包含部分文件名。 tempdata是一个子目录。我将这些文件倒置在tempdata中。

提前谢谢。
Hello

I have a program that basically inverts the contents of files except
first line.

It compiles fine but gives me core dump on running. If i comment
temp.clear() it runs fine, but i need it to clear the temp vector for
each file.

********************* code *******************
#include <fstream>
#include <string>
#include <vector>
#include <iostream> #include <algorithm>
using namespace std;

// This program just inverts the tickers.csv files execpt first line
int main(){

string ticker, line;
string input,output;
ifstream Tickers( "tickers.txt", ios::in);
ifstream Input_File;
ofstream Output_File;
vector <string> temp;

while(Tickers>>ticker){
input = "tempdata/" + ticker + ".csv";
output = "tempdata/" + ticker + "1.csv";
Input_File.open( input.c_str(), ios::in);
Output_File.open ( output.c_str(), ios::app); while ( Input_File >> line ){ while ( std::getline(Input_File, line) )
temp.push_back(line);
}
Output_File << temp[0] << endl; for ( unsigned i = ( temp.size() - 1 ); i > 0; i--)
Output_File << temp[i] << endl; std::copy(temp.rbegin(), temp.rend() - 1, std::ostream_iterator(Output_File));
Input_File.close();
Output_File.close();
temp.clear();
}
return 0;
}
***************************************

tickers.txt contains part of file name. tempdata is a subdirectory. i
have the files to be inverted present in tempdata.

thank you in advance.




除了getline(在如果你在一条线上有空白,那对我来说看起来还行......

当然,我不是大师...



Other than the getline (in case you have blanks in a line), it looks OK to me...
Of course, I''m not a guru...


2003年10月8日10:27:40 -0700, ba********@hotmail.com (Gaurav)写道:
On 8 Oct 2003 10:27:40 -0700, ba********@hotmail.com (Gaurav) wrote:
你好

我有一个基本上反转文件内容的程序,除了第一行。

它编译得很好,但在运行时给了我核心转储。如果我评论
temp.clear()它运行正常,但我需要它来清除每个文件的临时矢量。

********* ************代码*******************
#include< fstream>
#include< string>
#include< vector>
#include< iostream>

使用命名空间std;

//这个程序只是反转了代码.csv文件execpt第一行
int main(){
字符串自动收报机,行;
字符串输入,输出;
ifstream Tickers(" tickers.txt" ,ios :: in);
ifstream Input_File;
ofstream Output_File;
vector< string>温度;


你应该不习惯在函数顶部声明你的变量

。在C ++中没有必要,它使得代码更难以读取。另外,这意味着你不能在

构建时初始化它们。

while(Tickers>> ticker){
input =" tempdata / " + ticker +" .csv";
output =" tempdata /" + ticker +" 1.csv";
Input_File.open(input.c_str(),ios :: in);
Output_File.open(output.c_str(),ios :: app);
while(Input_File>> line){
temp.push_back(line);
}


不应该是:

while(getline(Input_File,line)){

temp.push_back(line);

}

Output_File << temp [0]<< endl;
for(unsigned i =(temp.size() - 1); i> 0; i--)
Output_File<< temp [i]<< endl;
Hello

I have a program that basically inverts the contents of files except
first line.

It compiles fine but gives me core dump on running. If i comment
temp.clear() it runs fine, but i need it to clear the temp vector for
each file.

********************* code *******************
#include <fstream>
#include <string>
#include <vector>
#include <iostream>

using namespace std;

// This program just inverts the tickers.csv files execpt first line
int main(){

string ticker, line;
string input,output;
ifstream Tickers( "tickers.txt", ios::in);
ifstream Input_File;
ofstream Output_File;
vector <string> temp;
You should get out of the habit of declaring your variables at the top
of the functions. There is not need in C++, and it makes code much
less readable. In addition, it means you can''t initialize them at
construction.

while(Tickers>>ticker){
input = "tempdata/" + ticker + ".csv";
output = "tempdata/" + ticker + "1.csv";
Input_File.open( input.c_str(), ios::in);
Output_File.open ( output.c_str(), ios::app);
while ( Input_File >> line ){
temp.push_back(line);
}
Shouldn''t that be:
while (getline(Input_File, line)){
temp.push_back(line);
}
Output_File << temp[0] << endl;
for ( unsigned i = ( temp.size() - 1 ); i > 0; i--)
Output_File << temp[i] << endl;




更好(使用< algorithm>和< iterator>标题):


std: :copy(temp.rbegin(),temp.rend(),

std :: ostream_iterator< std :: string>(Output_File," \ n"));


这也有一个好处,即当temp.size()为0时不会崩溃,

我怀疑是你的问题。


这是完整的程序。请注意,由于仅在需要时才声明变量,因此它的长度要短得多




#include< fstream>

#include< string>

#include< vector>

#include< algorithm>

#include< iterator> ;

#include< iostream>


使用命名空间std;


//这个程序只是颠倒了tickers.csv文件execpt第一行

int main(){


ifstream Tickers(" tickers.txt",ios :: in);

string ticker;


while(Tickers>> ticker){

string input =" tempdata /" + ticker +" .csv";

string output =" tempdata /" + ticker +" 1.csv";

ifstream Input_File(input.c_str(),ios :: in);

ofstream Output_File(output.c_str(), ios :: app);

vector< string> temp;

while(Input_File>> line){

temp.push_back(line);

}

std :: copy(

temp.rbegin(),

temp.rend(),

std :: ostream_iterator< string> (Output_File," \ n")

);

}

返回0;

}


Tom



Better would be (using the <algorithm> and <iterator> headers):

std::copy(temp.rbegin(), temp.rend(),
std::ostream_iterator<std::string>(Output_File, "\n"));

This also has the benefit of not crashing when temp.size() is 0, which
I suspect is your problem.

Here''s the complete program. Notice that it is quite a lot shorter
thanks to declaring the variables only once they are needed:

#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

using namespace std;

// This program just inverts the tickers.csv files execpt first line
int main(){

ifstream Tickers( "tickers.txt", ios::in);
string ticker;

while(Tickers>>ticker){
string input = "tempdata/" + ticker + ".csv";
string output = "tempdata/" + ticker + "1.csv";
ifstream Input_File( input.c_str(), ios::in);
ofstream Output_File( output.c_str(), ios::app);
vector <string> temp;
while ( Input_File >> line ){
temp.push_back(line);
}
std::copy(
temp.rbegin(),
temp.rend(),
std::ostream_iterator<string>(Output_File, "\n")
);
}
return 0;
}

Tom




" tom_usenet" <到******** @ hotmail.com>在消息中写道

news:4c ******************************** @ 4ax.com ...

"tom_usenet" <to********@hotmail.com> wrote in message
news:4c********************************@4ax.com...
int main(){
字符串自动收报机,行;
字符串输入,输出;
ifstream Tickers(" tickers.txt",ios :: in);
ifstream Input_File;
ofstream Output_File;
vector< string> temp;
int main(){

string ticker, line;
string input,output;
ifstream Tickers( "tickers.txt", ios::in);
ifstream Input_File;
ofstream Output_File;
vector <string> temp;



你应该养成在函数顶部声明变量的习惯。在C ++中没有必要,它使代码的可读性更低。另外,这意味着你不能在
施工中初始化它们。



You should get out of the habit of declaring your variables at the top
of the functions. There is not need in C++, and it makes code much
less readable. In addition, it means you can''t initialize them at
construction.




说什么?



Say what?


这篇关于vector&lt; string&gt; .clear()现在正在工作 - 请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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