我有一个Playlist
具有 Tracks
vector 的类每个 Track
有一个multimap<long, Note>
作为数据成员。
class Track {
private:
multimap<long, Note> noteList;
}
使用迭代器访问轨道没有问题,因此这部分工作正常:
vector<Track>::iterator trackIT;
try{
for(noteIT = trackIT->getNoteList().begin(); noteIT != trackIT->getNoteList().end(); noteIT++){
cout << "---" << noteIT->second.getName() << endl;
}
}catch (int e){
cout << "exception #" << e << endl;
}
接下来我想做的是迭代 Notes
每个Track
。但从这部分开始,所有输出都停止了。所以我只能看到第一首轨道的名称。之后的任何 cout 都不会显示,编译器也不会给我任何错误。即使 try catch block 内的 cout 也不起作用..
vector<Track>::iterator trackIT;
multimap<long, Note>::iterator noteIT;
for(trackIT = this->playlist.getTracklist().begin(); trackIT < this->playlist.getTracklist().end(); trackIT++){
cout << trackIT->getTrackName() << endl;
for(noteIT = trackIT->getNoteList().begin(); noteIT != trackIT->getNoteList().end(); noteIT++){
cout << "---" << noteIT->second.getName() << endl;
}
}
cout << "random cout that is NOT shown" << endl; // this part doesn't show up in console either
此外,我用于添加 Note 对象的 Track 类中的方法如下所示:
void Track::addNote(Note ¬e) {
long key = 1000009;
this->noteList.insert(make_pair(key, note));
}
// I'm adding the notes to the track like this:
Note note1(440, 100, 8, 1, 1);
note1.setName("note1");
synthTrack.addNote(note1);
有什么想法为什么迭代器不起作用?
最佳答案
改变
noteIT < trackIT->getNoteList().end()
至
noteIT != trackIT->getNoteList().end()
并非所有迭代器都支持小于/大于比较。
如果您有 c++11,您可以使用基于范围的 for 循环:
for (Note& note : trackIT->getNoteList())
或者您可以使用 BOOST_FOREACH
BOOST_FOREACH (Note& note, trackIT->getNoteList())
关于c++ - 多重映射迭代器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10212881/