Hi everyone,

I have a std::map of structures . I'm reading the data from a file named "auxIdSorted.txt" and I need to do a while() using the variable auxID. The idea is read the map many times until all values of auxID become different of zero (while(auxID == 0). I don't know how use this value auxID to do a while(). All cases that I found use while reading from beginning to end but not many times.

Could you help me please?

Cheers!

struct STreeGenSimple                      /* Family structure declaration   */
{
  int ID;
  int level;
  int auxID;
  int oldId;
  int father;
  int mother;

} ;

// ...


		std::map<int, STreeGenSimple> IndivAuxIDSortedMap;
	

		myfileReadAuxIdSorted.open("auxIdSorted.txt"); // REABRINDO ARQUIVO PARA LEITURA

		while(myfileReadAuxIdSorted >> indivV)
		{

			STreeGenSimple& current_indivSortedAuxID = IndivAuxIDSortedMap[indivV];
			current_indivSortedAuxID.ID = indivV; //assign AUX ID to the new record at ID indivV.

			myfileReadAuxIdSorted >> auxIdV;
			current_indivSortedAuxID.auxID = auxIdV;

			myfileReadAuxIdSorted  >> fatherV >> motherV >> levelV;
			current_indivSortedAuxID.level = levelV;
			current_indivSortedAuxID.father = fatherV;
			current_indivSortedAuxID.mother = motherV;
		};

		std::map<int, STreeGenSimple>::const_iterator it4;

		for (it4 = IndivAuxIDSortedMap.begin(); it4 != IndivAuxIDSortedMap.end(); ++it4)
		{
			cout << it4->first << '\t' << it4->second.ID << '\t' <<  it4->second.father  << '\t' <<  it4->second.mother  << '\t' << '\t' << it4->second.level << std::endl;
		}

		std::map<int, STreeGenSimple>::const_iterator iterSearch;
		iterSearch = IndivAuxIDSortedMap.begin();

		// * POSITION TO INCLUDE WHILE()

		    for (it4 = IndivAuxIDSortedMap.begin(); it4 != IndivAuxIDSortedMap.end(); ++it4)
			{
				iterSearch = IndivAuxIDSortedMap.find(it4->second.father);
				if (iterSearch != IndivAuxIDSortedMap.end())
					std::cout << "Value is: " << iterSearch->second.auxID << '\n';
				else
					std::cout << "Key is not in my_map" << '\n';


			}

                 // END OF WHILE(). "FOR" WILL BE WITHIN "WHILE". I will update the auxID using FOR

When you say you need to update the auxID many times what exactly do you mean? What is meant by "become different of zero"? If you simply want to make sure that auxID for every object in the map is not zero then you can do something like

std::map<int, STreeGenSimple>::iterator it = IndivAuxIDSortedMap.begin(), end = IndivAuxIDSortedMap.end();
while (it++ != end)
{
    if ((*it).second.auxID == 0)
    {
        // do something here to change it
    }
}

Edited 5 Years Ago by NathanOliver: n/a

Hi NathanOliver,

Thanks for replied me. I need read my map the first time and update the auxId (I didn't show this in my code). But, just reading my map one time is no possible to update my variable. I need to do this many times or in other words I need to read my MAP until my variable AuxId in all lines become different of zero.

I don't know how to read a map many times controlled by while and auxiId.

Could you help me?

Thanks!

Well if you want to have a condition to where you keep looping until all auxID's are zero I would write a function for that

// function to see if any zeros are left
bool ZerosRemaning(std::map<int, STreeGenSimple> IndivAuxIDSortedMap)
std::map<int, STreeGenSimple>::iterator it = IndivAuxIDSortedMap.begin(), end = IndivAuxIDSortedMap.end();
while (it++ != end)
{
    if ((*it).second.auxID == 0)
    {
        return true;
    }
}

// while loop
while (ZerosRemaning(IndivAuxIDSortedMap))
{
    // your for loop here.
}

I think this might be something your looking for. It does involve a bit of overhead though.

Hi Nathan, I will try that idea.

Thanks for your help. I will try implement the Boolean function.

Cheers!

Hi Nathan,

Can you check my code, please? Is it in your mind?

bool ZerosRemaning(std::map<int, STreeGenSimple> IndivAuxIDSortedMap)
{
	std::map<int, STreeGenSimple>::const_iterator it5;
	for (it5 = IndivAuxIDSortedMap.begin(); it5 != IndivAuxIDSortedMap.end(); ++it5)
	{
		if(it5->second.auxID == 0)
			return 1;
	}
}

The function looks good. I did make an error though. After you close out your for loop you should add return false;

bool ZerosRemaning(std::map<int, STreeGenSimple> IndivAuxIDSortedMap)
{
    std::map<int, STreeGenSimple>::const_iterator it5;
    for (it5 = IndivAuxIDSortedMap.begin(); it5 != IndivAuxIDSortedMap.end(); ++it5)
    {
        if(it5->second.auxID == 0)
            return 1;
    }
        return false;  // add this.
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Thanks Nathan!

I will mark as solved!

Cheers and have a nice week!

This question has already been answered. Start a new discussion instead.