c++ - Extracting elements from a struct stored in an std::container -
i have std::vector stores complex struct s (coming real time source) consists of nested structs n1, n2, n3. struct s has time field.
a common task, retrieve of 3 nested structs between 2 times, example getsequencen1(starttime, stoptime, itrstart, itrstop), gives 2 iterators start , end of subsequence.
so code this:
struct { double time struct n1; struct n2; struct n3; } s; class d { public: void getsequencen1(starttime, stoptime, itrstart, itrstop); void getsequencen2(starttime, stoptime, itrstart, itrstop); void getsequencen3(starttime, stoptime, itrstart, itrstop); private: std::vector<s> stream; };
what right way of implementing functionality of getsequencen1?
one way of course have vector each n1, n2, n3, (in real live there more 3 subtracts) wondering if std offers nice feature this?
i not forced use vector btw, std container (maybe boost) works. hoping provide view vector either see n1 n2 or n3.
one way provide customized iterators give n1 n2 , n3 respectively.
void getsequencen1(double starttime, double stoptime, vector<struct n1>& sequence) { for( s cur: stream) { if (s.time >= starttime && s.time < endtime) { sequence.push-back(s.n1's_name); } } }
optimize , adjust start , end conditions suit. example, if s not going modified while examining sequence, can use vector of pointers struct n1 , save copying.
edit
playing around concept. still need abstract because declaring different iterator each sub structure pretty stupid solution. suggestions appreciated.
#include<iostream> #include<vector> struct n1 { int count; bool operator==(const n1 & rhs) const { return count == rhs.count; } bool operator!=(const n1 & rhs) const { return count != rhs.count; } }; struct s { double time; struct n1 n1; bool operator<(const s & rhs) const { return time < rhs.time; } }; class n1iterator: public std::iterator<std::input_iterator_tag, struct n1> { std::vector<s>::iterator mit; public: n1iterator() { } n1iterator(std::vector<s>::iterator it) : mit(it) { } n1iterator(const n1iterator& it) : mit(it.mit) { } n1iterator& operator++() { ++mit; return *this; } n1iterator operator++(int) { n1iterator tmp(*this); operator++(); return tmp; } bool operator==(const n1iterator& rhs) { return mit->n1 == rhs.mit->n1; } bool operator!=(const n1iterator& rhs) { return mit->n1 != rhs.mit->n1; } n1& operator*() { return mit->n1; } n1* operator->() { return &mit->n1; } }; std::vector<s> stream; n1iterator & getsequencen1(double starttime, double stoptime, n1iterator & begin, n1iterator & end) { s key; key.time = starttime; begin=n1iterator(std::lower_bound(stream.begin(), stream.end(), key)); key.time = stoptime; end=n1iterator(std::lower_bound(stream.begin(), stream.end(), key)); return begin; } int main(int argc, char **argsv) { (int count = 0; count < 10; count++) { s s1; s1.time = count; s1.n1.count = count; stream.push_back(s1); } n1iterator begin; n1iterator end; getsequencen1(3, 7, begin, end); while (begin != end) { std::cout << begin->count << std::endl; ++begin; } }
Comments
Post a Comment