[英]Should "if (argc < 2 || argc > 2)" be fine with 2 arguments? & terminate called after throwing an instance of 'std::out_of_range' error
Solution: Use if (argc !=3)
and getline(row_as_stringstream, substr, ';')
解决方案:使用
if (argc !=3)
和getline(row_as_stringstream, substr, ';')
This is a continuation of my previous question: error: no matching function for call to , which allowed the code to compile without any errors这是我上一个问题的延续: 错误:没有匹配的函数调用 to ,这允许代码编译而没有任何错误
I am new to C++ and I am trying to debug this code from my supervisor.我是 C++ 的新手,我正在尝试从我的主管那里调试这段代码。
The original code took a csv
file as input, containing rows and columns of integers
and strings
.原始代码将一个
csv
文件作为输入,其中包含integers
和strings
行和列。 Now we're reading in a txt
file having the shape:现在我们正在读取具有以下形状的
txt
文件:
TEXT 0; INT; INT; INT; INT; ... 0; INT; INT; INT; INT; ... 18 more lines of the above numbers and semicolons
In that file I replaced in one instance the semicolons by line breaks and in another by empty spaces, because I was not sure which we needed.在那个文件中,我在一个实例中用换行符替换了分号,在另一个实例中用空格替换了分号,因为我不确定我们需要哪个。
Solution: Use the txt
file with the semicolons, but with the semicolons at the end of the lines removed.解决方案:使用带有分号的
txt
文件,但删除行尾的分号。
It seems to have an issue with if (argc < 2 || argc > 2)
. if (argc < 2 || argc > 2)
似乎有问题。 It throws the " Usage: ./ao <> <> " error message.它会抛出“ Usage: ./ao <> <> ”错误消息。 However, both are strict inequalities.
然而,两者都是严格的不等式。 Shouldn't this
if
be fine with 2 arguments?如果不这样
if
是罚款2个参数? In the original code it read if (argc < 2 || argc > 3)
, which I changed it back to.在原始代码中,它读取
if (argc < 2 || argc > 3)
,我将其改回。
EDIT: As john pointed out the "program name is an argument" as well, so I want actually 3 not 2.编辑:正如约翰指出的“程序名称也是一个参数”,所以我实际上想要 3 而不是 2。
Both txt
files (line breaks & spaces) seem to produce the same error message below两个
txt
文件(换行符和空格)似乎都在下面产生相同的错误消息
Error message: (For the int
threshold I tried various values)错误消息:(对于
int
阈值,我尝试了各种值)
Registering only edges shorter than int.
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 1) >= this->size() (which is 1)
Aborted (core dumped)
Ideas:想法:
I am aware what this error message means when we have a simple case, for example :当我们有一个简单的案例时,我知道此错误消息的含义,例如:
#include <vector>
int main()
{
std::vector<int> v;
v.push_back(123); // v has 1 element [0 to 0]
int x4 = v.at(3); // exception
}
We get an exception, because v has 1 element and element 3 does not exist.我们得到一个异常,因为 v 有 1 个元素而元素 3 不存在。
However, in my code I am not sure what exactly to look for.但是,在我的代码中,我不确定要查找的内容。
The original code read a csv
with lines and columns, but in this case a matrix form with empty spaces in between is probably causing issues.原始代码读取带有行和列的
csv
,但在这种情况下,中间有空格的矩阵形式可能会导致问题。 Does that mean that I just want a txt
file looking like a column vector?这是否意味着我只想要一个看起来像列向量的
txt
文件? That is one file I tried, so it might be that the code is not happy with the amount of columns?这是我试过的一个文件,所以可能是代码对列的数量不满意?
Relevant function:相关功能:
int main(int argc, char** threshold_and_distanceMatrixfilename)
{
if (argc < 2 || argc > 3)
{
std::cerr << "Usage: ./distanceMatrixToSageGraph.o <threshold>
<distanceMatrix_file_calculated_fromDGEsingleCell_data>" << std::endl;
return -1;
}
string distanceMatrixfilename = threshold_and_distanceMatrixfilename[2];
int threshold = std::stoi(threshold_and_distanceMatrixfilename[1]);
std::ifstream distanceMatrixFile(distanceMatrixfilename);
if (!distanceMatrixFile)
{
std::cerr << "Error opening distanceMatrix file: " << distanceMatrixfilename << std::endl;
return -1;
}
string row;
std::getline(distanceMatrixFile, row); // discard the first row, which specifies the format of the file.
vector<vector<int>> the_entries;
while (std::getline(distanceMatrixFile, row))
{
std::stringstream row_as_stringstream(row);
int i; i = 0;
vector<string> row_as_vector;
while (row_as_stringstream.good())
{
string substr;
getline(row_as_stringstream, substr, ',');
row_as_vector.push_back( std::stoi(substr) );
};
the_entries.push_back(row_as_vector); //LINE 104
};
}
Whole code:全码:
// Convert distanceMatrix tables of protein interactions to SAGE graph.
///////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <list>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;
void writeGraphInSageFormat(string name, std::vector<std::vector<int>> TheEdges)
{
//////////////////////////////////////////////////////////////////////////////////////
// Write out the edges in SAGE format.
///////////////////////////////////////////////////////////////////////////////////////
int edgeNumber = TheEdges.size();
ofstream d1sageFile(name, ios::out);
d1sageFile << "g = Graph([" << endl;
for (int n = 0; n < edgeNumber; n++) {
d1sageFile << "(" << TheEdges[n][0] + 1 << "," << TheEdges[n][1] + 1 << ")," << endl;
}
d1sageFile << "])" << endl;
d1sageFile << "g.show()" << endl;
d1sageFile.close();
std::cout << "SAGE graph written into the file " << name << std::endl;
}
std::vector<std::vector<int>> ConvertEntriesMatrixToEdges(vector<vector<int>> the_entries, int threshold)
{
////////////////////////////////////////////////////////////////////////////////////////////
// Construct the edge-vertex incidence matrix (d_1) from the distanceMatrix entries matrix:
////////////////////////////////////////////////////////////////////////////////////////////
std::vector<std::string> proteinNames;
std::vector<std::vector<int>> TheEdges;
std::cout << "Registering only edges shorter than " << threshold << "." << std::endl;
int thisDistance;
for (int i = 0; i < the_entries.size(); i++)
{
for (int j = i + 1; j < the_entries.size(); j++)
{
// we could use the_entries.size() instead of the_entries.at(i).size(), because this is a square matrix.
thisDistance = the_entries.at(i).at(j);
if (thisDistance < threshold)
{
std::vector<int> CurrentEdge(2);
CurrentEdge[0] = i;
CurrentEdge[1] = j;
TheEdges.push_back(CurrentEdge);
};
};
};
return TheEdges;
}
///////////////////////////////////////////
// Main Program: Extract edges from a distanceMatrix file.
///////////////////////////////////////////
int main(int argc, char** threshold_and_distanceMatrixfilename)
{
if (argc < 2 || argc > 3)
{
std::cerr << "Usage: ./distanceMatrixToSageGraph.o <threshold> <distanceMatrix_file_calculated_fromDGEsingleCell_data>" << std::endl;
return -1;
}
string distanceMatrixfilename = threshold_and_distanceMatrixfilename[2];
int threshold = std::stoi(threshold_and_distanceMatrixfilename[1]);
std::ifstream distanceMatrixFile(distanceMatrixfilename);
if (!distanceMatrixFile) {
std::cerr << "Error opening distanceMatrix file: " << distanceMatrixfilename << std::endl;
return -1;
}
string row; //LINE 88
std::getline(distanceMatrixFile, row); // discard the first row, which specifies the format of the file.
vector<vector<int>> the_entries;
while (std::getline(distanceMatrixFile, row))
{
std::stringstream row_as_stringstream(row);
int i; i = 0;
vector<string> row_as_vector;
while (row_as_stringstream.good())
{
string substr;
getline(row_as_stringstream, substr, ',');
row_as_vector.push_back( std::stoi(substr) );
};
the_entries.push_back(row_as_vector); //LINE 104
};
////////////////////////////////////////////////////////////
// Now we assemble the entries to an edges matrix, and write it into a Sage file:
////////////////////////////////////////////////////////////
std::vector<std::vector<int>> TheEdges = ConvertEntriesMatrixToEdges(the_entries, threshold);
char outputFilename[60]; strcpy(outputFilename, distanceMatrixfilename.c_str()); strcat(outputFilename, "AtThreshold"); string thrshld = std::to_string(threshold); strcat(outputFilename, thrshld.c_str()); strcat(outputFilename, ".txt");
writeGraphInSageFormat(outputFilename, TheEdges);
return 0;
}
First question.第一个问题。 What you're missing is that the program name is an argument, so
你缺少的是程序名称是一个参数,所以
program arg1 arg2
is three arguments and argc
will equal 3 not 2. You could have found this out for yourself by either using a debugger (you really need to learn how to use one, much better than asking here) or at the very least adding cout << "argc=" << argc << '\\n';
是三个参数,
argc
将等于 3 而不是 2。您可以通过使用调试器(您确实需要学习如何使用一个调试器,比在此处询问要好得多)或至少添加cout << "argc=" << argc << '\\n';
to your code.到您的代码。
Second question, your original code was written for comma separated values, see the comma here getline(row_as_stringstream, substr, ',');
第二个问题,您的原始代码是为逗号分隔值编写的,请参阅此处的逗号
getline(row_as_stringstream, substr, ',');
so obviously you need to change it for semi colon or space separated values.所以显然你需要将它更改为分号或空格分隔值。
Above all though you need to learn to use a debugger.最重要的是,您需要学习使用调试器。 Trying to debug programs by looking at code is not easy.
试图通过查看代码来调试程序并不容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.