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. ATL / WTL / STL
  4. Help with STL list::insert

Help with STL list::insert

Scheduled Pinned Locked Moved ATL / WTL / STL
c++helpquestion
17 Posts 3 Posters 40 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi I need a little guidance trying to insert a element my element is a structure here below are my data structures so I define a structure of type tcbholder struct tcbholder tcbx; I initialize the iterator member with the following code

    tcbx.straverse = tcbx.strptr.begin(); // storage discriptor pointer
    tcbx.stdecsx.blktraverse = tcbx.stdecsx.ablkptr.begin(); // begin block pointer

    then to insert a blkdesc type I use the following code

    tcbx.stdecsx.ablkptr.insert(tcbx.stdecsx.blktraverse,tcbx.stdecsx.ablkdescx);

    I then think I would have to bump up the postion of the iterator so I do the following

    tcbx.stdecsx.blktraverse++;

    after which I get an exception that I went past the end Any help would be appreciated thanks My first question is am I correct that in initalizing the iterator the code is call list::begin and then to bump up the interator to get next postion its operator ++

    struct blkdesc
    {
    char type;
    int blkaddr;
    int blklen;
    // struct blkdesc* nextblkdesc;
    };
    struct stdecs
    {
    struct vsmdesc stordesc;
    char* tcb;
    struct blkdesc ablkdescx;
    struct blkdesc fblkdescx;
    // struct stdecs* nextdecs;

    	list ablkptr;
    	list::iterator blktraverse;
    	list fblkptr;
    };
    
    struct tcbholder
    {
    	char\* tcb;
    	char programname\[8\];
    	//	struct stdecs \*storageptr;
    	struct stdecs stdecsx;
    	list  strptr;
    	list ::iterator stfirstptr;
    	list ::iterator straverse;
    };
    
    G L 2 Replies Last reply
    0
    • F ForNow

      Hi I need a little guidance trying to insert a element my element is a structure here below are my data structures so I define a structure of type tcbholder struct tcbholder tcbx; I initialize the iterator member with the following code

      tcbx.straverse = tcbx.strptr.begin(); // storage discriptor pointer
      tcbx.stdecsx.blktraverse = tcbx.stdecsx.ablkptr.begin(); // begin block pointer

      then to insert a blkdesc type I use the following code

      tcbx.stdecsx.ablkptr.insert(tcbx.stdecsx.blktraverse,tcbx.stdecsx.ablkdescx);

      I then think I would have to bump up the postion of the iterator so I do the following

      tcbx.stdecsx.blktraverse++;

      after which I get an exception that I went past the end Any help would be appreciated thanks My first question is am I correct that in initalizing the iterator the code is call list::begin and then to bump up the interator to get next postion its operator ++

      struct blkdesc
      {
      char type;
      int blkaddr;
      int blklen;
      // struct blkdesc* nextblkdesc;
      };
      struct stdecs
      {
      struct vsmdesc stordesc;
      char* tcb;
      struct blkdesc ablkdescx;
      struct blkdesc fblkdescx;
      // struct stdecs* nextdecs;

      	list ablkptr;
      	list::iterator blktraverse;
      	list fblkptr;
      };
      
      struct tcbholder
      {
      	char\* tcb;
      	char programname\[8\];
      	//	struct stdecs \*storageptr;
      	struct stdecs stdecsx;
      	list  strptr;
      	list ::iterator stfirstptr;
      	list ::iterator straverse;
      };
      
      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      You are correct that begin() will initialize an iterator, and that the ++ operator will advance it. I'm not sure why you are storing iterators though - if you want fast random access you might be better off with a std::vector. But if all you want to do is add items to the list, push_front() and push_back() (or emplace_front() and emplace_back()) are easier to use. insert() is more useful for adding items in the middle of the list.

      F 1 Reply Last reply
      0
      • G Graham Breach

        You are correct that begin() will initialize an iterator, and that the ++ operator will advance it. I'm not sure why you are storing iterators though - if you want fast random access you might be better off with a std::vector. But if all you want to do is add items to the list, push_front() and push_back() (or emplace_front() and emplace_back()) are easier to use. insert() is more useful for adding items in the middle of the list.

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        with insert when I add items and then use the ++ operator on the iterator I get an exception ? that I went past the end what I am trying to do is add item 1 and have on the top of the list add item 2 and have it the second on the list. not sure push_back do that with push_back if for arguments sake there are lets say room for 10 items and I add 3 so the first would be number 10 and when adding the second it would be number 9 and when adding the 3rd it would number 8. If later on I would like to retrieve the first I would have to know that the first is number 8 How would I reference that with an iterator ? thanks

        G 1 Reply Last reply
        0
        • F ForNow

          with insert when I add items and then use the ++ operator on the iterator I get an exception ? that I went past the end what I am trying to do is add item 1 and have on the top of the list add item 2 and have it the second on the list. not sure push_back do that with push_back if for arguments sake there are lets say room for 10 items and I add 3 so the first would be number 10 and when adding the second it would be number 9 and when adding the 3rd it would number 8. If later on I would like to retrieve the first I would have to know that the first is number 8 How would I reference that with an iterator ? thanks

          G Offline
          G Offline
          Graham Breach
          wrote on last edited by
          #4

          If you want the first element of a list you can use list.front() and for the last element list.back(). The push_front() function adds elements at the start of the list and push_back() adds them at the end. The list doesn't start off with any fixed length, it grows and shrinks as you add and remove elements. So if I was adding two items to a list I would do something like this:

          ItemType item1, item2;
          std::list my_list;

          my_list.push_back(item1);
          my_list.push_back(item2);

          You should use insert() when you want to add an item in the middle of the list - the iterator you pass in should point to the element you want to insert the new item in front of.

          F 1 Reply Last reply
          0
          • G Graham Breach

            If you want the first element of a list you can use list.front() and for the last element list.back(). The push_front() function adds elements at the start of the list and push_back() adds them at the end. The list doesn't start off with any fixed length, it grows and shrinks as you add and remove elements. So if I was adding two items to a list I would do something like this:

            ItemType item1, item2;
            std::list my_list;

            my_list.push_back(item1);
            my_list.push_back(item2);

            You should use insert() when you want to add an item in the middle of the list - the iterator you pass in should point to the element you want to insert the new item in front of.

            F Offline
            F Offline
            ForNow
            wrote on last edited by
            #5

            Okay then how I would traverse the list. If I do pop_front its gets the first element but the doc says it DELETES it as well I want to maintain the list I have it associated with a items in a dropdown combo box. When using insert you are providing the iterator my question is how do get value after I do list::begin to get the first when I bump it up with ++ operator I get an exception that I want past the end.

            G 1 Reply Last reply
            0
            • F ForNow

              Hi I need a little guidance trying to insert a element my element is a structure here below are my data structures so I define a structure of type tcbholder struct tcbholder tcbx; I initialize the iterator member with the following code

              tcbx.straverse = tcbx.strptr.begin(); // storage discriptor pointer
              tcbx.stdecsx.blktraverse = tcbx.stdecsx.ablkptr.begin(); // begin block pointer

              then to insert a blkdesc type I use the following code

              tcbx.stdecsx.ablkptr.insert(tcbx.stdecsx.blktraverse,tcbx.stdecsx.ablkdescx);

              I then think I would have to bump up the postion of the iterator so I do the following

              tcbx.stdecsx.blktraverse++;

              after which I get an exception that I went past the end Any help would be appreciated thanks My first question is am I correct that in initalizing the iterator the code is call list::begin and then to bump up the interator to get next postion its operator ++

              struct blkdesc
              {
              char type;
              int blkaddr;
              int blklen;
              // struct blkdesc* nextblkdesc;
              };
              struct stdecs
              {
              struct vsmdesc stordesc;
              char* tcb;
              struct blkdesc ablkdescx;
              struct blkdesc fblkdescx;
              // struct stdecs* nextdecs;

              	list ablkptr;
              	list::iterator blktraverse;
              	list fblkptr;
              };
              
              struct tcbholder
              {
              	char\* tcb;
              	char programname\[8\];
              	//	struct stdecs \*storageptr;
              	struct stdecs stdecsx;
              	list  strptr;
              	list ::iterator stfirstptr;
              	list ::iterator straverse;
              };
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              It appears from my test that using a saved iterator will yield inconsistent results. I have a list with a single element. After adding an item before the saved iterator it still points to the first element; i.e. not the newly inserted value. If I then increment the iterator it points to an invalid address. This is reasonable as the begin and end iterators are dynamically adjusted as the list increases or decreases. So the moment you add or remove an item, your saved iterator can no longer be relied upon. The take home message is - don't do it this way, use the proper member functions of the std::list.

              F 1 Reply Last reply
              0
              • F ForNow

                Okay then how I would traverse the list. If I do pop_front its gets the first element but the doc says it DELETES it as well I want to maintain the list I have it associated with a items in a dropdown combo box. When using insert you are providing the iterator my question is how do get value after I do list::begin to get the first when I bump it up with ++ operator I get an exception that I want past the end.

                G Offline
                G Offline
                Graham Breach
                wrote on last edited by
                #7

                When you want to traverse the list get an iterator with begin(). This is the first element in the list. Access the information you need, then increment the iterator. If it is equal to end() then you have reached (one item past) the end of the list:

                for(auto i = my_list.begin(); i != my_list.end(); ++i)
                {
                // do something with iterator i
                std::cout << "Item " << i->name << "\n";
                }

                Or you could use range-based for:

                for(auto& i : my_list)
                {
                // do something with i, which is a reference to the item in the list
                std::cout << "Item " << i.name << "\n";
                }

                F 1 Reply Last reply
                0
                • G Graham Breach

                  When you want to traverse the list get an iterator with begin(). This is the first element in the list. Access the information you need, then increment the iterator. If it is equal to end() then you have reached (one item past) the end of the list:

                  for(auto i = my_list.begin(); i != my_list.end(); ++i)
                  {
                  // do something with iterator i
                  std::cout << "Item " << i->name << "\n";
                  }

                  Or you could use range-based for:

                  for(auto& i : my_list)
                  {
                  // do something with i, which is a reference to the item in the list
                  std::cout << "Item " << i.name << "\n";
                  }

                  F Offline
                  F Offline
                  ForNow
                  wrote on last edited by
                  #8

                  so I can use the iterator as a pointer to reference members of my type which is a structure Cool

                  1 Reply Last reply
                  0
                  • L Lost User

                    It appears from my test that using a saved iterator will yield inconsistent results. I have a list with a single element. After adding an item before the saved iterator it still points to the first element; i.e. not the newly inserted value. If I then increment the iterator it points to an invalid address. This is reasonable as the begin and end iterators are dynamically adjusted as the list increases or decreases. So the moment you add or remove an item, your saved iterator can no longer be relied upon. The take home message is - don't do it this way, use the proper member functions of the std::list.

                    F Offline
                    F Offline
                    ForNow
                    wrote on last edited by
                    #9

                    Richard not sure how you add items with insert I get the initial pointer from begin but incrementing it with the ++ operator doesn't yield valid results I think the push_back will have things in the right order I am not sure what value the iterator should have when using insert to add an item with push_back I think the method allocates the storage

                    L 1 Reply Last reply
                    0
                    • F ForNow

                      Richard not sure how you add items with insert I get the initial pointer from begin but incrementing it with the ++ operator doesn't yield valid results I think the push_back will have things in the right order I am not sure what value the iterator should have when using insert to add an item with push_back I think the method allocates the storage

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      I think you missed the point. The begin and end iterators are dynamic and are recalculated every time you insert or remove an element from the list. In your case you capture the begin iterator which points to the first element of the list. You then insert an item at the front of the list, so your saved iterator is no longer valid. You then increment it so it could, quite reasonably, point beyond the end of the list. To use iterators properly you must call the begin and end methods of the list each time you need their values.

                      F 1 Reply Last reply
                      0
                      • L Lost User

                        I think you missed the point. The begin and end iterators are dynamic and are recalculated every time you insert or remove an element from the list. In your case you capture the begin iterator which points to the first element of the list. You then insert an item at the front of the list, so your saved iterator is no longer valid. You then increment it so it could, quite reasonably, point beyond the end of the list. To use iterators properly you must call the begin and end methods of the list each time you need their values.

                        F Offline
                        F Offline
                        ForNow
                        wrote on last edited by
                        #11

                        Richard Just one question then when calling insert you provide as the first parameter an iterator how to do you get a value for that Do allocate the storage with for instance new Not sure

                        L 1 Reply Last reply
                        0
                        • F ForNow

                          Richard Just one question then when calling insert you provide as the first parameter an iterator how to do you get a value for that Do allocate the storage with for instance new Not sure

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          The iterator is controlled by the template class. So to get the current value of an iterator you must call begin or end. As I said previously these values are not fixed, but must be recalculated each time the list changes. You can easily test this with a simple list of integer. Do some inserts and deletes and display the saved iterators after each action.

                          F 1 Reply Last reply
                          0
                          • L Lost User

                            The iterator is controlled by the template class. So to get the current value of an iterator you must call begin or end. As I said previously these values are not fixed, but must be recalculated each time the list changes. You can easily test this with a simple list of integer. Do some inserts and deletes and display the saved iterators after each action.

                            F Offline
                            F Offline
                            ForNow
                            wrote on last edited by
                            #13

                            In order to do an insert I have to get a value for a iterator as the documentation says you provide that to insert In all my google searches I have never seen how that’s done I understand begin starts a iterator with a initial value how do I get a value for it when inserting the second or third item the using the ++ operator gives me an exception

                            L 1 Reply Last reply
                            0
                            • F ForNow

                              In order to do an insert I have to get a value for a iterator as the documentation says you provide that to insert In all my google searches I have never seen how that’s done I understand begin starts a iterator with a initial value how do I get a value for it when inserting the second or third item the using the ++ operator gives me an exception

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              As I keep saying: To get an iterator you must call one of the functions listed under the title Iterators at std::list - cppreference.com[^]. If you then insert an element in front of the iterator then it is no longer valid. So before you increment it call begin a second time to ensure you have the current value.

                              F 1 Reply Last reply
                              0
                              • L Lost User

                                As I keep saying: To get an iterator you must call one of the functions listed under the title Iterators at std::list - cppreference.com[^]. If you then insert an element in front of the iterator then it is no longer valid. So before you increment it call begin a second time to ensure you have the current value.

                                F Offline
                                F Offline
                                ForNow
                                wrote on last edited by
                                #15

                                So to put into code what you just said List mynode; List ::iterator it: It = mynode.begin(); mynode.insert(it,valueref); It = mynode.begin(); It++; mynode.insert(it,valueref); It = mynode.begin(); It+=2; mynode.insert(it,valueref); It = mynode.begin(); It+=3; mynode(it,valueref); I’ll try this out Thanks

                                L 1 Reply Last reply
                                0
                                • F ForNow

                                  So to put into code what you just said List mynode; List ::iterator it: It = mynode.begin(); mynode.insert(it,valueref); It = mynode.begin(); It++; mynode.insert(it,valueref); It = mynode.begin(); It+=2; mynode.insert(it,valueref); It = mynode.begin(); It+=3; mynode(it,valueref); I’ll try this out Thanks

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  There are easier ways of adding more than one element the way you want. Take a closer look at the insert page and maybe use one of:

                                  iterator insert( const_iterator pos, InputIt first, InputIt last );
                                  iterator insert( const_iterator pos, std::initializer_list ilist );
                                  (5) (since C++11)

                                  If you create a list of your values first using push_back, then you can add them in one go at whatever point you need in your main list. Or you could use push_front passing your elements in reverse order.

                                  F 1 Reply Last reply
                                  0
                                  • L Lost User

                                    There are easier ways of adding more than one element the way you want. Take a closer look at the insert page and maybe use one of:

                                    iterator insert( const_iterator pos, InputIt first, InputIt last );
                                    iterator insert( const_iterator pos, std::initializer_list ilist );
                                    (5) (since C++11)

                                    If you create a list of your values first using push_back, then you can add them in one go at whatever point you need in your main list. Or you could use push_front passing your elements in reverse order.

                                    F Offline
                                    F Offline
                                    ForNow
                                    wrote on last edited by
                                    #17

                                    My insert is in the middle of for(;;) loop but I understand what you are saying thanks

                                    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