Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use at for STL list

How to use at for STL list

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
13 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V VC_RYK

    Hi Experts I need at() feature in my STL List. can anyone help me out? With Regards R e h a n

    M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #4

    std::list is not a "random access" sequence (i.e. cannot use a random access iterator).

    Watched code never compiles.

    1 Reply Last reply
    0
    • V VC_RYK

      Hi Experts I need at() feature in my STL List. can anyone help me out? With Regards R e h a n

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #5

      As Richard and the others have said, use vector if you can. If you really really really must have at-like functionality you can use std::advance to do it. Cheers, Ash

      V 1 Reply Last reply
      0
      • A Aescleal

        As Richard and the others have said, use vector if you can. If you really really really must have at-like functionality you can use std::advance to do it. Cheers, Ash

        V Offline
        V Offline
        VC_RYK
        wrote on last edited by
        #6

        Thanks a lot.... It is working......

        1 Reply Last reply
        0
        • V VC_RYK

          Hi Experts I need at() feature in my STL List. can anyone help me out? With Regards R e h a n

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #7

          What about:

          template <typename T> class mylist: public list<T>
          {
          public:
          T & at(int n)
          {
          int i;
          list::iterator it= this->begin();
          i = 0;
          while (it != this->end())
          {
          if ( i==n ) return *it;
          i++; it++;
          }
          throw( out_of_range("out of mylist range"));
          }
          };

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          V E 2 Replies Last reply
          0
          • CPalliniC CPallini

            What about:

            template <typename T> class mylist: public list<T>
            {
            public:
            T & at(int n)
            {
            int i;
            list::iterator it= this->begin();
            i = 0;
            while (it != this->end())
            {
            if ( i==n ) return *it;
            i++; it++;
            }
            throw( out_of_range("out of mylist range"));
            }
            };

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            V Offline
            V Offline
            VC_RYK
            wrote on last edited by
            #8

            Thanks Dear..

            1 Reply Last reply
            0
            • CPalliniC CPallini

              What about:

              template <typename T> class mylist: public list<T>
              {
              public:
              T & at(int n)
              {
              int i;
              list::iterator it= this->begin();
              i = 0;
              while (it != this->end())
              {
              if ( i==n ) return *it;
              i++; it++;
              }
              throw( out_of_range("out of mylist range"));
              }
              };

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              E Offline
              E Offline
              El Corazon
              wrote on last edited by
              #9

              depending on how the at() is used you can shortcut operations by holding the current iterator around as long as add/delete/insert operations have not been done. Using the difference between last at() and current at() you can move up or down the list. It takes some work, but if you are doing big searches you wouldn't always want to start at the beginning unless the new at() command is closer to the start or the end. :) and if you do add/delete/insert, just invalidate the iterator, and start over as you have done.

              _________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....

              CPalliniC 1 Reply Last reply
              0
              • V VC_RYK

                Hi Experts I need at() feature in my STL List. can anyone help me out? With Regards R e h a n

                E Offline
                E Offline
                El Corazon
                wrote on last edited by
                #10

                If you use at() often you might consider something like a skip-list: http://www.codersource.net/microsoft-net/c-advanced/skip-list-a-simpler-alternative-to-binary-trees.aspx[^] Skip lists would allow you to skip sequentially starting from the beginning on ever at() command.

                _________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....

                1 Reply Last reply
                0
                • E El Corazon

                  depending on how the at() is used you can shortcut operations by holding the current iterator around as long as add/delete/insert operations have not been done. Using the difference between last at() and current at() you can move up or down the list. It takes some work, but if you are doing big searches you wouldn't always want to start at the beginning unless the new at() command is closer to the start or the end. :) and if you do add/delete/insert, just invalidate the iterator, and start over as you have done.

                  _________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #11

                  Clever. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  E 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Clever. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    E Offline
                    E Offline
                    El Corazon
                    wrote on last edited by
                    #12

                    CPallini wrote:

                    Clever.

                    naw... comes from using skiplists, which made me think that wouldn't be a bad method here if there aren't many add-deletes, skip-lists let you jump around lists rapidly.

                    _________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....

                    CPalliniC 1 Reply Last reply
                    0
                    • E El Corazon

                      CPallini wrote:

                      Clever.

                      naw... comes from using skiplists, which made me think that wouldn't be a bad method here if there aren't many add-deletes, skip-lists let you jump around lists rapidly.

                      _________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #13

                      Well, skiplists (I didn't know about) are clever, at lest... :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups