Help with STL list::insert
-
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.
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 toend()
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";
} -
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 toend()
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";
} -
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
andend
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 thestd::list
.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
-
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
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
andend
methods of the list each time you need their values. -
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
andend
methods of the list each time you need their values. -
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
The iterator is controlled by the template class. So to get the current value of an iterator you must call
begin
orend
. 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. -
The iterator is controlled by the template class. So to get the current value of an iterator you must call
begin
orend
. 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.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
-
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
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. -
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.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
-
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
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 usepush_front
passing your elements in reverse order. -
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 usepush_front
passing your elements in reverse order.