3.2部分比较简单 or 重复的题跳过
#include3.5#include using std::cin; using std::cout; using std::endl; using std::string; int main () { string s,line; getline(cin,line); //读一行 getline 直到 换行终止 cout << line << endl; cin >> s ; cout << s << endl;//读一个单词 遇到空格截止 return 0; }
#include3.6#include using std::cin; using std::cout; using std::endl; using std::string; int main () { string s1,s2; while(cin >> s1) { s2 += s1; s2 += " "; //按CTRl + Z 退出 } cout << s2 << endl; return 0; }
#include3.9 3.10#include using std::cin; using std::cout; using std::endl; using std::string; int main () { string s; getline(cin, s); for(auto &it : s) { // for(char &it : s) { 咱俩句话等价 3.7 it = 'x'; } cout << s << endl; return 0; }
使用ispunct(s) 判断是否为标点
3.11
类型是常量引用 无法被修改 会保存
#include3.18#include #include using std::cin; using std::cout; using std::endl; using std::string; using std::vector; int main () { string s; vector vs; while(cin >> s) { string tmp; for(auto &it : s) { tmp += toupper(it); } vs.push_back(tmp); } for(auto it : vs) { cout << it << endl; } cout << endl; return 0; }
#include3.34#include #include using std::cin; using std::cout; using std::endl; using std::string; using std::vector; string sa[10]; int ia[10]; int main () { string sa2[10]; int ia2[10]; for(auto it : sa) { cout << it << " "; }cout << endl; for(auto it : sa2) { cout << it << " "; } cout << endl; for(auto it : ia) { cout << it << " "; } cout << endl; for(auto it : ia2) { cout << it << " "; }cout << endl; return 0; }
指向p2 如果类型不同则会报错
C++11 可以数组可以使用 begin 和 end函数
#include3.37#include #include using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; int main () { const int sz= 10; int a[sz]; for(int i = 0; i < 10; i ++) { a[i] = i; } for(auto it : a) { cout << it << ' '; } cout << endl; int *p = begin(a); //需要 using while (p != end(a)) { *p = 0; p ++; } for(auto it : a) { cout << it << ' '; } return 0; }
由于没有/0 所以会死循环
加上 /0 or 改为字符串 “hello”
int a[10]
vector



