2011-10-31 54 views
0

对于文本的每一行:(会有线的未知量,每个寻找相同的格式)矢量,我不知道该怎么办

-Get first name 
-Get last name 
-Get Student Number 


格式:

First Last 111111111 
First Last 111111112 
... 

我想把每个学生放到自己的结构中。我已经设置了结构如下:

struct Student{ 
    string lastName; 
    string firstName; 
    string stdNumber; 
    double assgn1;//doubles will be used later in prog. 
    double assign2; 
    double assign3; 
    double assign4; 
    double midTerm; 
    double finalGrade; 
}; 

我的文件输入功能如下:

int getFileInfo() 
{  
    int failed=0; 
    ifstream fin; 
    string fileName;  
    vector<Student> students; 

    Student s;     // A place to store data of one student 
    cout<<"Please enter the filename of the student grades (ex. filename_1.txt)."<<endl; 
    do{         
     if(failed>=1)       
      cout<<"Please enter a correct filename."<<endl; 
     cin>>fileName;        
     fin.open(fileName.c_str());// Open the file  
     failed++;           
    }while(!fin.good());           
    while (fin >> s.firstName >> s.lastName >> s.stdNumber) 
     students.push_back(s);      
    fin.close();        
    return 0;         
} 

在文件输入,我做一个载体,但我不确定如何访问它的每个单独部分或学生s,因为它似乎只能让一个学生。我被告知,文件的每一行都被分割并输入到students向量中,但我不知道如何提取该信息。我如何让每个学生脱离students向量并进入它自己的结构,这样我就可以将每个学生用作它自己的结构? 所以,最后,我想成为能够输出:

Student1: First Last 111111111 
Student2: First Last 111111112 
However More students are in the file 

在此先感谢您的帮助!

@Loki 我改变了循环,所以loop != end,我仍然得到同样的问题。这里是我的代码:

int getFileInfo() 
{ 
int failed=0; 
ifstream fin; 
string fileName; 
vector<Student> students;// A place to store the list of students 
vector<Student>::iterator loop = students.begin(); 
    vector<Student>::iterator end = students.end(); 

Student s;     // A place to store data of one student 
cout<<"Please enter the filename of the student grades (ex. filename_1.txt)."<<endl; 
do{ 
if(failed>=1) 
    cout<<"Please enter a correct filename."<<endl; 
    cin>>fileName; 
    fin.open(fileName.c_str());// Open the file 
    failed++; 
}while(!fin.good()); 

while (fin >> s.firstName >> s.lastName >> s.stdNumber){ 
    cout<<"Reading "<<s.firstName<<" "<<s.lastName<<" "<<s.stdNumber<<endl; 
    students.push_back(s); 
} 
fin.close(); 

    for(loop;loop!=end;++loop) 
    cout<<loop->firstName<<" "<<loop->lastName<<" "<<loop->stdNumber<<endl; 

return 0; 
} 

此外,Visual Studio是说vector iterators incompatible
指着 “C:\ Program Files文件(x86)的\微软的Visual Studio 10.0 \ VC \包括\载体”

+0

Nothing fancy.Just read [ifstream :: read()](http://www.cplusplus.com/reference/iostream/) istream/read /)来自该文件的每个结构,并使用[vector :: push_back](http://www.cplusplus.com/reference/stl/vector/push_back/)将其保存在向量中。 –

+0

我已经将数据从文本文件中删除并放入向量中。它从我遇到的结构向量中获取保存的数据。 – b3orion

+0

我加了一个答案,希望对你有所帮助。 –

回答

1

在文件输入,我做一个载体,但我不确定如何访问它的每个部件。

可以通过操作员访问各个元素[]

students[0].firstName; // get the first name. 

std::cout << students[1].firstName; // prints out the first name. 

可以使用的索引从0 - >尺寸()。

size_t size = students.size(); // number of students in the vector. 

students[size].firstName; // This is wrong. You expression in 
          // [] must be less than size as the elements are 
          // numbered from 0 to size() -1. 

,因为它似乎只让一个学生。

您只创建一个学生的'(作为一个方面没有选择一个更好的名字)。但是当你调用push_back()时,你正在将这个学生对象复制到vector中。所以矢量得到它存储的's'的副本。然后每次通过循环时,用从文件中检索的新值覆盖's'中的旧值。

有人告诉我,文件的每一行都被拆分并输入到学生向量中,但我不知道如何提取该信息。

由于每个元件如上所述可以单独使用操作[]

或者,也可以使用迭代得到一个范围达到。学生的。

std::vector<Student>::iterator loop = students.begin(); 
std::vector<Student>::iterator end = students.end(); 

您可以通过取消参考循环访问元素并通过递增循环移动到下一个学生。

std::cout << loop->firstName; // prints out student[0] 
++loop;       // Increment loop now it points at student 1 
std::cout << loop->firstName; // prints out student[1] 
++loop;       // Increment loop now it points at student 2 
// etc 

如果循环==结束,那么你已经移过所有的学生。

如何让每个学生离开学生向量并进入自己的结构,这样我就可以将每个学生用作自己的结构?

学生们已经在结构中了。你可以像使用外部结构一样直接使用它们。但是,如果你想将它们复制出来,你可以这样做:

Student tmp = students[0]; // copies student 0 into tmp. 

我想成为能够输出:

使用std ::法院输出的东西到标准输出。或创建std :: ofstream类型的对象以输出到文件:

std::cout << tmp.lastName << " " 
      << tmp.firstName << " " 
      << tmp.stdNumber << "\n"; 

std::ofstream file("plop1.txt"); 
file  << tmp.lastName << " " 
      << tmp.firstName << " " 
      << tmp.stdNumber << "\n"; 
+0

当我尝试输出使用'std :: cout',它给了我一个弹出窗口,它显示''Debug Assertion Failed!“'表达式:向量迭代器不可忽略。”任何想法是什么意思?我可以看到一个问题出现在一个不可取消的引用循环中... – b3orion

+0

您是否尝试去引用传入结尾的迭代器(如end()所返回的迭代器所指示的那样)。 –

+0

我做了for循环:'for(loop; loop == end; ++ loop)'现在它说'vector iterators incompatible'。你可以解释吗? – b3orion

2

Online Demo Sample:

#include<iostream>  
#include<string> 
#include<vector> 

using namespace std; 

struct Student 
{ 
    string lastName; 
    string firstName; 
    string stdNumber; 
    double assgn1;//doubles will be used later in prog. 
    double assign2; 
    double assign3; 
    double assign4; 
    double midTerm; 
    double finalGrade; 
}; 

int main() 
{ 
    Student obj; 
    obj.firstName = "ABC"; 
    obj.lastName = "XYZ"; 

    vector<Student> students; 
    students.push_back(obj); 
    vector<Student>::iterator it; 

    cout << "students contains:"; 
    for (it=students.begin() ; it != students.end(); ++it) 
    { 
     cout << " " << (*it).firstName; 
     cout << " " << (*it).lastName; 
     //And so on... 
    } 

     return 0; 
} 
+1

'it Nawaz

+0

@Nawaz:看上去很好。我在编辑时很感谢你指出。 –

+0

+1现在.......... – Nawaz