C++ Primer 读书笔记:(P168-P216)

一转眼三个月过去了,第二次翻开这本书。
知识还是得small bits taken often啊!

vector, string and array

const char *str = s.c_str(); // ok

The name c_str indicates that the function returns a C-style character string. That is, it returns a pointer to the beginning of a null-terminated character array that holds the same data as the characters in the string. The type of the pointer is const char*, which prevents us from changing the contents of the array. If a program needs continuing access to the contents of the array returned by str(), the program must copy the array returned by c_str.

// equivalent initialization without the optional nested braces for each row int
ia[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

// explicitly initialize only element 0 in **each** row
int ia[3][4] = {{ 0 }, { 4 }, { 8 }};

// explicitly initialize **row 0**; The remaining elements are initialized to 0
int ix[3][4] = {0, 3, 6, 9};

Type Aliases Simplify Pointers to Multidimensional Arrays

using int_array = int[4]; 
typedef int int_array[4]; // equivalent

Bit Manipulation

1UL << 27 // generate a value with only bit number 27 set

unsigned long quiz1 = 0; // we'll use this value as a collection of bits
quiz1 |= 1UL << 27; // indicate student number 27 passed
bool status = quiz1 & (1UL << 27); // how did student number 27 do?

Numeric literals can have suffixes that determine their types. These suffixes are optional, as the compiler can usually tell from context what kind of constant you’re intending.

unsigned int nValue = 5u; // unsigned int
long nValue2 = 5L; // long

Binary, Octal and hexadecimal literals

#include <iostream>
 
int main()
{
    int x = 012; // 0 before the number means this is octal
    std::cout << x; // 10

    int x = 0xF; // 0x before the number means this is hexadecimal
    std::cout << x;

    // binary:
    int bin(0);
    bin = 0b1;  // assign binary 0000 0001 to the variable
    bin = 0b11; // assign binary 0000 0011 to the variable
    bin = 0b1010; // assign binary 0000 1010 to the variable
    bin = 0b11110000; // assign binary 1111 0000 to the variable

    // easier to read
    int bin = 0b1011'0010;  // assign binary 1011 0010 to the variable
    long value = 2'532'673'462; // much easier to read than 2532673462
}

sizeof

The sizeof operator returns the size, in bytes, of an expression or a type name. The operator is right associative. The result of sizeof is a constant expression of type size_t.

Sales_data data, *p; 
sizeof(Sales_data); // size required to hold an object of type Sales_data 
sizeof data; // size of data's type, i.e., sizeof(Sales_data) 
sizeof p;  // size of a pointer 

// size of the type to which p points, i.e., sizeof(Sales_data) 
// because sizeof does not evaluate its operand
// it doesn’t matter that p is an invalid (i.e., uninitialized) pointer
// Dereferencing an invalid pointer as the operand to sizeof is safe
// because the pointer is not actually used
// sizeof doesn’t need dereference the pointer to know what type it will return
// size of the type to which p points, i.e., sizeof(Sales_data)
sizeof *p; 

sizeof data.revenue; // size of the type of Sales_data's revenue member 
sizeof Sales_data::revenue; // alternative way to get the size of revenue

sizeof a string or a vector returns only the size of the fixed part of these types; it does not return the size used by the object’s elements. sizeof() is computed at compile time, so there is no way it can tell you how many elements it has inside. Use the size() method of the vector object.

Because sizeof returns the size of the entire array, we can determine the number of elements in an array by dividing the array size by the element size:

// sizeof(ia)/sizeof(*ia) returns the number of elements in ia 
constexpr size_t sz = sizeof(ia)/sizeof(*ia); 
int arr2[sz]; // ok sizeof returns a constant expression
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,173评论 0 10
  • 居然想捡起小石头在操场的水涡上打水漂……小时候特别喜欢下水摸鱼——我还记得一清二楚
    爱自由的阅读 847评论 0 0
  • 当别人不想着去理你的时候,请你不要再去无趣的和别人说话了,你不是孩子了,应该知道爱你的表现是什么,爱你就是让你...
    那些三月花阅读 1,369评论 0 0
  • 这是我创业过程中犯的最大的错误之一 虽然按正常的逻辑应该是:等地铁修好了,减缓了主通道的人流后,政府的市政部门再来...
    谌基平阅读 1,540评论 0 0
  • 我们熟知的《红辣椒》、《千年女优》、《东京教父》皆出自于今敏之手,不论是《梦之安魂曲》、《黑天鹅》还是《盗梦空间》...
    与Winter的五百天阅读 6,036评论 0 6

友情链接更多精彩内容