c++primer 3.2,3.3练习题
生活随笔
收集整理的這篇文章主要介紹了
c++primer 3.2,3.3练习题
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 3.2.2 string 對(duì)象上的操作
- 3.2.3 處理string對(duì)象中的字符
- 3.3.2 向vector對(duì)象中添加元素
- 3.3.3其他vector操作
練習(xí)題涉及到代碼的部分。
3.2.2 string 對(duì)象上的操作
3.2
//一次輸入一整行 #include<string> #include<iostream> using namespace std;int main(){string line;cout<<"cin : ";while(getline(cin,line)){cout<<"cout : ";cout << line << endl;}return 0; } /* cin : dskhfsdfj dflk dkf ewr cout : dskhfsdfj dflk dkf ewr *///一次輸入一個(gè)詞: #include<string> #include<iostream> using namespace std;int main(){string word;cout<<"cin : ";while(cin >> word){cout<<"cout : ";cout << word << endl;}return 0; } /* cin : a v a d ss dfdfk dsfls cout : a cout : v cout : a cout : d cout : ss cout : dfdfk cout : dsfls */3.3 string類的輸入運(yùn)算符和getline函數(shù)分別是如何處理空白字符串的?
- 輸入運(yùn)算符:
在使用cin執(zhí)行讀取操作時(shí),string對(duì)象會(huì)忽略開頭空白,從第一個(gè)字符開始,到下一個(gè)空白結(jié)束。
- getline函數(shù):
如果說在最終字符串中保留輸入時(shí)的空白,使用getline函數(shù)替代>>。
函數(shù)從給定的輸入流中讀取數(shù)據(jù),直到遇到換行符為止(換行符也被讀進(jìn)來了),然后把所讀的內(nèi)容存放在string對(duì)象里(這里面不存在換行符)。
如果輸入一開始就是個(gè)換行符,那么所得結(jié)果是一個(gè)空string。
3.4
//比較是否相等 #include<string> #include<iostream> using namespace std;int main(){string s1 , s2;int i = 0;while(getline(cin,s1) && getline(cin,s2)){if(s1 == s2)cout << "equal";else if(s1 > s2)cout<<s1;else cout<<s2;cout<<endl;cout<<endl;}return 0; } /* aaasa bbsd bbsdasdlsf asdlsf equalavklfs vs vsklk kl klk*/ //比較是否等長(zhǎng)#include<string> #include<iostream> using namespace std;int main(){string s1 , s2;int i = 0;while(getline(cin,s1) && getline(cin,s2)){if(s1.size() == s2.size())cout << "equal";else if(s1.size() > s2.size())cout<<s1;else cout<<s2;cout<<endl;cout<<endl;}return 0; } /* sajdlsafldlsf adsa sajdlsafldlsfsakdkfjldsf saddfkgorjgojoe saddfkgorjgojoesd sd equal*/3.5
#include<string> #include<iostream> using namespace std;int main(){string s1 , s2;while(cin >> s1){s2 =s2 + s1;}cout<<s2;return 0; } /* dfjdslf dfds sdf sdf dsf ^Z dfjdslfdfdssdfsdfdsf */#include<string> #include<iostream> using namespace std;int main(){string s1 , s2;int i = 0;while(cin >> s1){if(i==0){s2 = s2 + s1;i++;}elses2 = s2 + " " + s1;}cout<<s2;return 0; } /* dfj sad ads asd adsf bg dsf ^Z dfj sad ads asd adsf bg dsf */3.2.3 處理string對(duì)象中的字符
3.6、3.7
#include<string> #include<iostream> using namespace std;int main(){string s(10,'c');for(auto &a : s)a = 'X';cout<<s<<endl;return 0; } /* XXXXXXXXXX */ #include<string> #include<iostream> using namespace std;int main(){string s(10,'c');for(char &a : s)a = 'X';cout<<s<<endl;return 0; } /* XXXXXXXXXX */3.8
#include<string> #include<iostream> using namespace std;int main(){string s(10,'c');for(decltype(s.size()) i = 0; i != s.size(); ++i){s[i] = 'X';}cout<<s<<endl;return 0; } /* XXXXXXXXXX */#include<string> #include<iostream> using namespace std;int main(){string s(10,'c');decltype(s.size()) index = 0; while(index != s.size()){s[index] = 'X'; ++index;}cout<<s<<endl;return 0; } /* XXXXXXXXXX */3.10
#include<string> #include<iostream> using namespace std;int main(){string s;getline(cin,s);decltype(s.size()) index = 0; for(auto c : s)if(ispunct(c))continue;elsecout<<c;return 0; } /* i love you more_than i can say!! 521 ***()6&&&*^&%%&$%^#^%*(&789><::>::< i love you morethan i can say 521 ()6789 */3.3.2 向vector對(duì)象中添加元素
3.14
#include<vector> #include<iostream> using namespace std;int main(){vector<int> a;int b;while(cin>>b){a.push_back(b);}return 0; }3.15
#include<vector> #include<iostream> #include<string> using namespace std;int main(){vector<string> a;string b;while(cin>>b){a.push_back(b);}return 0; }3.3.3其他vector操作
3.16
#include<vector> #include<iostream> #include<string> using namespace std;int main(){vector<int> v1;vector<int> v2(10);vector<int> v3(10,42);vector<int> v4{10};vector<int> v5{10,42};vector<vector<int>> v{v1,v2,v3,v4,v5};vector<string> v6{10};vector<string> v7{10 , "hi"};vector<vector<string>> vv{v6,v7};int k = 1;cout<<"v"<<k<<" : ";for(auto i : v){for(auto j : i){cout<<j<<" ";}cout<<endl;cout<<"v"<<++k<<" : ";}for(auto i : vv){for(auto j : i){cout<<j<<" ";}if(k+1==8) break;cout<<endl;cout<<"v"<<++k<<" : ";}return 0; } /* v1 : v2 : 0 0 0 0 0 0 0 0 0 0 v3 : 42 42 42 42 42 42 42 42 42 42 v4 : 10 v5 : 10 42 v6 : v7 : hi hi hi hi hi hi hi hi hi hi */3.17
#include<string> #include<vector> #include<iostream> using namespace std;int main(){vector<string> v;string word;while(cin >> word){for(auto & c : word)c = toupper(c);v.push_back(word);}for(auto s : v)cout<<s<<endl; return 0; } /* hudhfds dsfhds sdf sdfi we wer c ^Z HUDHFDS DSFHDS SDF SDFI WE WER C */3.20
#include<string> #include<vector> #include<iostream> using namespace std;int main(){vector<int> v;int a;while(cin >> a){v.push_back(a);}decltype(v.size()) n = v.size();for(decltype(v.size()) i = 0; i <= n-1-i; ++i){if(i == n-1-i)cout<<v[i]<<endl;elsecout<<v[i]+v[n-1-i]<<endl;}return 0; } /* 1 2 3 4 5 ^Z 6 6 3 */ /* 1 2 3 4 ^Z 5 5 */總結(jié)
以上是生活随笔為你收集整理的c++primer 3.2,3.3练习题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言基础练习题 字符串输入scanf/
- 下一篇: 基于SVD的降维优化