heterogeneous-array

c++ heterogeneous container, get entry as type

故事扮演 提交于 2019-12-11 02:29:15
问题 I have the following simple implementation of a Heterogeneous container: struct Container { struct HolderBase { }; template<typename S> struct Holder : HolderBase { Holder(S* s) : s_(s) {} S* s_; }; template<typename S> void push_back(S* s) { h_.push_back(new Holder<S>(s)); } vector<HolderBase*> h_; template<typename B> B* get(int i) { //magic here } }; Here's how to use it: struct ElementBase {}; struct Element : ElementBase {}; int main() { Container container; container.push_back(new

Sorting an array based on an attribute that may be nil in some elements

倾然丶 夕夏残阳落幕 提交于 2019-11-27 12:20:25
问题 I have an array of objects [<#a star=1 val=1>, <#a star=nil val=3> , <#a star=2 val=2>] i need the array to be sorted by time, then by val [ <#a star=2 val=2>, <#a star=1 val=1>, <#a star=nil val=3> ] but using the sort_by throws an error because the time is nil. I am using an ugly way to sort right now, but i am sure there is a nice way to go about it starred=[] @answers.each {|a| (starred << a) if a.starred } @answers=@answers-starred starred=starred.sort_by {|a| a.starred }.reverse