gpt4 book ai didi

c++ - 对具有 55K 行和不同列的文件进行排序

转载 作者:行者123 更新时间:2023-11-30 03:11:06 26 4
gpt4 key购买 nike

我想找到一个使用 C++ 的编程解决方案。

我有 900 个文件,每个文件大小为 27MB。 (只是为了告知其严重性)。

每个文件有 55K 行和可变列。但是标题表示列

我想按 w.r.t 到列值的顺序对行进行排序。

我为此编写了排序算法(您可能会说这绝对是我的新手尝试)。该算法适用于少数数字,但不适用于较大的数字。

这是相同的代码:我定义在主要代码中使用的基本功能:

int getNumberOfColumns(const string& aline)
{
int ncols=0;
istringstream ss(aline);
string s1;
while(ss>>s1) ncols++;
return ncols;
}

vector<string> getWordsFromSentence(const string& aline)
{
vector<string>words;
istringstream ss(aline);
string tstr;
while(ss>>tstr) words.push_back(tstr);
return words;
}

bool findColumnName(vector<string> vs, const string& colName)
{
vector<string>::iterator it = find(vs.begin(), vs.end(), colName);
if ( it != vs.end())
return true;
else return false;
}

int getIndexForColumnName(vector<string> vs, const string& colName)
{
if ( !findColumnName(vs,colName) ) return -1;
else {
vector<string>::iterator it = find(vs.begin(), vs.end(), colName);
return it - vs.begin();
}
}

////////// I like the Recurssive functions - I tried to create a recursive function
///here. This worked for small values , say 20 rows. But for 55K - core dumps
void sort2D(vector<string>vn, vector<string> &srt, int columnIndex)
{
vector<double> pVals;
for ( int i = 0; i < vn.size(); i++) {
vector<string>meancols = getWordsFromSentence(vn[i]);
pVals.push_back(stringToDouble(meancols[columnIndex]));
}

srt.push_back(vn[max_element(pVals.begin(), pVals.end())-pVals.begin()]);
if (vn.size() > 1 ) {
vn.erase(vn.begin()+(max_element(pVals.begin(), pVals.end())-pVals.begin()) );
vector<string> vn2 = vn;
//cout<<srt[srt.size() -1 ]<<endl;
sort2D(vn2 , srt, columnIndex);
}
}

现在主要代码:

 for ( int i = 0; i < TissueNames.size() -1; i++)
{
for ( int j = i+1; j < TissueNames.size(); j++)
{
//string fname = path+"/gse7307_Female_rma"+TissueNames[i]+"_"+TissueNames[j]+".txt";
//string fname2 = sortpath2+"/gse7307_Female_rma"+TissueNames[i]+"_"+TissueNames[j]+"Sorted.txt";
string fname = path+"/gse7307_Male_rma"+TissueNames[i]+"_"+TissueNames[j]+".txt";
string fname2 = sortpath2+"/gse7307_Male_rma"+TissueNames[i]+"_"+TissueNames[j]+"4Columns.txt";
vector<string>AllLinesInFile;
BioInputStream fin(fname);
string aline;
getline(fin,aline);
replace (aline.begin(), aline.end(), '"',' ');
string headerline = aline;
vector<string> header = getWordsFromSentence(aline);

int pindex = getIndexForColumnName(header,"p-raw");
int xcindex = getIndexForColumnName(header,"xC");
int xeindex = getIndexForColumnName(header,"xE");
int prbindex = getIndexForColumnName(header,"X");

string newheaderline = "X\txC\txE\tp-raw";
BioOutputStream fsrt(fname2);
fsrt<<newheaderline<<endl;

int newpindex=3;
while ( getline(fin, aline) ){

replace (aline.begin(), aline.end(), '"',' ');
istringstream ss2(aline);
string tstr;
ss2>>tstr;
tstr = ss2.str().substr(tstr.length()+1);
vector<string> words = getWordsFromSentence(tstr);
string values = words[prbindex]+"\t"+words[xcindex]+"\t"+words[xeindex]+"\t"+words[pindex];
AllLinesInFile.push_back(values);
}

vector<string>SortedLines;
sort2D(AllLinesInFile, SortedLines,newpindex);

for ( int si = 0; si < SortedLines.size(); si++)
fsrt<<SortedLines[si]<<endl;
cout<<"["<<i<<","<<j<<"] = "<<SortedLines.size()<<endl;
}
}

有人可以建议我更好的方法吗?为什么它无法获得更大的值(value)。 ?

此查询感兴趣的主要函数是 Sort2D 函数。

感谢您的耐心等待。

普拉萨德。

最佳答案

我不确定您的代码为什么会崩溃,但在这种情况下递归只会降低代码的可读性。但是,我怀疑这是堆栈溢出,因为您在每次调用中都没有使用太多堆栈空间。

C++ 已经有 std::sort ,为什么不用它呢?你可以这样做:

// functor to compare 2 strings
class CompareStringByValue : public std::binary_function<string, string, bool>
{
public:
CompareStringByValue(int columnIndex) : idx_(columnIndex) {}
bool operator()(const string& s1, const string& s2) const
{
double val1 = stringToDouble(getWordsFromSentence(s1)[idx_]);
double val2 = stringToDouble(getWordsFromSentence(s2)[idx_]);
return val1 < val2;
}
private:
int idx_;
};

然后对您要调用的线路进行排序

std::sort(vn.begin(), vn.end(), CompareByStringValue(columnIndex));

现在,有一个问题。这会很慢,因为 stringToDoublegetWordsFromSentence在同一个字符串上被多次调用。您可能希望生成一个单独的 vector ,该 vector 已经预先计算了每个字符串的值,然后有 CompareByStringValue只需将该 vector 用作查找表即可。

另一种方法是将字符串插入 std::multimap<double, std::string> 中.只需将条目插入为 (value, str)然后逐行读出来。这更简单但更慢(尽管具有相同的 big-O 复杂性)。

编辑:清理了一些不正确的代码并派生自 binary_function .

关于c++ - 对具有 55K 行和不同列的文件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2541608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com