在c++中迭代类的Vector

Iterating Over Vector of classes in c++

本文关键字:Vector 迭代 c++      更新时间:2023-10-16

Song是一个类我想访问它的一个公共方法

class RadioManager {
    std::vector<Song> all_songs;
public:
void addSong(const Song& song);
}
void mtm::RadioManager::addSong(const Song& song){
vector<Song>::iterator i;
    for (i = all_songs.begin(); i != all_songs.end(); ++i) {
    i->getSongName(); // When i type i-> i don't get the list of methods in the class song;
}

为什么不显示迭代器的内容?

如果它不显示内容,你可以帮助他。(他是你的IDE)

for (i = all_songs.begin(); i != all_songs.end(); ++i) 
{
    Song& song = *i;
    song.getSongName(); 
}

代码做的几乎是一样的,但是你可以在你的调试器中QuickWatch和使用AutoCompletion关于song类型的对象song