12-13-2018学习-2.1&10.3

1、字符类型有两种:char 和 wchar_t。char 类型保证了有足够的空间,能够存储机器基本字符集中任何字符相应的数值,因此,char 类型通常是半个机器字节(byte)。wchar_t 类型用于扩展字符集,比如汉字和日语,这些字符集中的一些字符不能用单个 char 表示。

2、C++ 中,把负值赋给 unsigned 对象是完全合法的,其结果是该负数对该类型的取值个数求模后的值。所以,如果把 -1 赋给8位的 unsigned char,那么结果是 255,因为 255 是 -1 对 256 求模后的值。

3、类型 float、 double 和 long double 分别表示单精度浮点数、双精度浮点数和扩展精度浮点数。一般 float 类型用一个字(32 位)来表示,double 类型用两个字(64 位)来表示,long double 类型用三个或四个字(96 或 128 位)来表示。类型的取值范围决定了浮点数所含的有效数字位数。    Float一般至少能够表示6位有效数字,Double至少能够表示10位有效数字。

4、事实上,在某些应用中 char 类型被当作 signed 类型,在另外一些应用中则被当作 unsigned 类型,因此把 char 类型作为计算类型使用时容易出问题。

5、使用 double 类型基本上不会有错。在 float 类型中隐式的精度损失是不能忽视的,而 double 类型精度代价相对于 float 类型精度代价可以忽略。事实上,有些机器上,double 类型比 float 类型的计算要快得多。long double 类型提供的精度通常没有必要,而且还需要承担额外的运行代价。

6、C++ 定义了一组表示整数、浮点数、单个字符和布尔值的算术类型,另外还定义了一种称为 void 的特殊类型。void 类型没有对应的值,仅用在有限的一些情况下,通常用作无返回值函数的返回类型。

7、8位为一个字节,32位或4个字节为一个“字”(word)。一个机器字长为16位。int为16位(bits),long为32位(bits)

 


1、C++ guarantees short and int is at least 16 bits, long at least 32 bits, long long at least 64 bits.

     The signed can represent positive numbers, negative numbers and zero, while unsigned can only represent numbers no less than zero.

     The C and C++ standards do not specify the representation of float, double and long double. It is possible that all three implemented as IEEE double-precision. Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) float is indeed a IEEE single-precision floating point number (binary32), and double is a IEEE double-precision floating point number (binary64).

Usage:

  • Use int for integer arithmetic. short is usually too small and, in practice, long often has the same size as int. If your data values are larger than the minimum guaranteed size of an int, then use long long. (In a word: short < int < long < long long)

  • Use an unsigned type when you know that the values cannot be negative. (In a word: no negative, unsigned.)

  • Use double for floating-point computations; float usually does not have enough precision, and the cost of double-precision calculations versus single-precision is negligible. In fact, on some machines, double-precision operations are faster than single. The precision offered by long double usually is unnecessary and often entails considerable run-time cost. (In a word: float < double < long double)

10.3章

1、与其他容器一样,map 同样提供 begin 和 end 运算,以生成用于遍历整个容器的迭代器。例如,可如下将 map 容器 word_count 的内容输出:

     // get iterator positioned on the first element
     map::const_iterator
                             map_it = word_count.begin();
     // for each element in the map
     while (map_it != word_count.end()) {
         // print the element key, value pairs
         cout << map_it->first << " occurs "
              << map_it->second << " times" << endl;
         ++map_it; // increment iterator to denote the next element
     }

2、读取元素而不插入该元素

The find operation returns an iterator to the element or the end iterator if the element is not present:

find 操作返回指向元素的迭代器,如果元素不存在,则返回 end 迭代器:

     int occurs = 0;
     map::iterator it = word_count.find("foobar");
     if (it != word_count.end())
         occurs = it->second;
 

We should use find when we want to obtain a reference to the element with the specified key if it exists, and do not want to create the element if it does not exist. 

如果希望当具有指定键的元素存在时,就获取该元素的引用,否则就不在容器中创建新元素,那么应该使用 find

3、下标操作符给出了读取一个值的最简单方法:

     map word_count;
     int occurs = word_count["foobar"];
 

As we've seen, using a subscript has an important side effect: If that key is not already in the map, then subscript inserts an element with that key.

但是,使用下标存在一个很危险的副作用:如果该键不在 map 容器中,那么下标操作会插入一个具有该键的新元素。

Whether this behavior is correct depends on our expectations. In this example, if "foobar" weren't already present, it would be added to the map with an associated value of 0. In this case, occurs gets a value of 0.

这样的行为是否正确取决于程序员的意愿。在这个例子中,如果“foobar”不存在,则在 map 中插入具有该键的新元素,其关联的值为 0。在这种情况下,occurs 获得 0 值。

Our word-counting programs relied on the fact that subscripting

 

4、下面是使用 insert 重写的单词统计程序:

     // count number of times each word occurs in the input
     map word_count; // empty map from string to int
     string word;
     while (cin >> word) {
         // inserts element with key equal to word and value 1;
         // if word already in word_count, insert does nothing
         pair::iterator, bool> ret =
                   word_count.insert(make_pair(word, 1));
         if (!ret.second)          // word already in word_count
             ++ret.first->second;  // increment counter
     }

 

  • ret holds return value from insert, which is a pair. The first member of that pair is a map iterator referring to the key that was inserted.

    ret 存储 insert 函数返回的 pair 对象。该 pair 的 first 成员是一个 map 迭代器,指向插入的键。

  • ret.first fetches the map iterator from the pair returned by insert.

    ret.first 从 insert 返回的 pair 对象中获取 map 迭代器。

  • ret.first->second dereferences that iterator obtaining a value_type object. That object is also a pair, in which the second member is the value part of the element we added.

    ret.first->second 对该迭代器进行解引用,获得一个 value_type 类型的对象。这个对象同样是 pair 类型的,它的 second 成员即为我们所添加的元素的值部分。

  • ++ret.first->second increments that value.

    ++ret.first->second 实现该值的自增运算。 

5、传递给 insert 的实参相当笨拙。可用两种方法简化:使用 make_pair:

     word_count.insert(make_pair("Anna", 1));
 

Or use a typedef:

或使用 typedef

     typedef map::value_type valType;
     word_count.insert(valType("Anna", 1));
 

Either approach improves readability by making the call less complicated. 

这两种方法都使用调用变得简单,提高了程序的可读性。

6、

显然,map 迭代器返回 value_type 类型的值——包含 const key_type 和 mapped_type 类型成员的 pair 对象;下标操作符则返回一个 mapped_type 类型的值。

map 类额外定义了两种类型:key_type 和 mapped_type,以获得键或值的类型。对于 word_count,其 key_type 是 string 类型,而 mapped_type 则是 int 型。如同顺序容器(第 9.3.1 节)一样,可使用作用域操作符(scope operator)来获取类型成员,如 map::key_type。

 

对迭代器进行解引用时,将获得一个引用,指向容器中一个 value_type 类型的值。对于 map 容器,其 value_type 是 pair 类型:

     // get an iterator to an element in word_count
     map::iterator map_it = word_count.begin();

     // *map_it is a reference to a pair object
     cout << map_it->first;                  // prints the key for this element
     cout << " " << map_it->second;          // prints the value of the element
     map_it->first = "new key";              // error: key is const
     ++map_it->second;     // ok: we can change value through an iterator
 

Dereferencing the iterator yields a pair object in which first member holds the const key and second member holds the value.

对迭代器进行解引用将获得一个 pair 对象,它的 first 成员存放键,为 const,而 second 成员则存放值。

 

map::key_type

The type of the keys used to index the map.

在 map 容器中,用做索引的键的类型

map::mapped_type

The type of the values associated with the keys in the map.

在 map 容器中,键所关联的值的类型

map::value_type

A pair whose first element has type const map::key_type and second has type map::mapped_type.

一个 pair 类型,它的 first 元素具有 const map::key_type 类型,而 second 元素则为 map::mapped_type 类型

 

你可能感兴趣的:(12-13-2018学习-2.1&10.3)