I have a tree like structure in my program like the following:
struct A {
vector<string> m_vStr;
};
struct B {
vector<A> m_vA;
// implemented with nested for loops
const string& getStringWithIndex(size_t i) const;
};
The structure will hardly change but I go through accessing the leafs (I mean the elements of m_vStr
) too often calling getStringWithIndex
so I decided to have something like one of the followings:
vector<const string*> vstr1;
vector<const string&> vstr2; // Error forming pointer to reference type
vector<reference_wrapper<const string&>> vstr3; // forming pointer to reference type
which will be updated whenever the structure is changed and I could use them for more efficient calling of getStringWithIndex
instead of nested for loops. as you see the last two generates error (why?) and I want to know what choices I have here mostly want to know how to store these const references for easy access. And I could use any new features introduced in C++11 to C++17.