c++ primer笔记1

1. 基本数据类型

容器定义的类型别名  
size_type Unsigned integral type large enough to hold size of largest possible container of this container type
iterator Type of the iterator for this container type
const_iterator Type of the iterator that can read but not write the elements
reverse_iterator Iterator that addresses elements in reverse order
const_reverse_iterator Reverse iterator that can read but not write the elements
difference_type Signed integral type large enough to hold the difference, which might be negative, between two iterators
value_type Element type
reference Element’s lvalue type; synonym for value_type&
const_reference Element’s const lvalue type; same as const value_type&
Types Defined by the map Class  
map::key_type The type of the keys used to index the map.
map::mapped_type The type of the values associated with the keys in the map.
map::value_type A pair whose first element has type const map::key_type and second has type map::mapped_type
iterator的类型成员  
value_type 该迭代器实际指向的数据的类型T
pointer T *
reference T&

2、对迭代器解引用返回的是元素的引用

if (!ilist.empty()) 
{
// val and val2 refer to the same element
list<int>::reference val = *ilist.begin();
list<int>::reference val2 = ilist.front();

// last and last2 refer to the same element
list<int>::reference last = *--ilist.end();
list<int>::reference last2 = ilist.back();
}

3、map的下标操作

map <string, int> word_count;
word_count["Anna"] = 1;
  1. word_count is searched for the element whose key is Anna . The element is not found.
  2. A new keyvalue pair is inserted into word_count . The key is a const string holding Anna .
    The value is value initialized, meaning in this case that the value is 0.
  3. The new keyvalue pair is inserted into word_count .
  4. The newly inserted element is fetched and is given the value 1.

For map , if the key is not already present, a new element is created and inserted into the map for that key.

4、Algorithms Never Execute Container Operations

The generic algorithms do not themselves execute container operations. They operate solely in terms of iterators and iterator operations. The fact that the algorithms operate in terms of iterators and not container operations has a perhaps surprising but essential implication: When used on “ordinary” iterators, algorithms never change the size of the underlying container. As we’ll see, algorithms may change the values of the elements stored in the container, and they may move elements around within the
container. They do not, however, ever add or remove elements directly.
There is a special class of iterator, the inserters, that do more than traverse the sequence to which they are bound. When we assign to these iterators, they execute insert operations on the underlying container. When an algorithm operates on one of these
iterators, the iterator may have the effect of adding elements to the container. The algorithm itself, however, never does so.

5、类的前向声明

An incomplete type can be used only in limited ways. Objects of the type may not be defined. An incomplete type may be used to define only pointers or references to the type or to declare (but not define) functions that use the type as a paremeter or return type.

6、Returning *this from a const Member Function

In an ordinary non const member function, the type of this is a const pointer to the class type. We may change the value to which this points but cannot change the address that this holds. In a const member function, the type of this is a const pointer to a const class-type object. We may change neither the object to which this points nor the address that this holds.
We cannot return a plain reference to the class object from a const member function. A const member function may return *this only as a const reference.

class Screen
{

public:
const Screen& print() const
{
cout << "print" << endl;
return *this;
}
};

7、Overloading Based on const

We can overload a member function based on whether it is const for the same reasons that we can overload a function based on whether a pointer parameter points to const. A const object will use only the const member. A non const object could use
either member, but the non const version is a better match.

class Screen 
{
public:
Screen& display(std::ostream &os)
{ do_display(os); return *this; }

const Screen& display(std::ostream &os) const
{ do_display(os); return *this; }

private:
void do_display(std::ostream &os) const
{ os << contents; }

};

8、Parameter Lists and Function Bodies Are in Class Scope

In a member function defined outside the class, the parameter list and member-function body both appear after the member name. These are defined inside the class scope and so may refer to other class members without qualification.

9、Function Return Types Aren’t Always in Class Scope

In contrast to the parameter types, the return type appears before the member name. If the function is defined outside the class body, then the name used for the return type is outside the class scope. If the return type uses a type defined by the class, it must use the fully qualified name.

10、类成员声明中的名字查找(Name Lookup for Class Member Declarations)

1、The declarations of the class members that appear before the use of the name are considered.
2、If the lookup in step 1 is not successful, the declarations that appear in the scope in which the class is defined, and that appear before the class definition itself, are considered.

11、类成员定义中的名字查找(Name Lookup in Class Member Definitions)

1、Declarations in the member-function local scopes are considered first.
2、If the a declaration for the name is not found in the member function, the declarations for all the class members are considered.
3、If a declaration for the name is not found in the class, the declarations that appear in scope before the member function definition are considered.

int height;
class Screen
{
public:
void dummy_fcn(int height)
{
cursor = width * height;
}
private:
int cursor;
int height, width;
};

dummy_fun函数内赋值语句中的height指的是dummy_fun函数参数中的height。如果想使用Screen中的成员height,则cursor = width * Screen::height; 如果想使用全局变量height,则cursor = width * ::height;

12、typedef 引起的微妙错误

typedef string Type;
Type initVal();
class Exercise
{

public:
typedef double Type;
Type setVal(Type); // Type为double
Type initVal(); // Type为double
private:
int val;
};

// 注意,在类的外部定义函数时,返回值的类型
Exercise::Type Exercise::setVal(Type parm)
{
val = parm + initVal();
}

如果把typedef double Type放到setVal和initVal的后面,就会出现错误。因为按照类成员声明的名字查找规则,setVal中的Type为string。等到编译器解析到typedef double Type语句时,Type有两种意思了,于是发生编译错误。

error: declaration of ‘typedef double Exercise::Type’
error: changes meaning of ‘Type’ from ‘typedef struct std::string Type’