c++ - Recursive function does not fully recurse object / sub-objects -
i have recursive function find()
tries find item given id. below extracted relevant parts class make example compile:
#include <iostream> #include <cstdarg> #include <cstdio> #include <string> #include <vector> class item { private: std::vector<item> subitems; public: std::wstring id; public: item() : subitems(0), id(l"") {} item(const item& rhs) : subitems(rhs.subitems.size()) { (std::size_t = 0; < rhs.subitems.size(); ++i) subitems[i] = rhs.subitems[i]; id = rhs.id; } item& operator==(const item& rhs) { if (this != &rhs) { (std::size_t = 0; < rhs.subitems.size(); ++i) subitems[i] = rhs.subitems[i]; id = rhs.id; } return *this; } std::vector<item> getsubitems() { return subitems; } item addsubitems(item * item ...) { va_list args; va_start(args, item); (item * arg = item; arg != null; arg = va_arg(args, item *)) { subitems.push_back(*item); } va_end(args); return *this; } item addsubitems(std::vector<item>& items) { (typename std::vector<item>::value_type &item : items) { subitems.push_back(item); } return *this; } static item * find(int id, std::vector<item>& items) { std::wstring id_str = std::to_wstring(id); std::wcout << "--> find id=" << id_str << std::endl; std::wcout << "size of items=" << items.size() << std::endl; (typename std::vector<item>::value_type &c : items) { std::wcout << "it .. cur id=" << c.id << std::endl; if (!c.id.empty() && c.id == id_str) { std::wcout << "==> found" << std::endl; return &c; } if (!(c.getsubitems()).empty()) { std::wcout << "-> find " << id << " in subitems" << std::endl; std::vector<item> subcls = c.getsubitems(); std::wcout << "size of subitems=" << subcls.size() << std::endl; item * sub = find(id, subcls); if (sub != null) { std::wcout << "==> found in subitems" << std::endl; return sub; } } } return null; } }; int main() { item c1; c1.id = l"0"; item c2; c2.id = l"1"; item c3; c3.id = l"2"; item c4; c4.id = l"3"; //std::vector<item> cll4({c4}); //std::vector<item> cll3({c3}); //std::vector<item> cll2({c2}); c3.addsubitems(&c4, null); c2.addsubitems(&c3, null); c1.addsubitems(&c2, null); //c1.addsubitems(cll2); //c2.addsubitems(cll3); //c3.addsubitems(cll4); std::vector<item> items({c1}); item * c = item::find(2, items); std::wcout << "found item=" << ((c != null && c == &c3) ? "true" : "false") << std::endl; std::wcout << ((c != null) ? c->id : l"") << std::endl; return 0; }
i create few items
, add sub-items
them. want able lookup id of item , return found item or sub-item object using recursive find()
method. if add items addsubitems()
(with variable args), find item doesn't return (valid) item object. if use addsubitems method passing vector of items find() method not recurse subitems.
actually sitting on problem last 4 hours , out of ideas, might simple thing oversee or miss. added copy constructor / , assignment operator afterwords (just see if there changes in behaviour), no. not worry item id being string type (the reason later serialization), class in stages chose string type.
could please point me flaws/problems class straight! in advance!
well, 1 problem "it find item doesn't return (valid) item object." is:
you send item* addsubitems method , add (*item) vector; initialize copy c'tor, later on, when &c == &c3, false, since while objects indeed identical, addresses not since copies of 1 another.
not understand why want copy solution either test
if (c == c3) -> activating item operator ==
or allocate members, save a
std::vector<item*> subitems;
and ask if (c == c3) -> asking addresses
Comments
Post a Comment