使用用户定义的类类型对象的向量 [英] Using vector of user defined class type objects

查看:124
本文介绍了使用用户定义的类类型对象的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作在C ++中解决一个简单的问题,想了解如何使用用户定义类类型的对象的向量,而不固定(最坏情况下)数量的元素分配,而实例化对象的向量。



目前我必须使用

 学生(10000人) 

其中10000是我假设的记录的最大值。
如果一些文件有更多的记录,它会明显崩溃。



所以在下面的代码,如何增长我的类对象的向量
动态读取我的类变量中的记录。
我不能使用push_back(),如下面的代码解释。或者如何使用push_back()?



下面的代码应该解释 -

  class grades 
{
public:
string mName;
string mSurname;
int mAge;
string mLocation;
int mMarks;
char mGrade;

// void readdata();
void calcgrade();
void showgrade(string name_surname);

};

使用namespace std;
int main(int argc,char * argv [])
{
** vector< grades>学生(10000); //我不想这样做,但使它动态**
ifstream infile;
char c;
int i = 0;
int no_of_records = 0;

infile.open(argv [1],ios :: in);
if(infile.is_open()!= true)
{
cerr<< 打开输入数据文件时出错:<< argv [1]< ... exiting\\\
;
exit(-1);
}

while(!infile.eof())
{
infile>>学生[i] .mName; //我可以在这里使用push_back()和读取所有下面的实体,使向量增长动态
infile>>学生[i]。
infile>>学生[i]。
infile>>学生[i]。
infile>>学生[i]。
i ++;
no_of_records ++;
}

for(i = 0; i {
//对每个对象做一些函数学生[i] b $ b}
}

FYI: - 我用C ++读取一个文本文件条目如下(实体的顺序和一行中不同类型实体的数量是固定的,我知道,但行数可以根据要读取的不同输入文件而有所不同):

  Name1 Surname1 Course1 Marks1 
Name2 Surname2 Course2 Marks2
Name3 Surname3 Course3 Marks3
...
...

EDIT:处理各种杂散空间,记录实体中的标签的代码 / p>

  while(!infile.eof())
{
c = infile.get
if(isalnum(c))
{
infile.seekg(-1,ios :: cur);
}

if(isalnum(c))
{
grades stud_temp;

infile>> stud_temp.mName;
infile>> stud_temp.mSurname;
infile>> stud_temp.mAge;
infile>>螺柱
infile>> stud_temp.mMarks;
students.push_back(stud_temp);
}
}


解决方案

可以先声明您的矢量:

 矢量< grades>学生们; 

然后读取值,同时在向量中推送元素:

  while(!infile.eof())
{
grade student;
infile>> student.mName;
infile>>学生姓名
infile>> student.mAge;
infile>> student.mLocation;
infile>>学生。
students.push_back(student);
}

您不需要 no_of_records ,因为记录数将 students.size()


Working on solving a simple problem in C++, want to understand how I could use vector of objects of user defined class type without fixed(worst case) number of elements allocated while instantiating that vector of objects. i.e.

Currently I have to use

vector<grades> students(10000); 

where 10000 is some max value of records I have assumed. if some file has more records, it crashes obviously.

So in case of code below, how to grow the vector of my class objects dynamically and read the records in my class variables. I cannot use push_back(), as explained in code below. Or how can i use push_back() ?

Code below should explain -

class grades
{
public:
    string mName;
    string mSurname;
    int mAge;
    string mLocation;
    int mMarks;
    char mGrade;

    //void readdata();
    void calcgrade();
    void showgrade(string name_surname);

};

    using namespace std;
    int main(int argc,char *argv[])
    {
        **vector<grades> students(10000);// I do not want to do this but make it dynamic**
        ifstream infile;
        char c;
        int i=0;
        int no_of_records=0;

        infile.open(argv[1],ios::in);
        if(infile.is_open() != true)
        {
            cerr << "Error opening input data file:" <<argv[1] << "...exiting\n";
            exit(-1);
        }

            while(!infile.eof()) 
            {
                 infile >> students[i].mName;//Can i use push_back() here and for reading all below entities, to make vector grow dynamically
                infile >> students[i].mSurname;
                infile >> students[i].mAge;
                infile >> students[i].mLocation;
                infile >> students[i].mMarks;
                i++;
                no_of_records++;
            }

            for(i=0;i<no_of_records;i++)
            {
               //Do some functions on each object students[i] in the vector 
            }
}

FYI:- I read a text file in C++ which has entries as below(The order of entities and number of different types of entities in one row is fixed which I know but the number of rows can vary based on different input file to be read) :

Name1 Surname1 Course1 Marks1
Name2 Surname2 Course2 Marks2
Name3 Surname3 Course3 Marks3
...
...

EDIT: code to handle all kinds of spurious spaces, tabs in records entities

while(!infile.eof()) 
    {
        c=infile.get();     
        if(isalnum(c))
        {
            infile.seekg(-1,ios::cur);
        }

        if(isalnum(c))
        {
            grades stud_temp;

            infile >> stud_temp.mName;
            infile >> stud_temp.mSurname;
            infile >> stud_temp.mAge;
            infile >> stud_temp.mLocation;
            infile >> stud_temp.mMarks;         
            students.push_back(stud_temp);          
        }
    }

解决方案

You can just declare your vector first:

vector<grades> students;

And then read the values, while pushing elements in the vector:

while(!infile.eof()) 
{
    grades student;
    infile >> student.mName;
    infile >> student.mSurname;
    infile >> student.mAge;
    infile >> student.mLocation;
    infile >> student.mMarks;
    students.push_back(student);
}

You don't need no_of_records anymore, since the number of records will be students.size().

这篇关于使用用户定义的类类型对象的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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