From: Stephen Howe on 2 Jul 2010 10:08 Hi I have a POD struct in a vector and there are 4 counts I am interested in. It works out that pair of iterators mark the beginning and end of "Channel" events and that one of members of the struct, bool indicates if this channel event has "AI". It is also possible that several Channel "events" (call it it a segment) may be merged in the final output (from adjacent events in the vector) and if that is so, "AI" is present in a merged segment, if there is any AI in the separate events. Given struct ChannelData { : bool AIPresent; : }; and vector<ChannelData> v; vector<ChannelData>::const_iterator itStart = ... // Start of Channel Events vector<ChannelData>::const_iterator itEnd = ... // End of Channel Events then I have int NChannelEvents = static_cast<int>(distance(itStart, itEnd)); // 1 int NAIlEvents = static_cast<int>(count_if(itStart, itEnd, AIPresent)); // 2 where AIPresent is a predicate of the form bool AIPresent(ChannelData c) { return c.AIPresent; } ================== Now the snag is, i would like to write in a STL fashion int NMergedChannelEvents = ???? // 3 int NMergedAIlEvents = ????; // 4 I have done these but the code lacks elegance. For 3, I think I can use Boost's filtered iterators where the predicate indicates whether the current iterator can be merged with the existing state (that is if the Boost's filtered iterators allow state). But 4 seems impossible. It seems I want to combine Boost's filtered iterators with C++0x's any_of() algorithm, and count how many times any_of() returns true. DOing this within the scope of C++ 2003 standard seems not possible. For 3, I have the function int MergedCount(vector<ChannelData>::const_iterator it, vector<ChannelData>::const_iterator itEnd) { int Count = 0, StrideCount; for (; it != itEnd; it += StrideCount, ++Count) StrideCount = MergedStrideCount(it, itEnd); return Count; } For 4, I have the function int MergedAICount(vector<ChannelData>::const_iterator it, vector<ChannelData>::const_iterator itEnd) { int Count = 0, StrideCount; for (; it != itEnd; it += StrideCount) { StrideCount = MergedStrideCount(it, itEnd); if (AIPresent(it, it + StrideCount)) ++Count; } return Count; } but I would like if possible to do this more succinctly. I am not sure if Boosts filtered iterators are right thing, they seem to be a close fit on "Stride Iterators". Any thoughts? Thanks Stephen Howe -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: Cv-qualification as a template parameter Next: enforcing const overload of a method |