标签:分享 类对象 etl span out 制表符 16px div while
3.2
1 #include <iostream> 2 #include <string> 3 4 using std::cin; 5 using std::cout; 6 using std::endl; 7 using std::string; 8 9 int main() 10 { 11 string line; 12 while (getline(cin, line)) 13 cout << line << endl; 14 return 0; 15 }
1 #include <iostream> 2 #include <string> 3 4 using std::cin; 5 using std::cout; 6 using std::endl; 7 using std::string; 8 9 int main() 10 { 11 string word; 12 while (cin >> word) 13 cout << word << endl; 14 return 0; 15 }
3.3
输入运算符:自动忽略string类对象开头的空白(即空格符、换行符、制表符等),并从第一个真正的字符开始读起,直到遇见下一处空白为止。
getline函数:从给定的输入流中读入内容,直到遇到换行符为止(换行符也被读进来了),然后把所读的内容存入到那个 string 对象中去(不存换行符)。
3.4
1 #include <iostream> 2 #include <string> 3 4 using std::cin; 5 using std::cout; 6 using std::endl; 7 using std::string; 8 9 void is_equal(string &s1, string &s2) 10 { 11 if (s1 == s2) { 12 cout << s1 << " is equal to " << s2 << endl; 13 } 14 else { 15 if (s1 < s2) cout << s1 << " is smaller than " << s2 << endl; 16 else cout << s1 << " is bigger than " << s2 << endl; 17 } 18 } 19 20 void is_same_length(string &s1, string &s2) 21 { 22 if (s1.size() == s2.size()){ 23 cout << s1 << "‘s length is equal to " << s2 << endl ; 24 } 25 else { 26 if (s1.size() < s2.size()) cout << s1 << "‘s length is shorter than " << s2 << endl; 27 else cout << s1 << "‘s length is longer than " << s2 << endl; 28 } 29 } 30 31 int main() 32 { 33 string s1, s2; 34 cin >> s1 >> s2; 35 is_equal(s1, s2); 36 is_same_length(s1, s2); 37 return 0; 38 }
3.5
1 #include <iostream> 2 #include <string> 3 4 using std::cin; 5 using std::cout; 6 using std::endl; 7 using std::string; 8 9 void solve1() 10 { 11 string s1, s2 = ""; 12 while (getline(cin, s1)) { 13 s2 += s1; 14 cout << s2 << endl; 15 } 16 } 17 18 void solve2() 19 { 20 string s1, s2 = ""; 21 while (getline(cin, s1)) { 22 s2 = s2 + s1 + " "; 23 cout << s2 << endl; 24 } 25 26 } 27 28 int main() 29 { 30 solve1(); 31 solve2(); 32 return 0; 33 }
标签:分享 类对象 etl span out 制表符 16px div while
原文地址:http://www.cnblogs.com/xzxl/p/7624717.html