C + + Primer Learning Note Chapter 9

Source: Internet
Author: User

9.1 Sequential Container Sequential Containers

Vector: Fast random access,

List: Quick Insert/delete,

Deque: Double-ended queue

9.2 Scope of iterators and iterators

Iterator Operation:

*iter: Returns a reference to the element that ITER points to.

Iter->mem: The dereference of ITER is equivalent to (*iter). Mem.

++iter and iter++: Add 1 to ITER to point to the next element.

--iter and iter--: points to the next element.

Iter1 = = Iter2 and Iter1! = Iter2: Two iterators are the same element of the same container, or both point to the container when it exceeds the end of the next position when equal.

To calculate the center position of a vector object:

Vector<int>::iterator iter = Vec.begin () + vec.size ()/2;

The list container does not support arithmetic operations of iterators

Exercises Section 9.2

Exercise 9.7: The following code compiles an error, and the list's iterator does not support relational operator operations

list<int> lst1;list<int>::iterator iter1 == lst1.end ();  while (Iter1 < iter2) {  break;}

Exercise 9.8: Execute if Inner statement when iterator string is empty

  Exercise 9.8:write a loop to Write the elements of a list reverse order.

#include <iostream>
#include <list>
#include <vector>
#include <sstream>

using namespace Std;

void Exercize_9_9 ();

int main () {
Exercize_9_9 ();
return 0;
}

void Exercize_9_9 () {
List<string> List1;
for (short i = 0; i < ++i) {
StringStream SS;
SS << I;
List1.push_back ("string" + ss.str ());
}

List<string>::iterator it = List1.end ();
Do
{
--it;
string s = *it;
cout << s.c_str () << Endl;
}
while (It! = List1.begin ());
}

Exercise 9.10:which, if any, of the following iterator uses is in error?

Constvector<int> Ivec (Ten); Vector<string> Svec (Ten); list<int> IList (Ten);(a) vector<int>::iterator it =Ivec.begin ();//Error: The iterator in Ivec is const, changed toVector<int>::const_iterator
(b) List<int>::iterator it = ilist.begin () +2;//Error: An iterator in list cannot perform arithmetic plus minus operations
(c) Vector<string>::iterator it = &svec[0]; Error: Svec[0], is a character string.
(d) for(vector<string>:: Iterator it= Svec.begin (); It! =0; ++IT)//error, it! = 0 change to it! = Svec.end ()
//....

C + + Primer Learning Note Chapter 9

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.