CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2015
    Posts
    500

    replace the multiset with new set inside the existing map

    Hello,

    I have map of multiset. Now i filter the multiset , and comeup with new set.

    I want to replace the old multiset for that key with the filtered one.

    Code:
    		for (auto [sitekey, neighkeys] : m_SiteKeyToNeighbourKeys)
    		{
    			int nTotalNeighboutHOCount(0);
    			int nTotalNeighboutDistanceCount(0);
    			int nTotalSiteNeighbours(0);
    
    			for (auto neigh : neighkeys)
    			{
    				nTotalNeighboutDistanceCount += neigh.m_Distance;
    				nTotalNeighboutHOCount += neigh.m_HOCount;
    				nTotalSiteNeighbours++;
    			}
    			ApplyInitialDistanceHOCriterion(sitekey, neighkeys, nTotalNeighboutHOCount, 
                                        nTotalNeighboutDistanceCount, nTotalSiteNeighbours);
    
                             // Now i want to replace the neighkeys for this site key
    
                       }
    thanks a lot
    pdk

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: replace the multiset with new set inside the existing map

    1) Usually const auto& rather than auto so that you pass by ref and not by value.

    2) If neighkeys is auto& (by ref) and neighkeys is passed by ref to ApplyinitialDistanceHOCriterion(), won't that do what is required?

    ie:

    Code:
    for (auto& [sitekey, neighkeys] : m_SiteKeyToNeighbourKeys)
    		{
    			int nTotalNeighboutHOCount(0);
    			int nTotalNeighboutDistanceCount(0);
    			int nTotalSiteNeighbours(0);
    
    			for (const auto& neigh : neighkeys)
    			{
    				nTotalNeighboutDistanceCount += neigh.m_Distance;
    				nTotalNeighboutHOCount += neigh.m_HOCount;
    				nTotalSiteNeighbours++;
    			}
    			ApplyInitialDistanceHOCriterion(sitekey, neighkeys, nTotalNeighboutHOCount, 
                                        nTotalNeighboutDistanceCount, nTotalSiteNeighbours);
    
                             // if neighkeys is passed by ref to ApplyInitialDistanceHOCriterion(), won't neighkeys be now updated??
    
                       }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: replace the multiset with new set inside the existing map

    Thanks a lot kaud. I added
    m_SiteKeyToNeighbourKeys[sitekey] = neighkeys;

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: replace the multiset with new set inside the existing map

    If these containers have many elements, consider the performance impacts of all of these 'by value' copies being done.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured