如何访问指向 class 的一组指针中的 class 数据
How to access class data in a set of pointers to that class
我有一套指针:
set<StudentInterface*, Comparator> studentSet;
这些指针指向 Student
classes 继承自 StudentInterface
并包含一个 int 值 ID
。我想测试某个 class 是否具有特定的 ID 值。我目前的想法如下:
if(studentSet.find(????->getID()) != studentSet.end()) /* do something */
有没有办法以这种方式访问数据元素?如果没有,我可以访问这些元素以测试它们的最短方式(代码行)是什么?
好的,我对这个问题有了初步的回答。我可以使用以下内容在集合中搜索所需元素:
for(StudentInterface* currentSetElement : studentSet) if(currentSetElement->getID() == ID) /* do something */
不确定这是否是最好的方法,但我认为它可行。
我认为您无法使用任何 STL 容器(例如 vector/set/map.
中的查找函数来搜索 class 的特定元素值
您可以按照以下方式执行此操作:
for ( auto x : studentSet )
{
if ( x->getID() == ???? )
{
/* Do something */
}
}
或者,如果您可以创建一个对象进行搜索,那么您可以按照您提出的问题做同样的事情:
if ( studentSet.find(studentSet.begin(), studentSet.end(), YourObject) != studentSet.end() )
/* do something */
您还可以使用库中包含的 set
的迭代器。例如,给定集合 studentSet
,您可以:
for (std::set<StudentInterface*, Comparator>::iterator it=studentSet.begin(); it!=studentSet.end(); ++it){
//Code here, example:
*it->getID();
}
这个for
将从studentSet
的第一个元素迭代到最后一个元素,it
接收存储的对象。此外,it
将收到对该元素的引用(指针)。
运行没时间了。没有必要使用集合然后手动搜索集合。不妨使用矢量。
下面是一个更好的框架方式:
#include <iostream>
#include <set>
using namespace std;
class Interface
{
public:
Interface(int ID):mID(ID)
{
}
bool operator<(const Interface& rhs)
{
return mID < rhs.mID;
}
int mID;
};
class InterfaceTest:public Interface
{
public:
InterfaceTest(int ID): Interface(ID)
{
}
};
class InterfaceKey:public Interface
{
public:
InterfaceKey(int ID): Interface(ID)
{
}
};
struct compare {
bool operator() ( Interface* const& lhs, Interface* const& rhs) const{
return *lhs < *rhs;
}
};
int main()
{
set<Interface*, compare> testset;
testset.insert(new InterfaceTest(1));
testset.insert(new InterfaceTest(2));
testset.insert(new InterfaceTest(3));
testset.insert(new InterfaceTest(4));
InterfaceKey A(1);
if (testset.find(&A) != testset.end())
{
cout << "Found A" << endl;
}
InterfaceKey B(5);
if (testset.find(&B) == testset.end())
{
cout << "Did not find B" << endl;
}
}
我认为使用比较器函数是一种更优雅的解决方案。
假设您将 class 学生定义为
class Student {
public:
Student(int id) : id(id) { }
Student(int id, std::string name) : id(id), name(name) { }
int id;
std::string name;
};
然后你可以在创建集合的时候定义一个比较器函数,然后在查找中使用它。在这种情况下,此函数具有签名 bool (Student, Student)
.
int main() {
std::set<Student, std::function<bool (Student, Student)>> data(
[](Student a, Student b) {
return a.id < b.id;
});
data.insert(Student(1, "foo"));
data.insert(Student(2, "bar"));
data.insert(Student(3, "foobar"));
std::set<Student>::iterator it = data.find(Student(2));
std::cout << "Element found with id " << it->id << " and name " << it->name << std::endl;
return 0;
}
这个cas的输出是
Element found with id 2 and name bar
编辑:此外,您还可以检查
是否找到了某个元素
if (it != data.end()){
// found
} else {
// not found
}
我有一套指针:
set<StudentInterface*, Comparator> studentSet;
这些指针指向 Student
classes 继承自 StudentInterface
并包含一个 int 值 ID
。我想测试某个 class 是否具有特定的 ID 值。我目前的想法如下:
if(studentSet.find(????->getID()) != studentSet.end()) /* do something */
有没有办法以这种方式访问数据元素?如果没有,我可以访问这些元素以测试它们的最短方式(代码行)是什么?
好的,我对这个问题有了初步的回答。我可以使用以下内容在集合中搜索所需元素:
for(StudentInterface* currentSetElement : studentSet) if(currentSetElement->getID() == ID) /* do something */
不确定这是否是最好的方法,但我认为它可行。
我认为您无法使用任何 STL 容器(例如 vector/set/map.
中的查找函数来搜索 class 的特定元素值您可以按照以下方式执行此操作:
for ( auto x : studentSet )
{
if ( x->getID() == ???? )
{
/* Do something */
}
}
或者,如果您可以创建一个对象进行搜索,那么您可以按照您提出的问题做同样的事情:
if ( studentSet.find(studentSet.begin(), studentSet.end(), YourObject) != studentSet.end() )
/* do something */
您还可以使用库中包含的 set
的迭代器。例如,给定集合 studentSet
,您可以:
for (std::set<StudentInterface*, Comparator>::iterator it=studentSet.begin(); it!=studentSet.end(); ++it){
//Code here, example:
*it->getID();
}
这个for
将从studentSet
的第一个元素迭代到最后一个元素,it
接收存储的对象。此外,it
将收到对该元素的引用(指针)。
运行没时间了。没有必要使用集合然后手动搜索集合。不妨使用矢量。 下面是一个更好的框架方式:
#include <iostream>
#include <set>
using namespace std;
class Interface
{
public:
Interface(int ID):mID(ID)
{
}
bool operator<(const Interface& rhs)
{
return mID < rhs.mID;
}
int mID;
};
class InterfaceTest:public Interface
{
public:
InterfaceTest(int ID): Interface(ID)
{
}
};
class InterfaceKey:public Interface
{
public:
InterfaceKey(int ID): Interface(ID)
{
}
};
struct compare {
bool operator() ( Interface* const& lhs, Interface* const& rhs) const{
return *lhs < *rhs;
}
};
int main()
{
set<Interface*, compare> testset;
testset.insert(new InterfaceTest(1));
testset.insert(new InterfaceTest(2));
testset.insert(new InterfaceTest(3));
testset.insert(new InterfaceTest(4));
InterfaceKey A(1);
if (testset.find(&A) != testset.end())
{
cout << "Found A" << endl;
}
InterfaceKey B(5);
if (testset.find(&B) == testset.end())
{
cout << "Did not find B" << endl;
}
}
我认为使用比较器函数是一种更优雅的解决方案。
假设您将 class 学生定义为
class Student {
public:
Student(int id) : id(id) { }
Student(int id, std::string name) : id(id), name(name) { }
int id;
std::string name;
};
然后你可以在创建集合的时候定义一个比较器函数,然后在查找中使用它。在这种情况下,此函数具有签名 bool (Student, Student)
.
int main() {
std::set<Student, std::function<bool (Student, Student)>> data(
[](Student a, Student b) {
return a.id < b.id;
});
data.insert(Student(1, "foo"));
data.insert(Student(2, "bar"));
data.insert(Student(3, "foobar"));
std::set<Student>::iterator it = data.find(Student(2));
std::cout << "Element found with id " << it->id << " and name " << it->name << std::endl;
return 0;
}
这个cas的输出是
Element found with id 2 and name bar
编辑:此外,您还可以检查
是否找到了某个元素if (it != data.end()){
// found
} else {
// not found
}