关闭

Step-by-step learning C + +(chapter three summary____Library Types)

标签: librarytypesciteratorvectorstring
224人阅读 评论(0) 收藏 举报

Chapter Three Summary

1. namespace 

  • A using declaration introduces only one namespace member at a time.
  • Best Practice: In general, it is good practice for headers to define only what is strictly necessary.

                 Example:  use  using std:cout   rather than using namespace std

2. string:: size_type (One of Companion Types)

  •           string::size_type st = stringObject.size()
  •         Any variable used to store the result from the string size  operation ought to be of type string::size_type . It is particularly important not to assign the return from size to an int.
  • The safest way to hold the size of  a string is to use the type the library defines for the purpose (avoid contents of a file could easily exceed 32767 or another size ), which is string::size_type

3. Dealing with the characters of  a string object

  • Process the individual character of as string , these function are defined  in the cctype header

          Such as : isalnum(c), True if c is a letter or a digit

                          isalpha(c), True if c is a letter

                          islower(c), True if  c is lowercase letter

                         tolower(c), If c is  an uppercase letter,return its lowercase equivalent

4. Library vector Type

  • Vector as a container because it contains other objects
  • Vector is a class template
  • vector is not  a type,it is a template that we can use to define any number of types. vector<int>   and vector<string> are types
  • Preallocate a given number of  elements in a vector object
  • Value Initialization : vector<int> ivec(10);//10 elements each initialized to 0 (This character different array)
  • vector<int>::size_type st = ivec.size()
  • An element must exist in order to subscript it; elements are not added when we assign through a subscript

              vector<int> ivec;// empty vector

              cout << ivec[0]; //ERROR:ivec has no elements


                                           vector<int> ivec2(10); //vector has 10 elements

            run time error------- cout << ivec2[10]; //ERROR: ivec has elements subscript as 0...9

5. Iterator

  •  While we can use subscript to access the elements in a vector , the library also give us another way to example elements : an iterator (maybe pointer).
  • Each container defines a pair of functions named begin and end that return iterators.
  • The iterator returned by the end operation is an iterator positioned "One past the end" of the vector.
  • const_iterator Type: It should be  used when reading, but not writing to, the container elements.(Readonly)

                        Note:

                                   vector<int>:: const_iterator it;// an iterator that cannot write elements

                                   const vector<int>::iterator it; //an iterator whose value cannot change

6. Iterator arithmeric

  •  iter + n or iter - n :

           Doing so yields a new iterator .

           It position n elements ahead(addition) of or behind(subtraction) the elements to which iter refers.

  • iter1 - iter2

    Computes the difference between two iterators as a value of a signed integral type name different_type(signed int )


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4434次
    • 积分:159
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章分类
    文章存档