c++;循环遍历对象数组 我是C++中的新手,IM有困难的时间,通过对象数组循环。对象的格式如下: 类别*arr; (就我而言:个人*个人名单;)
问题:我不知道如何循环对象数组,我想对它们调用函数,例如,您可以看到我的代码有Persons类和函数printPersonsInfo(),我在主文件(array personList)中有数组decellation,我希望能够执行以下操作 对于(个人p:personList)p.printPersonInfo() 尝试创建多种类型的循环,但对于数组大小不成功的循环不成功,到目前为止没有任何效果。下面是代码: 主文件:c++;循环遍历对象数组 我是C++中的新手,IM有困难的时间,通过对象数组循环。对象的格式如下: 类别*arr; (就我而言:个人*个人名单;),c++,C++,问题:我不知道如何循环对象数组,我想对它们调用函数,例如,您可以看到我的代码有Persons类和函数printPersonsInfo(),我在主文件(array personList)中有数组decellation,我希望能够执行以下操作 对于(个人p:personList)p.printPersonInfo() 尝试创建多种类型的循环,但对于数组大小不成功的循环不成功,到目前为止没有任何效果。下面是代码: 主文件: int main() { //reading objects fro
int main()
{
//reading objects from file into array
Persons * personList = Files::createPersonsArray();
//reading objects from file into array
Auto * autoList = Files::createAutoArray();
return 0;
}
其中一个对象类(两者几乎相同):
其思想是读取文件,从中创建对象数组,并将它们传递给主类
如果有什么不同的话,我会使用C++14
谢谢。您应该只返回std::vector而不是原始指针。原始指针不会带来它指向的元素数(您需要一个额外的参数)。然后,您可以像往常一样对向量进行迭代。您应该只返回一个std::vector,而不是原始指针。原始指针不会带来它指向的元素数(您需要一个额外的参数)。然后你可以像往常一样在向量上迭代。到底是什么问题?对不起,我忘了在帖子里写下我所有的想法。更新,谢谢注意。使用向量而不是动态分配的数组,然后你就可以在它们的基础上使用基于范围的for。@Etienedemartel非常感谢你,你的建议帮我节省了财富。到底是什么问题?对不起,我忘了在帖子里写下我所有的想法。更新,谢谢注意。使用向量而不是动态分配的数组,然后你可以在它们的基础上使用基于范围的for。@Etienedemartel非常感谢你,你的建议为我节省了财富时间。
class Persons
{
public:
int id;
string name, surename, phone;
Persons() {
//required empty constructor
}
Persons(int i, string n, string s, string p) {
this->id = i;
this->name = n;
this->surename = s;
this->phone = p;
}
void printPersonInfo() {
cout << endl;
cout << "ID : " << id << endl;
cout << "Name : " << name << endl;
cout << "Surename : " << surename << endl;
cout << "Phone : " << phone << endl;
}
};
class Files
{
public:
static Persons * createPersonsArray() {
string textLine;
// Read from the text file
ifstream PersonsFile("Persons.txt");
//counter
int count = 0;
//counting records in file
while (getline(PersonsFile, textLine))
count++;
//return to start of file
PersonsFile.clear();
PersonsFile.seekg(0, ios::beg);
//making array of persons
Persons* person_list = new Persons[count];
//total persons found
cout << "Lines of persons :" << count << endl;
int i = 0;
// Use a while loop together with the getline() function to read the file line by line
while (getline(PersonsFile, textLine)) {
//explodes line of person info into parts
auto parts = explode( textLine,'#');
//stoi converts ID from string to int
person_list[i] = Persons(stoi(parts[0]), parts[1], parts[2], parts[3]);
//increased counter
i++;
}
// Close the file
PersonsFile.close();
return person_list;
}
static Auto* createAutoArray() {
string textLine;
// Read from the text file
ifstream AutoFile("Auto.txt");
//counter
int count = 0;
//counting records in file
while (getline(AutoFile, textLine))
count++;
//return to start of file
AutoFile.clear();
AutoFile.seekg(0, ios::beg);
//making array of persons
Auto* auto_list = new Auto[count];
//total persons found
cout << "Lines of autos :" << count << endl;
int i = 0;
// Use a while loop together with the getline() function to read the file line by line
while (getline(AutoFile, textLine)) {
//explodes line of person info into parts
auto parts = explode(textLine, '#');
//stoi converts ID from string to int
auto_list[i] = Auto(stoi(parts[0]), stoi(parts[1]), stoi(parts[2]), parts[3], parts[4], parts[5]);
//increased counter
i++;
}
// Close the file
AutoFile.close();
return auto_list;
}
//explode function
static vector<string> explode(string const& s, char delim)
{
vector<string> result;
istringstream iss(s);
for (string token; getline(iss, token, delim); )
{
result.push_back(move(token));
}
return result;
}
//find owner info based on id
static void findCarOwnerInfo(int id) {
string textLine;
// Read from the text file
ifstream PersonsFile("Persons.txt");
//if file is not empty
if (PersonsFile.peek() != ifstream::traits_type::eof()) {
//counting records in file
while (getline(PersonsFile, textLine)) {
auto parts = explode(textLine, '#');
//if person id matches
if (stoi(parts.at(0)) == id) {
cout << endl;
cout << "(ID:"<<parts.at(0)<<") Owner info ---------" << endl;
cout << "Name : " << parts.at(1) << endl;
cout << "Surename : " << parts.at(2) << endl;
cout << "Phone : " << parts.at(3) << endl;
}
}
}
else {
cout << "error finding persons." << endl;
}
}
};
ID#NAME#SURENAME#PHONE