Hi friends,

I have a map containing an int as key and a struct as second value, read from file.
I need to update the second column with value obtained by the last value (bigger one) + 1. A kind of sequential code ...

I tried to use the max_element function but didn't work well. I don't know if I made mistake. I commented at the spot where I need to insert a code. (//receiveLastCode =).

E.G.:

1 1 0 0 1
2 2 0 0 1
6 3 0 0 1
7 4 0 0 1
10 5 0 0 1
999 6 0 0 1
1000 7 0 0 1
3 0 14 20 0
8 0 0 1000 0
14 0 999 6 0
20 0 10 1000 0
30 0 999 7 0
77 0 3 8 0
500 0 77 0 0


PS: The next code should be 8! There are codes from 1 to 7 until this point.

Any kind of suggestion will be very appreciated.

Cheers,

while(ZerosRemaning(IndivAuxIDSortedMap))
		{
			for (it4 = IndivAuxIDSortedMap.begin(); it4 != IndivAuxIDSortedMap.end(); ++it4)
			{

				iterSearch = IndivAuxIDSortedMap.find(it4->second.father);
				if (iterSearch != IndivAuxIDSortedMap.end())
				{
					std::cout << "Father RE-CODED Value is: " << iterSearch->second.auxID << '\n';
					receiveSearchFather = iterSearch->second.auxID;
				}
				else std::cout << "FATHER(Key) is not in my_map" << '\n';

				iterSearch = IndivAuxIDSortedMap.find(it4->second.mother);
				if (iterSearch != IndivAuxIDSortedMap.end())
				{
					std::cout << "Mother RE-CODED Value is: " << iterSearch->second.auxID << '\n';
					receiveSearchMother = iterSearch->second.auxID;
				}
				else std::cout << "FATHER(Key) is not in my_map" << '\n';

				iterSearch = IndivAuxIDSortedMap.find(it4->second.father);
				if (iterSearch != IndivAuxIDSortedMap.end())
				{
					std::cout << "Father's Level Value is: " << iterSearch->second.level << '\n';
					receiveSearchFatherLevel = iterSearch->second.level;
				}
				else std::cout << "Father's Level is not in my_map" << '\n';

				iterSearch = IndivAuxIDSortedMap.find(it4->second.mother);
				if (iterSearch != IndivAuxIDSortedMap.end())
				{
					std::cout << "Mother's Level Value is: " << iterSearch->second.level << '\n';
					receiveSearchMotherLevel = iterSearch->second.level;
				}
				else std::cout << "Mother's Level is not in my_map" << '\n';

				if(receiveSearchFather !=0 and receiveSearchMother!=0)
				{
					levelUpdated = returnGreater(receiveSearchFatherLevel,receiveSearchMotherLevel)+ 1;
					IndivAuxIDSortedMap[it4->first].level = levelUpdated;
					std::cout << "Updated Level is: " << IndivAuxIDSortedMap[it4->first].level << endl;
			// UPDATE THE AUX_CODE WITH THE NEXT NUMBER OF second column .
                        //receiveLastCode = ;
				}
			}
		}

First of all, you search for the father, then the mother, then the father again, then the mother again. It will be more efficient to search for the father first, and grab the auxID and the level at the same time; then repeat for the mother.

That said, I don't recall from your earlier thread how your member names correspond to the order of data elements in your table. So which element to update is unclear, but for each time through the loop of your people-sorted-by-auxID (from the name of your map variable), you wish to assign a sequentially increasing "code" value? Or is it possible that some "people" already have code values, so you need to make sure you start above that? If the former (everyone gets a new code value), then it's sufficient to include an integer counter in your outer loop control:

int next_code;
    for (it4 = IndivAuxIDSortedMap.begin(), next_code = 1;
         it4 != IndivAuxIDSortedMap.end();
         ++it4, ++next_code)
    {
        ...
        receiveLastCode = next_code;
    }

Note the commas separating the items within each functionality block (initialization, termination-expression [no change here], update), as opposed to the semicolons separating them.

If assigning the next_code is dependent on a condition, then instead initialize it to 1 at the beginning, and then increment it immediately after assigning it to a person.

Solved! Thanks raptr_dflo!

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.