C++ Primer 读书笔记2(一)

2015-01-26 23:13:02 · 作者: · 浏览: 18

1 Reada set of integers into a vector. Print the sum of each pair of adjacent elements.Change your program so that it prints the sum of the first and last elements,followed by the sum of the second and second-to- last, and so on.

        vector
  
    ivect;
	int value;
	
	while (cin >> value)
	{
		ivect.push_back(value);
	}
	for (decltype(ivect.size()) idx = 0; idx < ivect.size() - 1; idx += 2)
	{
		cout << ivect[idx] + ivect[idx + 1] << endl;
	}
	if (ivect.size() % 2 == 1)
		cout << ivect[ivect.size() - 1] << endl;

	//用迭代器实现
	vector
   
     ivect; int value; while (cin >> value) { ivect.push_back(value); } auto iter = ivect.begin(); for (; (iter != ivect.end()) && ((iter + 1) != ivect.end()); iter += 2) { cout << *iter + *(iter + 1) << " "; } if (iter != ivect.end()) { cout << *iter << endl; } vector
    
      ivect; int value; while (cin >> value) { ivect.push_back(value); } decltype(ivect.size()) i = 0, j = ivect.size() - 1; for (; i < j; ++i, --j) { cout << ivect[i] + ivect[j] << endl; } if (i == j) { cout << ivect[i] << endl; } //用迭代器实现 vector
     
       ivect; int value; while (cin >> value) { ivect.push_back(value); } auto ibegin = ivect.begin(), iend = ivect.end() - 1; for (; ibegin < iend; ++ ibegin, -- iend) { cout << *ibegin + *iend << " "; } if (ibegin == iend) { cout << *ibegin << endl; } 
     
    
   
  

2 Allof the library containers have iterators, but only a few of them support thesubscript operator. C++ programmers use != as a matter of habit. They do so forthe same reason that they use iterators rather than subscripts: This codingstyle applies equally well to various kinds of containers provided by thelibrary. As we’ve seen, only a few library types, vector and string being amongthem, have the subscript operator. Similarly, all of the library containershave iterators that define the == and != operators. Most of those iterators donot have the < operator. By routinely using iterators and !=, we don’t haveto worry about the precise type of container we’re processing.

3 Thetype returned by begin and end depends on whether the object on which theyoperator is const. If the object is const, then begin and end return aconst_iterator; if the object is not const, they return iterator:

vector
  
    v; 
const vector
   
     cv; auto it1 = v.begin(); // it1 has type vector
    
     ::iterator auto it2 = cv.begin(); // it2 has type vector
     
      ::const_iterator auto it3 = v.cbegin(); // it3 has type vector
      
       ::const_iterator 
      
     
    
   
  

As do the begin and end members, thesemembers return iterators to the first and one past the last element in thecontainer. However, regardless of whether the vector (or string) is const, theyreturn a const_iterator.

4 Asa result, the dimension must be known at compile time, which means that thedimension must be a constant expression:

unsigned cnt = 42;          // not a constant expression 
constexpr unsigned sz = 42; // constant expression                           
int arr[10];             // array of ten ints
int *parr[sz];           // array of 42 pointers to int 
string bad[cnt];         // error: cnt is not a constant expression 
string strs[get_size()]; // ok if get_size is constexpr, error otherwise

When we define an array, we must specify atype for the array. We cannot use auto to deduce the type from a list ofinitializers. As with vector, arrays hold objects. Thus, there are no arrays ofreferences.

5 Whenwe use a variable to subscript an array, we normally should define thatvariable to have type size_t. size_t is a machine-specific unsigned type thatis guaranteed to be large enough to hold the size of any object in memory. Thesize_t type is defined in the cstddef header, which is the C++ version of thestddef.h header from the C library.
6
string nums[] = {"one", "two", "three"};  // array of strings 
string *p = &nums[0];   // p points to the first element in nums
string *p2 = nums;      // equivalent to p2 = &nums[0]

int ia[] = {0,1,2,3,4,5,6,7,8,9}; // ia is an array of ten ints 
auto ia2(ia); // ia2 is an int* that points to the first element in ia 
ia2 = 42;     // error: ia2 is a pointer, and we can't assign an int to a pointer
auto ia2(&ia[0]);  // now it's clear that ia2 has type int*

// ia3 is an array of ten ints 
decltype(ia) ia3 = {0,1,2,3,4,5,6,7,8,9};
ia3 = p;    // error: can't assign an int* to an array 
ia3[4] = i; /