我如何搜索对象指针列表? C++
How do i search a list of object pointers? C++
首先,这是一个人为限制的任务。
作业要求我使用 STL、继承和多态性。我还必须使用迭代器根据对象 ID 从列表中查找、打印和删除项目。
我正在使用指向对象的指针列表。这些对象派生自一个抽象基class序列,动态分配并存储在列表中。
我的抽象基础class
class Sequence{
public:
virtual void print() = 0;
virtual int getId() = 0;
protected:
std::string m_label;
int m_id;
std::string m_sequence;
int m_length;
};
print()
和 getId()
函数在派生的 classes 中被覆盖。正在从文件中读取数据并通过每行上的命令进行解析。
SequenceDatabase::SequenceDatabase(){
std::list<Sequence*> myList;
}
// function reads in the filename creates a data stream and performs the requested actions
void SequenceDatabase::importEntries(std::string inputFile){
std::ifstream dnaFile(inputFile);
char command;
std::string label, sequence, type;
int id, length, index, orf;
while(dnaFile >> command){
Sequence* s;
// if the command = D this allocates memory for a dna object and pushes the object onto the list
if(command == 'D'){
dnaFile >> label >> id >> sequence >> length >> index;
std::cout << "Adding " << id << " ...\n\n";
s = new DNA(label, id, sequence, length, index);
myList.push_back(s);
}
// if the command = R this allocates memory for a RNA object and pushes the object onto the list
if(command == 'R'){
dnaFile >> label >> id >> sequence >> length >> type;
std::cout << "Adding " << id << " ...\n\n";
s = new RNA(label, id, sequence, length, type);
myList.push_back(s);
}
// if the command = A this allocates memory for an AA object and pushes the object onto the list
if(command == 'A'){
dnaFile >> label >> id >> sequence >> length >> orf;
std::cout << "Adding " << id << " ...\n\n";
s = new AA(label, id, sequence, length, orf);
myList.push_back(s);
}
// if the command = O this searches the list for the id and either outputs that the object doesn't exist or it deletes it
if(command == 'O'){
dnaFile >> id;
std::cout << "Obliterating " << id << " ...\n\n";
// problem
}
// if the command = P this searches the lists for the id and either outputs that the object doesn't exist or it prints out the info of the object
if(command == 'P'){
dnaFile >> id;
std::cout << "Printing " << id << " ...\n";
// problem
}
// if the command = S this outputs the number of entries in the list
if(command == 'S')
std::cout << "Entries: " << myList.size() << " total\n";
}
dnaFile.close();
}
正在正确构建列表。我的问题出现在尝试在列表中搜索具有特定 ID 的对象时。我创建了 findId()
函数,因为我知道我必须将它与读入的 id 进行比较。
我不确定在处理对象指针时如何使用 std::find
或 std::find_if
函数。我已经尝试了几个小时,但我尝试的每件事都无法编译。
感谢任何帮助。谢谢!
你可以使用像
这样的东西
for (Sequence* p : myList)
if (p->getId()>50) dosomething(p);
顺便说一句,您可能想使用智能指针列表(例如 std::shared_ptr),所以
std::list<std::shared_ptr<Sequence>> myList;
如果您可以访问 C++11 编译器,则可以使用:
auto iter = std::find_if(myList.begin(),
myList.end(),
[](Sequence* s) -> bool { return (s->getId() == id); });
if ( iter != myList.end() )
{
return *iter;
}
else
{
return nullptr;
}
std::find_if
搜索谓词 returns true
的元素。可以在 http://en.cppreference.com/w/cpp/algorithm/find.
找到更多信息
在这种情况下,谓词是一个 lambda 函数 returns true
如果给定的 ID 与 Sequence*
对象之一的 ID 匹配。
如果列表不包含匹配的 Sequence*
、find_if
returns myList.end()
.
如果您无法访问 C++11 编译器或者您不想使用 lambda 函数,您可以定义一个比较器
struct Comparator {
id_type find_id;
Comparator(id_type id) : find_id(id) {}
inline bool operator ()(const Sequence * obj) {
return find_id == obj->getId();
}
}
. . .
typename std::list<Sequence *>::iterator it;
it = std::find_if(List.begin(), List.end(), Comparator(id));
return it != List.end();
首先,这是一个人为限制的任务。 作业要求我使用 STL、继承和多态性。我还必须使用迭代器根据对象 ID 从列表中查找、打印和删除项目。
我正在使用指向对象的指针列表。这些对象派生自一个抽象基class序列,动态分配并存储在列表中。
我的抽象基础class
class Sequence{
public:
virtual void print() = 0;
virtual int getId() = 0;
protected:
std::string m_label;
int m_id;
std::string m_sequence;
int m_length;
};
print()
和 getId()
函数在派生的 classes 中被覆盖。正在从文件中读取数据并通过每行上的命令进行解析。
SequenceDatabase::SequenceDatabase(){
std::list<Sequence*> myList;
}
// function reads in the filename creates a data stream and performs the requested actions
void SequenceDatabase::importEntries(std::string inputFile){
std::ifstream dnaFile(inputFile);
char command;
std::string label, sequence, type;
int id, length, index, orf;
while(dnaFile >> command){
Sequence* s;
// if the command = D this allocates memory for a dna object and pushes the object onto the list
if(command == 'D'){
dnaFile >> label >> id >> sequence >> length >> index;
std::cout << "Adding " << id << " ...\n\n";
s = new DNA(label, id, sequence, length, index);
myList.push_back(s);
}
// if the command = R this allocates memory for a RNA object and pushes the object onto the list
if(command == 'R'){
dnaFile >> label >> id >> sequence >> length >> type;
std::cout << "Adding " << id << " ...\n\n";
s = new RNA(label, id, sequence, length, type);
myList.push_back(s);
}
// if the command = A this allocates memory for an AA object and pushes the object onto the list
if(command == 'A'){
dnaFile >> label >> id >> sequence >> length >> orf;
std::cout << "Adding " << id << " ...\n\n";
s = new AA(label, id, sequence, length, orf);
myList.push_back(s);
}
// if the command = O this searches the list for the id and either outputs that the object doesn't exist or it deletes it
if(command == 'O'){
dnaFile >> id;
std::cout << "Obliterating " << id << " ...\n\n";
// problem
}
// if the command = P this searches the lists for the id and either outputs that the object doesn't exist or it prints out the info of the object
if(command == 'P'){
dnaFile >> id;
std::cout << "Printing " << id << " ...\n";
// problem
}
// if the command = S this outputs the number of entries in the list
if(command == 'S')
std::cout << "Entries: " << myList.size() << " total\n";
}
dnaFile.close();
}
正在正确构建列表。我的问题出现在尝试在列表中搜索具有特定 ID 的对象时。我创建了 findId()
函数,因为我知道我必须将它与读入的 id 进行比较。
我不确定在处理对象指针时如何使用 std::find
或 std::find_if
函数。我已经尝试了几个小时,但我尝试的每件事都无法编译。
感谢任何帮助。谢谢!
你可以使用像
这样的东西 for (Sequence* p : myList)
if (p->getId()>50) dosomething(p);
顺便说一句,您可能想使用智能指针列表(例如 std::shared_ptr),所以
std::list<std::shared_ptr<Sequence>> myList;
如果您可以访问 C++11 编译器,则可以使用:
auto iter = std::find_if(myList.begin(),
myList.end(),
[](Sequence* s) -> bool { return (s->getId() == id); });
if ( iter != myList.end() )
{
return *iter;
}
else
{
return nullptr;
}
std::find_if
搜索谓词 returns true
的元素。可以在 http://en.cppreference.com/w/cpp/algorithm/find.
在这种情况下,谓词是一个 lambda 函数 returns true
如果给定的 ID 与 Sequence*
对象之一的 ID 匹配。
如果列表不包含匹配的 Sequence*
、find_if
returns myList.end()
.
如果您无法访问 C++11 编译器或者您不想使用 lambda 函数,您可以定义一个比较器
struct Comparator {
id_type find_id;
Comparator(id_type id) : find_id(id) {}
inline bool operator ()(const Sequence * obj) {
return find_id == obj->getId();
}
}
. . .
typename std::list<Sequence *>::iterator it;
it = std::find_if(List.begin(), List.end(), Comparator(id));
return it != List.end();