迭代器(iterator)
P96:使用迭代器将字符串中的每个元素变成大写:


1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <typeinfo> 5 using namespace std; 6 int main() 7 { 8 string s("some string"); 9 if (s.begin() != s.end()) { 10 for (auto it = s.begin(); it != s.end() /*&& !isspace(*it)*/; ++it) { 11 *it = toupper(*it); 12 } 13 } 14 cout << s << " "; 15 cout << endl; 16 system("pause"); 17 return 0; 18 }
迭代器类型:
#include <iostream> #include <vector> #include <string> #include <typeinfo> using namespace std; int main() { vector<int>::iterator it;//it能读写vector<int>的元素 string::iterator it2;//it2能读写string对象中的元素 vector<int>::const_iterator it3;//it3能读vector<int>的元素,不能写元素 string::iterator it4;//it4能读string对象中的元素,不能写元素 }
P98:依次输出text的每一行直至遇到第一个空白行为止


1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <typeinfo> 5 using namespace std; 6 int main() 7 { 8 vector<string> text{ "some string" }; 9 for (auto it = text.cbegin(); it != text.cend() && !it->empty(); ++it) { 10 cout << *it << endl; 11 } 12 system("pause"); 13 return 0; 14 }
3.22


1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <typeinfo> 5 using namespace std; 6 int main() 7 { 8 vector<string> text{ "some string" }; 9 for (string::size_type i = 0; i < text.size(); ++i) { 10 for (string::size_type j = 0; j < text[i].size(); ++j) { 11 text[i][j] = toupper(text[i][j]); 12 } 13 } 14 for (auto it = text.cbegin(); it != text.cend() && !it->empty(); ++it) { 15 cout << *it << endl; 16 } 17 system("pause"); 18 return 0; 19 }
由用户输入:


1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <typeinfo> 5 using namespace std; 6 int main() 7 { 8 vector<string> text; 9 string temp; 10 getline(cin, temp); 11 text.push_back(temp); 12 for (string::size_type i = 0; i < text.size(); ++i) { 13 for (string::size_type j = 0; j < text[i].size(); ++j) { 14 text[i][j] = toupper(text[i][j]); 15 } 16 } 17 for (auto it = text.cbegin(); it != text.cend() && !it->empty(); ++it) { 18 cout << *it << " "; 19 cout << endl; 20 } 21 system("pause"); 22 return 0; 23 }
3.23


1 #include <vector> 2 #include <string> 3 using namespace std; 4 int main() 5 { 6 vector<int> v(10,1); 7 for (string::size_type i = 0; i < v.size(); ++i) { 8 v[i] *= 2; 9 } 10 for (auto it = v.cbegin(); it != v.cend(); ++it) { 11 cout << *it << endl; 12 } 13 system("pause"); 14 return 0; 15 }
P100:使用迭代器计算:


1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 vector<int> text{ 1,2,3,4,5,6,7,8}; 8 auto beg = text.begin(), end = text.end(); 9 auto mid = text.begin() + (end - beg) / 2; 10 int sought = 3; 11 while (mid != end && *mid != sought) { 12 if (sought < *mid) { 13 end = mid; 14 } 15 else { 16 beg = mid + 1; 17 } 18 mid = beg + (end - beg) / 2; 19 } 20 cout << *mid << endl; 21 system("pause"); 22 return 0; 23 24 }
利用for循环和push_back添加100个数:


1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 vector<int> text; 8 for (string::size_type i = 0; i < 100; ++i) { 9 text.push_back(i); 10 } 11 /*for (string::size_type i = 0; i < text.size(); ++i) { 12 cout << text[i]; 13 }*/ 14 auto beg = text.begin(), end = text.end(); 15 auto mid = text.begin() + (end - beg) / 2; 16 int sought = 55; 17 while (mid != end && *mid != sought) { 18 if (sought < *mid) { 19 end = mid; 20 } 21 else { 22 beg = mid + 1; 23 } 24 mid = beg + (end - beg) / 2; 25 } 26 cout << *mid << " "; 27 cout << endl; 28 system("pause"); 29 return 0; 30 31 }
用户输入要查找的数:


1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 vector<int> text; 8 for (string::size_type i = 0; i < 1000; ++i) { 9 text.push_back(i); 10 } 11 auto beg = text.begin(), end = text.end(); 12 auto mid = text.begin() + (end - beg) / 2; 13 int sought; 14 cout << "输入要查找的数:" << endl; 15 cin >> sought; 16 while (mid != end && sought != *mid) { 17 if (sought < *mid) { 18 end = mid; 19 } 20 else { 21 beg = mid + 1; 22 } 23 mid = beg + (end - beg) / 2; 24 } 25 cout << *mid << " "; 26 cout << endl; 27 system("pause"); 28 return 0; 29 }
3.24所编写的程序不完美,限定vector对象的元素个数为偶数,否则报错:


1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 vector<int> text; 8 for (string::size_type i = 0; i < 1002; ++i) { 9 text.push_back(i); 10 } 11 /* 12 for (string::size_type i = 0; i < text.size(); ++i) { 13 cout << text[i] << endl; 14 } 15 */ 16 for (auto beg = text.begin(), end = text.end(); beg != end; ++beg) { 17 --end; 18 cout << *beg + *end << endl; 19 } 20 system("pause"); 21 return 0; 22 }
3.25


1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 vector<unsigned> scores(11, 0); 8 unsigned grade; 9 while (cin >> grade) { 10 ++scores[grade / 10]; 11 } 12 for (auto beg = scores.begin(), end = scores.end(); beg != end; ++beg) { 13 cout << *beg << endl; 14 } 15 system("pause"); 16 return 0; 17 }
3.26
因为end指的是最后一个元素的后一个位置!!!本来两个数之间的距离是y-x+1的,而这里end指的是最后一个元素的后一个位置,所以不需要+1了。
小结:类型修饰符从右往左依次绑定。