尝试在循环中动态初始化 3 维向量时出现分段错误
Getting Segmentation fault while trying to initialise a 3 dimensional vector dynamically in a loop
我正在创建一个名为 points_in_clusters 的向量>。
目的是将 csv 文件的线(点)存储到 3D 矢量中。向量的第一个维度是簇号,其余的 (vector) 是属于相应簇的点集。
因此,例如,在阅读文件后,我希望有一个这样的向量:
vector<vector<vector<string>>> points_in_clusters =
{
{{"a","b","c"},{"q","w","e"}}, //points which belongs to cluster "0"
{{"f","k","l"} , {"a","b","l"},{"f","b","l"}} //belongs to cluster "1"
};
问题是我无法在循环中动态执行此操作,因为我收到此错误:
分段错误(核心已转储)${command}
这是主要功能:
int main(int argc, char *argv[])
{
char fileName[] = "points.csv";
int k = 2;
/*****read_initial_clusters*****/
vector<vector<vector<string>>> points_in_clusters;
for(int numCluster = 0 ; numCluster < k ; ++numCluster){
read_initial_clusters(k , fileName , step , numCluster ,points_in_clusters);
}
return 0
}
这里是 read_initial_clusters 函数:
void read_initial_clusters(int k , string fileName , int step , int numCluster , vector<vector<vector<string>>>& points_in_clusters ){
int numCommas = 0;
string line;
ifstream myfile(fileName,ios::in);
vector<string> point;
if(!myfile.is_open()){
cout<<"Failed to open"<<endl;
exit(0);
}
int start_line = numCluster * step;
int end_line = start_line + step;
int myShare = 0;
int line_counter = 0;
while (getline(myfile, line))
{
if( myShare <= step)
{
if(start_line <= line_counter && line_counter < end_line)
{
myShare++;
stringstream sline(line);
string word;
while (getline(sline , word , ','))
{
point.push_back(word);
numCommas++;
}
**points_in_clusters[numCluster].push_back(point);**//------->Source of error
point.clear();
line_counter++;
}
else
{
line_counter++;
}
}
else
{
myfile.close();
}
}//While
}
问题是,如果我首先用一些含糊不清的东西初始化我的 points_in_clusters(考虑到第一维的数量),那么“read_initial_clusters 函数”中的 push_back 会起作用正确但如果不正确,我会收到此段错误。另一方面,唯一事先确定的是我的第一个维度的数字,即簇号,其余的必须动态填充。
有什么办法可以解决这个问题吗?
你的vector是空的,改一下
for(int numCluster = 0 ; numCluster < k ; ++numCluster){
read_initial_clusters(k , fileName , step , numCluster ,points_in_clusters);
}
至此
for(int numCluster = 0 ; numCluster < k ; ++numCluster){
points_in_clusters.push_back({});
read_initial_clusters(k , fileName , step , numCluster ,points_in_clusters);
}
看起来您正在正确处理其他维度,但您还需要在第一个维度上push_back
。
我正在创建一个名为 points_in_clusters 的向量
vector<vector<vector<string>>> points_in_clusters =
{
{{"a","b","c"},{"q","w","e"}}, //points which belongs to cluster "0"
{{"f","k","l"} , {"a","b","l"},{"f","b","l"}} //belongs to cluster "1"
};
问题是我无法在循环中动态执行此操作,因为我收到此错误: 分段错误(核心已转储)${command}
这是主要功能:
int main(int argc, char *argv[])
{
char fileName[] = "points.csv";
int k = 2;
/*****read_initial_clusters*****/
vector<vector<vector<string>>> points_in_clusters;
for(int numCluster = 0 ; numCluster < k ; ++numCluster){
read_initial_clusters(k , fileName , step , numCluster ,points_in_clusters);
}
return 0
}
这里是 read_initial_clusters 函数:
void read_initial_clusters(int k , string fileName , int step , int numCluster , vector<vector<vector<string>>>& points_in_clusters ){
int numCommas = 0;
string line;
ifstream myfile(fileName,ios::in);
vector<string> point;
if(!myfile.is_open()){
cout<<"Failed to open"<<endl;
exit(0);
}
int start_line = numCluster * step;
int end_line = start_line + step;
int myShare = 0;
int line_counter = 0;
while (getline(myfile, line))
{
if( myShare <= step)
{
if(start_line <= line_counter && line_counter < end_line)
{
myShare++;
stringstream sline(line);
string word;
while (getline(sline , word , ','))
{
point.push_back(word);
numCommas++;
}
**points_in_clusters[numCluster].push_back(point);**//------->Source of error
point.clear();
line_counter++;
}
else
{
line_counter++;
}
}
else
{
myfile.close();
}
}//While
}
问题是,如果我首先用一些含糊不清的东西初始化我的 points_in_clusters(考虑到第一维的数量),那么“read_initial_clusters 函数”中的 push_back 会起作用正确但如果不正确,我会收到此段错误。另一方面,唯一事先确定的是我的第一个维度的数字,即簇号,其余的必须动态填充。
有什么办法可以解决这个问题吗?
你的vector是空的,改一下
for(int numCluster = 0 ; numCluster < k ; ++numCluster){
read_initial_clusters(k , fileName , step , numCluster ,points_in_clusters);
}
至此
for(int numCluster = 0 ; numCluster < k ; ++numCluster){
points_in_clusters.push_back({});
read_initial_clusters(k , fileName , step , numCluster ,points_in_clusters);
}
看起来您正在正确处理其他维度,但您还需要在第一个维度上push_back
。