2012-04-18 60 views
2

我有一个Playlist类,它具有一个向量Tracks,每个Track有一个multimap<long, Note>作为datamember。多图迭代器不工作

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; 
    } 

我想接下来做的就是重复每个TrackNotes。但从这部分开始,所有输出都停止。所以我只能看到第一首曲目的名字。之后的任何cout都没有显示,编译器也没有给我任何错误。即使try catch块内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 

而且,在我的田径类中的方法,我使用添加的注释对象是这样的:

void Track::addNote(Note &note) { 
    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); 

任何想法为什么迭代器不会工作?

+1

'Note'的拷贝构造函数可能有问题。 - 类型签名不应该是'void Track :: addNote(const Note&note)'?或'void Track :: addNote(Note && note)'。 – leftaroundabout 2012-04-18 15:40:14

+0

'this-> curMsr'的价值是什么? – 2012-04-18 15:46:10

+0

您是否检查'notesIT'实际上是否在'trackIT-> end()'? 'this-> curMsr'的价值是什么? – Grizzly 2012-04-18 15:46:19

回答

1

你并没有显示出getTrackListgetNoteList的定义,但有一个共同的错误的人做 - 如果返回的容器,而不是它的一个引用的副本,迭代器将指向不同的容器使比较不可能。不仅如此,而且由于容器是临时的,任何使用迭代器都会导致未定义的行为。

+0

这实际上是问题所在。我公开了我的trackList,并解决了它.. – networkprofile 2012-04-24 00:11:16

5

变化

noteIT < trackIT->getNoteList().end() 

noteIT != trackIT->getNoteList().end() 

并非所有的迭代器支持比比较小于/大。

如果你有C++ 11你可以使用一个for循环基于范围的:

for (Note& note : trackIT->getNoteList()) 

或者你可以使用BOOST_FOREACH

BOOST_FOREACH (Note& note, trackIT->getNoteList()) 
+0

这消除了我得到的错误,但'cout'仍然没有显示。输出在第一个曲目名称后停止。 – networkprofile 2012-04-22 17:05:07

+0

这是一个iOS应用程序。我不认为我可以使用C++ 11 – networkprofile 2012-04-22 17:06:14

+0

如果你有一个新的问题与std :: cout,我会说这是一个不同的问题,我们真的希望这个问题变成一个“如何做我修复了我的示例程序中的所有编译错误“? 也没有看到整个程序,没有办法真正诊断问题。你的循环是嵌套的吗?我不知道... – 2012-04-22 17:09:42

0

如果你真的硬编码轨道键,则会有永远只能是在地图上一个轨道,因为的std ::地图商店独特的键...

long key = 1000009; //If yo are really doing this, this key is already inserted so it will fail to insert more. 

另外,如果你想升ike一个更优雅的方法,你可以使用函数对象。

struct print_track 
{ 
    void operator()(const Track& track) 
    { 
     cout << track.getTrackName() << endl; 
     std::for_each(track.getNoteList().begin(), track.getNoteList().end(), print_track_name()); 
    } 
}; 

struct print_note_name 
{ 
    void operator()(const std::pair<long,Note>& note_pair) 
    { 
     cout << "---" << note_pair.second.getName() << endl; 
    } 
}; 

//In use... 
std::for_each(playlist.getTracklist().begin(), playlist.getTracklist.end(), print_track()); 
+0

我正在使用multimap,它不需要唯一键。 – networkprofile 2012-04-24 00:10:04