Print Joseph ring using vector

Source: Internet
Author: User

I recently read the book C ++ primer. In the container chapter, I have a preliminary understanding of the container, and I mentioned the old problem of Joseph Ring in some interviews, A common idea is to use a circular linked list or array.

Here, the trainer decides to use the vector sequence container for implementation:

 # Include <vector> <br/> # include <iostream> <br/> using namespace STD; </P> <p> const int n = 1; <br/> const int M = 1; <br/> int main (INT argc, char * argv []) <br/>{< br/> vector <int> ring; <br/> // initialize <br/> for (INT I = 0; I <N; I ++) <br/> ring. push_back (I + 1); </P> <p> vector <int>: iterator ibegin = ring. begin (); <br/> vector <int>: iterator iend; <br/> while (! Ring. empty () <br/>{< br/> iend = ring. end (); <br/> If (ibegin = iend) <br/> ibegin = ring. begin (); </P> <p> for (INT I = 1; I <m; I ++) <br/>{< br/> ibegin ++; <br/> If (ibegin> = iend) <br/> ibegin = ring. begin (); <br/>}< br/> cout <* ibegin <Endl; <br/> ibegin = ring. erase (ibegin); <br/>}< br/>}

During the process, the iterator is not very familiar with the problem:

"Vector iterators incompatible"

Find the cause on the network and summarize it as follows:

 

Vector can implement erase in any way. It is not guaranteed that after an element of erase, subsequent elements must be moved to the location referenced by this iterator (address ). Of course, this is true in almost all STL implementations, which is why there is no problem after compiling with vc6. However, if we use a list or map instead of a vector, the program will crash without hesitation.

The correct method is as follows:

The erase implementation of all the containers in STL will return an iterator, which points to "the successor element of the currently deleted element, or end ()"

Therefore, after deleting an element through erase while traversing all elements of the container, the erase return value is assigned to the iteration variable.

 

 

Hope you can give me some advice ......

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.