Updating <map> Structure - Help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Copeland

    Updating <map> Structure - Help!

    I'm having difficulty updating map objects. In the code I've
    excerpted below, I store map objects without difficulty, but my attempts
    to modify elements of the stored data don't work.

    struct ChipRecord // Chip Times record
    {
    time_t hiStartTime; // Start Time
    // much more...
    } timeWork; // entrant info records
    typedef map<int, ChipRecordBCI;
    BCI bci;
    map<int, ChipRecord>::it erator bIter;

    int nBib = 17;
    time_t cTime = 11151544;



    // initialize and populate "timeWork" structure, etc.
    bci.insert(bci: :value_type(bib Number, timeWork));
    //...
    // obtain a cTime value...
    bIter = bci.find(nBib);
    if(bIter != bci.end())
    {
    timeWork = bIter->second; // ???
    timeWork = bci.find(nBib)->second; // ???
    timeWork = (*bIter).second ; // ???
    if(cTime timeWork.hiStar tTime) // use highest time
    { // update the object
    bIter->second.hiStart Time = cTime; // ???
    timeWork.hiStar tTime = cTime; // ???
    } // if
    } // if

    It's the code directly above that doesn't do what I intend: change
    the map object's value and update the object. There is much more going
    on in the program, but this is the simplest amount that demonstrates the
    issue. I don't know how to update the object. Please advise. TIA
  • AnonMail2005@gmail.com

    #2
    Re: Updating &lt;map&gt; Structure - Help!

    On Nov 17, 1:54 pm, mrc2...@cox.net (Mike Copeland) wrote:
       I'm having difficulty updating map objects.  In the code I've
    excerpted below, I store map objects without difficulty, but my attempts
    to modify elements of the stored data don't work.
    >
    struct ChipRecord                   // Chip Times record
    {
        time_t hiStartTime;                    // Start Time
    // much more...} timeWork;                      // entrant info records
    >
    typedef map<int, ChipRecordBCI;
            BCI bci;
            map<int, ChipRecord>::it erator bIter;
    >
        int    nBib  = 17;
        time_t cTime = 11151544;
    >
    // initialize and populate "timeWork" structure, etc.
        bci.insert(bci: :value_type(bib Number, timeWork));
    //...
    // obtain a cTime value...
        bIter = bci.find(nBib);
        if(bIter != bci.end())
        {
            timeWork = bIter->second;              //???
            timeWork = bci.find(nBib)->second;     // ???
            timeWork = (*bIter).second ;            // ???
            if(cTime timeWork.hiStar tTime)  //  use highest time
            {                                // update the object
                bIter->second.hiStart Time = cTime; // ???
                timeWork.hiStar tTime = cTime;      // ???
            }  // if
        }  // if
    >
       It's the code directly above that doesn't do what I intend: change
    the map object's value and update the object.  There is much more going
    on in the program, but this is the simplest amount that demonstrates the
    issue.  I don't know how to update the object.  Please advise.  TIA
    In your posted code you insert an element with the key bibNumber. But
    then you find with the key nBib. The former is not defined in your
    posted code.

    Why not just do a simple program and print out some results or run it
    through a debugger?

    HTH

    Comment

    • Mike Copeland

      #3
      Re: Updating &lt;map&gt; Structure - Help!

      I'm having difficulty updating map objects. =A0In the code I've
      excerpted below, I store map objects without difficulty, but my attempts
      to modify elements of the stored data don't work.

      struct ChipRecord = // Chip Times record
      {
      time_t hiStartTime; // Start Time
      // much more...} timeWork; // entrant info records

      typedef map<int, ChipRecordBCI;
      BCI bci;
      map<int, ChipRecord>::it erator bIter;

      nBib = 17;
      time_t cTime = 11151544;

      // initialize and populate "timeWork" structure, etc.
      bci.insert(bci: :value_type(nBi b, timeWork));
      //...
      // obtain a cTime value...
      bIter =3D bci.find(nBib);
      if(bIter !=3D bci.end())
      {
      timeWork = bIter->second; //???
      timeWork = bci.find(nBib)->second; // ???
      timeWork = (*bIter).second ; // ???
      if(cTime timeWork.hiStar tTime) // use highest time
      { // update the object
      bIter->second.hiStart Time = cTime; // ???
      timeWork.hiStar tTime = cTime; // ???
      } // if
      } // if

      It's the code directly above that doesn't do what I intend: change
      the map object's value and update the object. =A0There is much more going
      on in the program, but this is the simplest amount that demonstrates the
      issue. =A0I don't know how to update the object. =A0Please advise. TIA
      >
      In your posted code you insert an element with the key bibNumber. But
      then you find with the key nBib. The former is not defined in your
      posted code.
      Yes, my apologies. I tried to post a simple segment of my program,
      and I made that mistake.
      Nevertheless, I still need help on this. I had hoped my code
      explained the problem... 8<{{

      Comment

      • Mike Copeland

        #4
        Re: Updating &lt;map&gt; Structure - Help!

        I'm having difficulty updating map objects. =A0In the code I've
        excerpted below, I store map objects without difficulty, but my attempts
        to modify elements of the stored data don't work.

        struct ChipRecord = // Chip Times record
        {
        time_t hiStartTime; // Start Time
        // much more...} timeWork; // entrant info records

        typedef map<int, ChipRecordBCI;
        BCI bci;
        map<int, ChipRecord>::it erator bIter;

        nBib = 17;
        time_t cTime = 11151544;

        // initialize and populate "timeWork" structure, etc.
        bci.insert(bci: :value_type(nBi b, timeWork));
        //...
        // obtain a cTime value...
        bIter =3D bci.find(nBib);
        if(bIter !=3D bci.end())
        {
        timeWork = bIter->second; //???
        timeWork = bci.find(nBib)->second; // ???
        timeWork = (*bIter).second ; // ???
        if(cTime timeWork.hiStar tTime) // use highest time
        { // update the object
        bIter->second.hiStart Time = cTime; // ???
        timeWork.hiStar tTime = cTime; // ???
        } // if
        } // if

        It's the code directly above that doesn't do what I intend: change
        the map object's value and update the object. =A0There is much more going
        on in the program, but this is the simplest amount that demonstrates the
        issue. =A0I don't know how to update the object. =A0Please advise. TIA
        >
        In your posted code you insert an element with the key bibNumber. But
        then you find with the key nBib. The former is not defined in your
        posted code.
        Yes, my apologies. I tried to post a simple segment of my program,
        and I made that mistake.
        Nevertheless, I still need help on this. I had hoped my code
        explained the problem... 8<{{

        Comment

        • Thomas J. Gritzan

          #5
          Re: Updating &lt;map&gt; Structure - Help!

          Mike Copeland schrieb:
          I'm having difficulty updating map objects. In the code I've
          excerpted below, I store map objects without difficulty, but my attempts
          to modify elements of the stored data don't work.
          What means "don't work" in your case? Didn't it compile? Does it give
          you false results? Explain what is wrong with your approach and what the
          expected result is.

          Also, please provide a *compilable* minimal example, with main()
          function and all the stuff.
          struct ChipRecord // Chip Times record
          {
          time_t hiStartTime; // Start Time
          // much more...
          } timeWork; // entrant info records
          typedef map<int, ChipRecordBCI;
          BCI bci;
          map<int, ChipRecord>::it erator bIter;
          >
          int nBib = 17;
          time_t cTime = 11151544;
          >
          >
          >
          // initialize and populate "timeWork" structure, etc.
          bci.insert(bci: :value_type(bib Number, timeWork));
          //...
          // obtain a cTime value...
          bIter = bci.find(nBib);
          It's better style to declare variables on first use. It's easier for the
          reader of your program (co-workers, and in some weeks, yourself) to
          understand the code:

          BCI::iterator bIter = bci.find(nBib);
          if(bIter != bci.end())
          {
          timeWork = bIter->second; // ???
          Same here:
          ChipRecord timeWork = bIter->second;

          Note: This makes a copy of the object in the map. Changing timeWork only
          changes this copy. If you want to modify the object inplace, take a
          reference.

          // pick a better name for the variable, too
          ChipRecord& timeWork = bIter->second;

          Changes to 'timeWork' will directly alter the object in the map.
          timeWork = bci.find(nBib)->second; // ???
          timeWork = (*bIter).second ; // ???
          (*bIter).second and bIter->second are the same.
          if(cTime timeWork.hiStar tTime) // use highest time
          { // update the object
          bIter->second.hiStart Time = cTime; // ???
          timeWork.hiStar tTime = cTime; // ???
          } // if
          } // if
          >
          It's the code directly above that doesn't do what I intend: change
          the map object's value and update the object. There is much more going
          on in the program, but this is the simplest amount that demonstrates the
          issue. I don't know how to update the object. Please advise. TIA
          --
          Thomas

          Comment

          • Paavo Helde

            #6
            Re: Updating &lt;map&gt; Structure - Help!

            mrc2323@cox.net (Mike Copeland) kirjutas:
            I'm having difficulty updating map objects. In the code I've
            excerpted below, I store map objects without difficulty, but my attempts
            to modify elements of the stored data don't work.
            [...]
            map<int, ChipRecord>::it erator bIter;
            [...]
            bIter = bci.find(nBib);
            if(bIter != bci.end())
            {
            [...]
            bIter->second.hiStart Time = cTime; // ???
            This seems to be basically correct, so your error is probably elsewhere.
            Try to prepare a compilable short example of the problem; during this you
            will probably find the cause of the problem; if not, post here again!

            hth
            Paavo

            Comment

            Working...