C++ 将向量与结构结合使用
我在初始化数据库函数中有问题,但我似乎无法找出它是什么。此程序编译时没有错误,但在执行时总是崩溃。你知道initialise_数据库函数内部有什么问题吗C++ 将向量与结构结合使用,c++,vector,structure,C++,Vector,Structure,我在初始化数据库函数中有问题,但我似乎无法找出它是什么。此程序编译时没有错误,但在执行时总是崩溃。你知道initialise_数据库函数内部有什么问题吗 #include <iostream> #include <cstddef> #include <cstdlib> #include <fstream> #include <string> #include <vector> #include <algorith
#include <iostream>
#include <cstddef>
#include <cstdlib>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Date
{
int day, month, year;
};
struct Grade
{
int s1, s2, s3, s4, s5, s6;
};
struct Student
{
bool free;
long studentID;
string fname;
string sname;
Date DOB, DOE, DOG;
Grade semester1, semester2;
};
void initialise_database(vector<Student>, int size); // initialize each free variable to free
int main(int argc, char** argv)
{
fstream fin;
char choice_readfile;
int rowcount;
int size;
cout << "Enter number of student:\n";
cin >> size;
vector<Student> BENG;
do //verify choice to read from file is Y,y or N,n
{
cout << "Do you wish to read from file (Y/N)? (file name must be named students.txt)" << endl; //choice for user to read from external file
cin >> choice_readfile;
while(cin.fail())
{
cin.clear();
cin.ignore(80,'\n');
cout << "Please Re-Enter choice" << endl;
cin >> choice_readfile; // choice to read from file
}
}
while(choice_readfile != 'Y' && choice_readfile != 'y' && choice_readfile != 'N' && choice_readfile != 'n');
if(choice_readfile == 'Y' || choice_readfile == 'y')
{
fin.open("students.txt", ios::in|ios::out); //opens mygrades.txt
if(fin.fail())
{
cout << "Error occured while opening students.txt" << endl;
exit(1);
}
fin.clear();
fin.seekg(0);
string line;
while( getline(fin, line) ) //counts the rows in the external file
{
rowcount++;
}
cout << "Number of rows in file is " << rowcount << endl;
cout << size << " " << rowcount << endl;
}
size += rowcount;
int i=0;
initialise_database(BENG, size);
return 0;
}
void initialise_database(vector<Student> BENG, int size)
{
for(int i=0;i<size;i++)
{
BENG[i].free = false;
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构日期
{
整数日、月、年;
};
结构等级
{
int s1、s2、s3、s4、s5、s6;
};
体类型
{
无布尔;
学生长;
字符串fname;
弦圈套;
日期DOB、DOE、DOG;
中一、中二年级;
};
void初始化_数据库(向量,整数大小);//将每个自由变量初始化为自由变量
int main(int argc,字符**argv)
{
流鳍;
字符选择_读取文件;
整数行数;
整数大小;
大小;
向量BENG;
执行//验证要从文件中读取的选项是否为Y,Y或N,N
{
无法选择读取文件;
while(cin.fail())
{
cin.clear();
cin.忽略(80,“\n”);
cout choice_readfile;//从文件读取的选项
}
}
while(choice_readfile!=“Y”&&choice_readfile!=“Y”&&choice_readfile!=“N”&&choice_readfile!=“N”);
如果(选项_readfile=='Y'| |选项_readfile=='Y')
{
fin.open(“students.txt”,ios::in | ios::out);//打开mygrades.txt
if(fin.fail())
{
cout向量BENG;
在这里定义一个空的向量。稍后在函数中:
void initialise_database(vector<Student> BENG, int size)
{
for(int i=0;i<size;i++)
{
BENG[i].free = false;
}
}
我发现您显示的代码存在两个问题
1) std::vector不会自动增大大小。若要增大向量的大小,需要使用resize()、push_back()、emplace_back()或insert()方法。您没有使用这些方法。向量仍然为空。尝试访问向量中不存在的元素会导致未定义的行为
2) 初始化_database()的std::vector参数正在按值传递。这将生成向量的副本,并且初始化_database()中对向量的任何更改都不会反映在实际传递给此函数的向量中
size += rowcount;
BENG.resize(size);
initialise_database(BENG, size);