您最初调用ped_device_get_next(NULL);然后在后续调用中,传递它在前一次调用中返回的值。一种方法是这样的:
int main()
{
ped_device_probe_all();
PedDevice *tempbed = nullptr;
int c = 0;
while((tempbed = ped_device_get_next(tempbed))!= nullptr && c <255) {
std::cout<<c<<" "<<tempbed->model<<"\n";
c++;
}
return 0;
}
…或者你可以把它包装成一个模仿容器的类:
#include <parted/parted.h>
class devices {
public:
class iterator {
PedDevice *current = nullptr;
public:
iterator &operator++() {
current = ped_device_get_next(current);
return *this;
}
PedDevice &operator*() const { return *current; }
bool operator!=(iterator const &other) const {
return current != other.current;
}
};
devices() { ped_device_probe_all(); }
iterator begin() {
iterator i;
return ++i;
}
iterator end() { return iterator(); }
};
…然后像其他容器一样迭代它:
int main()
{
devices dev;
for (auto const &d : dev) {
std::cout << d.model << "\n";
}
}
顺便说一句,请注意,在大多数系统上,您必须以root身份运行它才能实际显示任何内容。