0

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.

Dean Seo
  • 5,486
  • 3
  • 30
  • 49
AMCoded
  • 1,374
  • 2
  • 24
  • 39
  • 2
    `vector> vstr3;` is the correct usage - a wrapper for a string, not for a reference to a string, so delete the `&`. – Ken Y-N Nov 23 '17 at 04:35
  • @KenY-N vector of reference-wrapper is not the same as vector of references – M.M Nov 23 '17 at 04:42
  • @Ken Y-N. thanks now I see my problem. Is there any Data structure I could use for this kind of problem. please post with your comment in the answer so that I could mark it correct – AMCoded Nov 23 '17 at 04:47

0 Answers0