1 #include <iostream> 2 3 using std::cout; 4 using std::endl; 5 6 class A{ 7 public: 8 A(int x0, int y0): x{ x0 }, y{ y0 } {} 9 void show() const { cout << x << ", " << y << endl; } 10 private: 11 int x, y; 12 }; 13 14 class B{ 15 public: 16 B(double x0, double y0): x{ x0 }, y{ y0 } {} 17 void show() const { cout << x << ", " << y << endl;} 18 private: 19 double x, y; 20 }; 21 22 void test() { 23 cout << "测试类A: " << endl; 24 A a(3, 4); 25 a.show(); 26 27 cout << "\n测试类B: " << endl; 28 B b(3.2, 5.6); 29 b.show(); 30 } 31 32 int main() { 33 test(); 34 }task1_1.cpp
1 #include <iostream> 2 #include <string> 3 4 using std::cout; 5 using std::endl; 6 7 template<typename T> 8 class X{ 9 public: 10 X(T x0, T y0): x{x0}, y{y0} {} 11 void show() const { cout << x << ", " << y << endl;} 12 private: 13 T x, y; 14 }; 15 16 void test() { 17 cout << "测试1: 模板类抽象类型T用int实例化" << endl; 18 X<int> x1(3, 4); 19 x1.show(); 20 21 cout << "\n测试2: 模板类抽象类型T用double实例化" << endl; 22 X<double> x2(3.2, 5.6); 23 x2.show(); 24 25 cout << "\n测试3: 模板类抽象类型T用标准库中的string实例化" << endl; 26 X<std::string> x3("hello", "c plus plus"); 27 x3.show(); 28 } 29 30 int main() { 31 test(); 32 }task1_2.cpp
1 #include <iostream> 2 #include <string> 3 #include <limits> 4 5 using namespace std; 6 7 int main() { 8 9 const int n = 10; 10 string prompt = string(n, '*') + "Enter a string: " + string(n, '*') + '\n'; 11 12 cout << "测试1:"; 13 cout << prompt; 14 string s1; 15 cin >> s1; 16 cout << "s1: " << s1 << endl; 17 cin.ignore(numeric_limits<streamsize>::max(), '\n'); 18 19 cout << "\n测试2:"; 20 cout << prompt; 21 getline(cin, s1); 22 cout << "s1: " << s1 << endl; 23 24 cout << "\n测试3:"; 25 string s2, s3; 26 cout << prompt; 27 getline(cin, s2, ' '); 28 getline(cin, s3); 29 cout << "s2: " << s2 << endl; 30 cout << "s3: " << s3 << endl; 31 }task2_1.cpp
1 #include <iostream> 2 #include <string> 3 4 int main() { 5 using namespace std; 6 7 string s; 8 9 cout << "Enter words: \n"; 10 11 while( getline(cin, s) ) { 12 for(auto &ch: s) 13 ch = toupper(ch); 14 cout << s << "\n"; 15 } 16 }task2_2.cpp
#include <iostream> #include <string> #include <algorithm> int main() { using namespace std; string s1; cout << "Enter words: \n"; getline(cin, s1); cout << "original words: \n"; cout << s1 <<endl; cout << "to uppercase: \n"; transform(s1.begin(), s1.end(), s1.begin(), ::toupper); cout << s1 << endl; }task2_3.cpp
#include <iostream> #include <string> int main() { using namespace std; string s1, s2; s1 = "nuist"; s1[0] = 'N'; s1.at(1) = 'U'; cout << boolalpha << (s1 == "nuist") << endl; cout << s1.length() << endl; cout << s1.size() << endl; s2 = s1 + ", 2050"; cout << s2 << endl; string email{"[email protected]"}; auto pos = email.find("@"); if (pos == string::npos) cout << "illegal email address"; else { auto s1 = email.substr(0, pos); auto s2 = email.substr(pos + 1); cout << s1 << endl; cout << s2 << endl; } string phone{"15216982937"}; cout << phone.replace(3, 5, string(5, '*')) << endl; string s3{"cosmos"}, s4{"galaxy"}; cout << "s3: " + s3 + " s4: " + s4 << endl; s3.swap(s4); cout << "s3: " + s3 + " s4: " + s4 << endl; string s5{"abc"}; const char *pstr = s5.c_str(); cout << pstr << endl; string s6{"12306"}; int x1 = stoi(s6); cout << x1 << endl; int x2 = 12306; string s7 = to_string(x2); cout << s7 << endl; double x3 = 123.06; string s8 = to_string(x3); cout << s8 << endl; }task2_4.cpp
#include <iostream> #include <vector> template<typename T> void output(const T &obj) { for(auto &item: obj) std::cout << item << ", "; std::cout << "\b\b \n"; } int main() { using namespace std; vector<int> v1; vector<int> v2(5); vector<int> v3(5, 42); vector<int> v4{1, 9, 8, 4}; vector<int> v5{v4}; cout << "v2: "; output(v2); cout << "v3: "; output(v3); cout << "v4: "; output(v4); cout << "v5: "; output(v5); }task3_1.cpp
#include <iostream> #include <vector> using std::cout; using std::endl; using std::vector; void output(const vector<int> &v) { cout << "v.size() = " << v.size() << endl; cout << "v.capacity() = " << v.capacity() << endl; cout << endl; } int main() { vector<int> v{42}; output(v); v.push_back(55); v.push_back(90); output(v); for(auto i = 0; i < 8; ++i) v.push_back(i); output(v); v.pop_back(); output(v); }task3_2.cpp
1 #include <iostream> 2 #include <vector> 3 #include <array> 4 #include <string> 5 #include <algorithm> 6 7 using namespace std; 8 9 template<typename T> 10 void output1(const T &obj) { 11 for(auto i = 0; i < obj.size(); ++i) 12 cout << obj.at(i) << ", "; 13 cout << "\b\b \n"; 14 } 15 16 template<typename T> 17 void output2(const T &obj) { 18 for(auto p = obj.cbegin(); p != obj.cend(); ++p) 19 cout << *p << ", "; 20 cout << "\b\b \n"; 21 } 22 23 template<typename T> 24 void output3(const T &obj) { 25 for(auto &item: obj) 26 cout << item << ", "; 27 cout << "\b\b \n"; 28 } 29 30 void test1() { 31 string s1{"cplus"}; 32 33 output1(s1); 34 35 reverse(s1.begin(), s1.end()); 36 output2(s1); 37 38 sort(s1.begin(), s1.end()); 39 output3(s1); 40 } 41 42 void test2() { 43 array< array<int, 4>, 3> x{1, 9, 8, 4, 2, 0, 2, 3, 2, 0, 4, 9 }; 44 45 output1( x.at(0) ); 46 output2( x.at(1) ); 47 output3( x.at(2) ); 48 } 49 50 void test3() { 51 vector<string> v1 {"Sheldon", "Leonard", "Howard", "Raj"}; 52 53 v1.push_back("Penny"); 54 v1.push_back("Amy"); 55 56 output1(v1); 57 58 sort(v1.begin(), v1.end(), std::greater<string>()); 59 output2(v1); 60 61 reverse(v1.begin(), v1.end()); 62 output3(v1); 63 } 64 65 int main() { 66 cout << "测试1: " << endl; 67 test1(); 68 69 cout << "测试2: " << endl; 70 test2(); 71 72 cout << "测试3: " << endl; 73 test3(); 74 }task4.cpp
#pragma once #include<iostream> #include<string> #include <iomanip> using namespace std; class Info{ public: Info(string nickname0, string contact0, string city0, int n0); ~Info()=default; void print() const; private: string nickname,contact,city; int n; }; Info::Info(string nickname0, string contact0, string city0, int n0) { nickname = nickname0; contact = contact0; city = city0; n=n0; } void Info::print() const { cout << "昵称: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout << "预定参加人数: " << n << endl; }Info.hpp
1 #include "Info.hpp" 2 #include<iostream> 3 #include<string> 4 #include <vector> 5 6 using namespace std; 7 8 int main() 9 { 10 const int capacity=100; 11 vector<Info> audience_info_list; 12 13 cout << "录入信息:" << endl; 14 cout << endl; 15 cout << "昵称 联系方式(邮箱/手机号) 所在城市 预定参加人数 " << endl; 16 char choice; 17 int sum=0; 18 string name, contact, city; 19 int n; 20 while(cin >> name >> contact >> city >> n) 21 { 22 sum+=n; 23 24 if(sum > capacity) 25 { 26 sum-=n; 27 cout << "对不起,只剩" << capacity-sum << "个位置" << endl; 28 cout << "1.输入u,更新(update)预定信息" << endl 29 << "2.输入q, 退出预定" << endl 30 <<"你的选择: "; 31 cin >> choice; 32 33 if(choice=='u') 34 { 35 cin >> name >> contact >> city >> n; 36 Info a(name,contact,city,n); 37 audience_info_list.push_back(a); 38 sum+=n; 39 } 40 else 41 break; 42 } 43 else audience_info_list.push_back(Info(name, contact, city, n)); 44 } 45 cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl; 46 for(auto &Info:audience_info_list) 47 { 48 Info.print(); 49 } 50 }task6.cpp
标签:std,cout,int,C++,实验,using,include,模板,string From: https://www.cnblogs.com/zs202183290495/p/17868943.html