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

    changing for loop

    Hello,

    The changes i made sometime ago in the code, looks like doesnot handle all the scenarios.

    Code:
    		for (auto config : m_pTACWizData->m_TacPlanData.GetConfigData())
    	{
    		for (auto& [tac, sitekeys] : m_TacToUnassignedNeighSiteKeysList)
    		{
    			for( auto site : sitekeys)
    			{
    				AssignTacsForSite(site, m_SiteKeyToNeighbourKeys[site], config, bAbort);
    
    				if(m_TacToSiteKeysList[tac].size() == config.GetNumSites())
    					break;
    			}
    		}
    Initially m_TacToUnassignedNeighSiteKeysList has 2,4.

    Now after running the inside for loop, for 2 within the AssignTacsForSite(), m_TacToUnassignedNeighSiteKeysList 5,6 gets added and becomes, 4, 5,6


    After second time inside for loop : for 4: within the AssignTacsForSite(), m_TacToUnassignedNeighSiteKeysList 7,8 gets added and becomes, 5,6, 7, 8

    Is this looks ok ? Not even tested yet !

    thanks a lot
    Last edited by pdk5; February 16th, 2021 at 12:32 PM.

  2. #2
    Join Date
    May 2015
    Posts
    500

    Re: changing for loop

    Actually tried to test: Does not work:
    Code:
    		for (auto& [tac, sitekeys] : m_TacToUnassignedNeighSiteKeysList)
    		{
    			for( auto site : sitekeys)
    			{
    				AssignTacsForSite(site, m_SiteKeyToNeighbourKeys[site], config, bAbort);
    
    			    //m_TacToUnassignedNeighSiteKeysList[tac].erase(site);
    
    				if(m_TacToSiteKeysList[tac].size() == config.GetNumSites())
    					break;
    			}
    		}
    the first for loop ran with m_TacToUnassignedNeighSiteKeysList : (1, (80))

    In the function AssignTacsForSite(), the m_TacToUnassignedNeighSiteKeysList became ( 1, (80, 90))

    I wanted to erase 80 and now next for loop to be run for 90.

    But in my case after the first one, loop exits.

    Not sure if i need to make the for loop run on reference, not value:
    I thought "for (auto& [tac, sitekeys] : m_TacToUnassignedNeighSiteKeysList)" takes reference and" for (auto [tac, sitekeys] : m_TacToUnassignedNeighSiteKeysList)" takes by value. so it should have updated the for loop...

    Also erase in red is not working

    Please kindly help. This is urgent, as i had not thought about this scenarion in the beginning
    Last edited by pdk5; February 16th, 2021 at 01:26 PM.

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: changing for loop

    I came up with the following change, as of now works. But will be helpful to get some advice and suggestions

    Code:
    	{
    		do {
    			for (auto& [tac, sitekeys] : m_TacToUnassignedNeighSiteKeysList)
    			{
    				for (auto site : sitekeys)
    				{
    					AssignTacsForSite(site, m_SiteKeyToNeighbourKeys[site], config, bAbort);
    
    					auto it = std::find(m_TacToUnassignedNeighSiteKeysList[tac].begin(), m_TacToUnassignedNeighSiteKeysList[tac].end(), site);
    					m_TacToUnassignedNeighSiteKeysList[tac].erase(it);
    
    					if (m_TacToSiteKeysList[tac].size() == config.GetNumSites())
    						break;
    				}
    			}
    		} while (m_TacToUnassignedNeighSiteKeysList[config.GetTAC()].size());

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

    Re: changing for loop

    After the find, you don't check if site hasn't been found.

    Code:
    auto it = std::find(m_TacToUnassignedNeighSiteKeysList[tac].begin(), m_TacToUnassignedNeighSiteKeysList[tac].end(), site);
    m_TacToUnassignedNeighSiteKeysList[tac].erase(it);
    Isn't this just:

    Code:
    const auto it = std::find(sitekeys.begin(), sitekeys.end(), site);
    if (it != sitekeys.end())
        sitekeys.erase(it);
    as sitekeys is a ref.
    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)

  5. #5
    Join Date
    May 2015
    Posts
    500

    Re: changing for loop

    Thanks a lot kaud.

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