-2

I am new to C++ and I have a query:

"How in list all iterators and references unaffected after any operation"

I googled a lot and everywhere I found that in list iterators invalidation remains unaffected after any operation, so I just want to know what is the back end process that occurs when we perform some operation on list that doesn't create any impact on list iterators.

 list<string> str;
 str.push_back("One");
 str.push_back("Two"); // So this doesn't affect the first iterator?

Some of the basics rules I referred are from this article: Iterator invalidation rules

Community
  • 1
  • 1
  • `list str = "";` won't compile. And what for did you add the line of code which has nothing to do with the question? – Sergey Sep 12 '16 at 10:02
  • i kept this line, because stack over flow was giving warning , your question is not having any code lines..something like that.... so for name shake i kept... – Sanjeev Kumar Sep 12 '16 at 10:04
  • @SanjeevKumar: When learning to program, one very important lesson is to heed warnings. Don't just put in something to make the warning disappear, fix the underlying problem. – MSalters Sep 12 '16 at 10:28

1 Answers1

2

std::list is an implementation of the classic double-linked list. Each value is stored in some node, with pointers to the previous and next node. Each list iterator refers to such a node - except for list.end() which is a special case.

This also means that the node exists as long as the particular list element exists. Adding or removing elements elsewhere in the list at most would affect the previous or next pointer of a node, but the node itself continues to exist.

MSalters
  • 173,980
  • 10
  • 155
  • 350