如何跳过读取文件的第一行?
How to skip reading the first line of file?
如何在代码中调用时忽略文本文件的第一行,从第二行开始?我想知道如何。另外,如何根据名字、姓氏和年级对文件进行排序?我只对名字进行了排序,但没有对姓氏和等级进行相应排序。如果你有任何想法,我希望你能与我分享。谢谢您的帮助!这是我的代码:
#include <iostream>
#include <fstream>
using namespace std;
struct studentRecord{
string lastname;
string firstname;
string grade;
};
int main(){
ifstream ifs("student-file.txt");
string lastname, firstname, grade, key;
studentRecord records[20];
if(ifs.fail()) {
cout << "Error opening student records file" <<endl;
exit(1);
}
int i = 0;
while(! ifs.eof()){
ifs >> lastname >> firstname >> grade;
records[i].lastname = lastname;
records[i].firstname = firstname;
records[i].grade = grade;
i++;
}
for (int a = 1, b = 0; a < 20; a++) {
key = records[a].firstname ;
b = a-1;
while (b >= 0 && records[b].firstname > key) {
records[b+1].firstname = records[b].firstname;
b--;
}
records[b+1].firstname = key;
}
for (int k = 0; k < 20; k++) {
cout << "\n\t" << records[k].firstname << "\t"<< records[k].lastname << "\t" << records[k].grade;
}
}
当我看到这个 post 时,它让我想起了在大学完成的类似任务。我已经重写了您的代码以执行相同的任务,但使用 类 而不是结构。我还提供了一种使用函数 here.
对向量进行排序的方法
我已经包含了@Scheff's Cat 提到的“忽略第一行”方法。
这里是:
#include <iostream>
#include <fstream>
#include <sstream>
#include <limits>
#include <string>
#include <vector>
using namespace std;
class studentrecord{
string firstname, lastname, grade;
public:
studentrecord(string firstname, string lastname, string grade){
this -> firstname = firstname;
this -> lastname = lastname;
this -> grade = grade;
}
friend ostream& operator<<(ostream& os, const studentrecord& studentrecord) {
os << "\n\t" << studentrecord.firstname << "\t" << studentrecord.lastname << "\t" << studentrecord.grade;
return os;
}
};
void displayRecords(vector <studentrecord*> records){
for(int i = 0; i < records.size(); i++){
cout << *records[i];
}
}
int main(){
//read in file
ifstream infile;
infile.open("student-file.txt");
infile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (!infile.is_open()){
cout << "Error opening student records file" <<endl;
exit(1);
}
vector <studentrecord*> records;
string firstname, lastname, grade;
while (infile >> firstname >> lastname >> grade;) {
records.push_back(new studentrecord(firstname, lastname, grade));
}
displayRecords(records);
return 0;
}
为了对矢量进行排序,使其按名字、姓氏或年级的顺序打印,我使用了以下函数:
bool sortfirstname(studentrecord* A, studentrecord* B) {
return (A->getfirstname() < B->getfirstname());
}
bool sortlastname(studentrecord* A, studentrecord* B) {
return (A->getlastname() < B->getlastname());
}
bool sortgrade(studentrecord* A, studentrecord* B) {
return (A->getgrade() < B->getgrade());
}
sort(records.begin(), records.end(), (sortfirstname));
sort(records.begin(), records.end(), sortlastname);
sort(records.begin(), records.end(), sortgrade);
如果您想按名字排序,您可以调用 sort(records.begin(), records.end(), (sortfirstname));
函数,然后调用 displayrecords()
函数。
使用存储在向量中的 类 的优点是您不必声明包含学生详细信息的向量的大小,因为您可以使用vector.push_back()
函数。它还使对包含的数据进行排序变得更加容易。
如果有任何不清楚的地方,请告诉我,我可以帮助您。
如何在代码中调用时忽略文本文件的第一行,从第二行开始?我想知道如何。另外,如何根据名字、姓氏和年级对文件进行排序?我只对名字进行了排序,但没有对姓氏和等级进行相应排序。如果你有任何想法,我希望你能与我分享。谢谢您的帮助!这是我的代码:
#include <iostream>
#include <fstream>
using namespace std;
struct studentRecord{
string lastname;
string firstname;
string grade;
};
int main(){
ifstream ifs("student-file.txt");
string lastname, firstname, grade, key;
studentRecord records[20];
if(ifs.fail()) {
cout << "Error opening student records file" <<endl;
exit(1);
}
int i = 0;
while(! ifs.eof()){
ifs >> lastname >> firstname >> grade;
records[i].lastname = lastname;
records[i].firstname = firstname;
records[i].grade = grade;
i++;
}
for (int a = 1, b = 0; a < 20; a++) {
key = records[a].firstname ;
b = a-1;
while (b >= 0 && records[b].firstname > key) {
records[b+1].firstname = records[b].firstname;
b--;
}
records[b+1].firstname = key;
}
for (int k = 0; k < 20; k++) {
cout << "\n\t" << records[k].firstname << "\t"<< records[k].lastname << "\t" << records[k].grade;
}
}
当我看到这个 post 时,它让我想起了在大学完成的类似任务。我已经重写了您的代码以执行相同的任务,但使用 类 而不是结构。我还提供了一种使用函数 here.
对向量进行排序的方法我已经包含了@Scheff's Cat 提到的“忽略第一行”方法。
这里是:
#include <iostream>
#include <fstream>
#include <sstream>
#include <limits>
#include <string>
#include <vector>
using namespace std;
class studentrecord{
string firstname, lastname, grade;
public:
studentrecord(string firstname, string lastname, string grade){
this -> firstname = firstname;
this -> lastname = lastname;
this -> grade = grade;
}
friend ostream& operator<<(ostream& os, const studentrecord& studentrecord) {
os << "\n\t" << studentrecord.firstname << "\t" << studentrecord.lastname << "\t" << studentrecord.grade;
return os;
}
};
void displayRecords(vector <studentrecord*> records){
for(int i = 0; i < records.size(); i++){
cout << *records[i];
}
}
int main(){
//read in file
ifstream infile;
infile.open("student-file.txt");
infile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (!infile.is_open()){
cout << "Error opening student records file" <<endl;
exit(1);
}
vector <studentrecord*> records;
string firstname, lastname, grade;
while (infile >> firstname >> lastname >> grade;) {
records.push_back(new studentrecord(firstname, lastname, grade));
}
displayRecords(records);
return 0;
}
为了对矢量进行排序,使其按名字、姓氏或年级的顺序打印,我使用了以下函数:
bool sortfirstname(studentrecord* A, studentrecord* B) {
return (A->getfirstname() < B->getfirstname());
}
bool sortlastname(studentrecord* A, studentrecord* B) {
return (A->getlastname() < B->getlastname());
}
bool sortgrade(studentrecord* A, studentrecord* B) {
return (A->getgrade() < B->getgrade());
}
sort(records.begin(), records.end(), (sortfirstname));
sort(records.begin(), records.end(), sortlastname);
sort(records.begin(), records.end(), sortgrade);
如果您想按名字排序,您可以调用 sort(records.begin(), records.end(), (sortfirstname));
函数,然后调用 displayrecords()
函数。
使用存储在向量中的 类 的优点是您不必声明包含学生详细信息的向量的大小,因为您可以使用vector.push_back()
函数。它还使对包含的数据进行排序变得更加容易。
如果有任何不清楚的地方,请告诉我,我可以帮助您。