Review Summary C ++ (b)

size_type type

string :: size_type a string :: size () function returns the value, he is an unsigned integer, the following code will return true;

bool main(void)
{
	string world;
	auto len = world.size(); // 虽然len=0但他是无符号的因此满足<-1
	if (len < -1) return true;
	else return false;
}

vector container

C ++ primer pointed out that the fifth edition of P91 page, vector unless all values are the same, more efficient way is to define an empty vector, and to add value at runtime.
Any use of the iterative loop or a range for the loop body can not add an operating element.

After the end of the iterator

After the end of the iterator iterator means a position after the last point of the container element, not only for the vessel, in fact, in the array has a similar definition. The following example, int * b = & nums2 [10]; is legal, although nums maximum index should be 9, but this statement is defined as the pointer end of the pointer array b nums2 is correct.

	vector<int> nums = { 1,2,3,4,5,6,7,8,9,10 };
	auto end = nums.end();
	int nums2[10] = {};
	int* b = &nums2[10];

Built-in index can be negative

The array index may be negative, subscripting reconciliation references are equal. For example, the following code is legal. However, a container such as vector index is an unsigned int, can not accept negative subscripts.

int nums2[10] = {};
int *b = &nums2[2];
int a = b[-2]; // 合法,相当于a = *(b-2);

Range for auto cycle and

To use the statement for processing multi-dimensional array range, in addition to the innermost loop, the loop control variables of all types should be referenced. A second segment following illegal code for (auto row: a) in, row should be int [4] of the variable type. However, since it is not a reference type, it is automatically converted into an array of pointers to elements of the first array element within the compiler initializes row. Therefore row is of type int *, could not be scope for.

	int a[3][4];
	// 合法
	for (auto &row : a)
		for (auto &col : row)
		{
			...
		}
	// 非法,此处auto row : a,row被定义成int *类型。
	for (auto row : a)
		for (auto &col : row)
		{
			...
		}
Released six original articles · won praise 0 · Views 91

Guess you like

Origin blog.csdn.net/qq_33584870/article/details/104655030