Monday, 15 July 2013

c++ - Searching through a const std::map -


I am working on one of my classes and I have come to a blocked block. I give you a sample of your source, only names of classes, methods and variables are different, but the implementation is the same. You will see my problem / question, question / s & amp; Anxiety within the code block for related work / s.

MyClass.h

  #ifndef MY_CLASS_H # defined MY_CLASS_H const std :: string strOne = std :: string ("a"); Const std :: string strTwo = std :: string ("two"); Const std :: string strThree = std :: string ("three"); Const std :: string strUnknown = std :: string ("unknown"); Calculate {one, two, three, unknown}; Class MyClass {Private: const std :: map & lt; Calculation, const std :: string & gt; M_mCount = createCountMap (); Unsigned less m_uCount; Std :: vector & lt; Std :: string & gt; M_vItems; Std :: multimap & lt; Const std :: string, const std :: string & gt; M_mmAssociatedItems; Public: Myclass (); MyClass (const std :: string strItem1, const std :: string strItem2); Get Static MyClass * (); Zero addItem (calculation type, const std :: string and strItem2); Zero addItem (const std :: string and strItem1, const std :: string and strItem2); Private: Stable Const Std :: Map & lt; Calculation, const std :: string & gt; CreateCountMap (); }; #endif // MY_CLASS_H  

MyClass.cpp

  #include "stdafx.h" #included "MyClass.h" static MyClass * s_pMyClass = nullptr; Const std :: map & lt; Calculation, const std :: string & gt; MyClass: createCountMap () {std :: map & lt; Calculation, const std :: string & gt; Meter; M.insert (std :: make_pair (count :: one, strOne)); M.insert (std :: make_pair (count :: TWO, strTwo)); M.insert (std :: make_pair (count: three, strThree)); M.insert (std :: make_pair (count: UNKNOWN, strUnknown)); Refund M; } // createCountMap MyClass * MyClass :: get () {If (! S_pMyClass) {return nullptr; } Return s_pMyClass; } // get MyClass :: MyClass (): m_uCount (0) {m_vItems.clear (); M_mmAssociatedItems.clear (); } / / MyClass MyClass :: MyClass (const std :: string and strItem1, const std :: string and strItem2): m_uCount (0) {addItem (strItem1, strItem2); } // MyClass void MyClass :: addItem (count type, const std :: string and strItem2) {const std :: map & lt; Calculation, const std :: string & gt; :: const_iterator it = m_mCount.find (type); If (this == m_mCount.end ()) {// no valid item key was found! // throw exception here! } M_vItems.push_back (strItem2); M_mm asymmetimes.int. (standard :: make-pair (it-> second, m_vItems.at (m_uCount)); ++ m_uCount; } Zero MyClass :: addItem (const std :: string and strItem1, const std :: string and strItem2) {// I need to do the same thing as above, instead of seeing my // const std :: map on key Price for a match, with this overloaded // function call to see if it is // within the map's content, where the map is - & gt; The second // will be the value, it needs to use strItem1 as the search item. If not throw a similar error in comparison to the above version, then once it populates my vector // equally larger as above and my calculation variable // // logically it's my next Phase Coast Staged :: map will be; Calculation, const std :: string & gt; :: const_iterator = m_mCount.begin (); // It is within the loop for it that it fails to compile! This fails on this / / part! Is there a way around this? Or // am I missing something simple? (; It! = M_mCount.end (); this ++) {// if the striatum 1 == it-> second & amp; Amp; this! = M_mCount.end () // found it, add item, if not throw error} m_vItems.push_back (strItem2); M_mm affiliation.in.inset (std :: make_pair (strItem1, strItem2)); ++ m_uCount; }  

In my source, this is the second task that is the most important first! Any help or suggestion is highly appreciated.

  const std :: map & lt; Count, const std :: string & gt; :: const_iterator = m_mCount.begin ();  

The first const should be removed otherwise it can not be extended, and you do not do it again via m_mCount You can.


No comments:

Post a Comment