-
词频统计:
#include <iostream> #include <string> #include <map> #include <sstream> #include <vector> #include <algorithm> #include <cctype> int main() { std::string text; std::cout << "Please input the English text: "; std::getline(std::cin,text); // 将标点替换为空格 // 排除'和- std::replace_if(text.begin(), text.end(), [](char c) { return std::ispunct(c) && c != '-' && c != '\''; }, ' '); // 转换为小写并分词 std::transform(text.begin(), text.end(), text.begin(), ::tolower); std::stringstream ss(text); std::string word; std::map<std::string, int> wordCount; // 统计词频 while (ss >> word) { if (!word.empty()) { wordCount[word]++; } } // 按字母顺序输出 std::cout << "Result:" << std::endl; for (const auto& pair : wordCount) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; }这个版本通过
replace_if处理标点以方便分词。可以处理一些不规范的输入:Please input the English text: Hi hi.Hi...HI Bye I'm Good thaNKs.AnD yOU? Result: and: 1 bye: 1 good: 1 hi: 4 i'm: 1 thanks: 1 you: 1 -
成绩管理:
#include<iostream> #include<vector> #include<algorithm> #include<string> #include<tuple> using namespace std; struct Student{ string name; int score; string id; }; int main(){ vector<Student> students={ // 或者 make_tuple("Alice",85,"001"), {"Alice",85,"001"}, {"Bob",92,"002"}, {"Charlie",78,"003"}, {"David",95,"004"}, }; sort(students.begin(),students.end(), [](const Student& a,const Student& b){ return a.score > b.score; }); cout<<"Sort results by grade: "<<endl; for(const auto& student : students){ cout << student.name <<"-Grade: "<<student.score<<" ID:"<<student.id<<endl; } string search_name; cout<<"\nPlease enter the name of the student you want to search for:"; cin>>search_name; auto it = find_if(students.begin(),students.end(), [&](const Student& s){ return s.name == search_name; }); if(it != students.end()){ cout<<"Find student: "<<it->name <<",grade: "<<it->score <<",ID: "<<it->id<<endl; }else{ cout<<"Not found: "<<search_name<<endl; } return 0; } -
铠甲合体#include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <iterator> int main() { std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 6, 7, 1, 0}; std::cout << "Origin: "; for (int num : numbers) std::cout << num << " "; std::cout << std::endl; // 过滤偶数 auto it = std::remove_if(numbers.begin(), numbers.end(), [](int x) { return x % 2 == 0; }); numbers.erase(it, numbers.end()); std::cout << "Even filtered: "; for (int num : numbers) std::cout << num << " "; std::cout << std::endl; // 每个元素平方 std::transform(numbers.begin(), numbers.end(), numbers.begin(), [](int x) { return x * x; }); std::cout << "Squared: "; for (int num : numbers) std::cout << num << " "; std::cout << std::endl; // 去重 std::sort(numbers.begin(), numbers.end()); auto last = std::unique(numbers.begin(), numbers.end()); numbers.erase(last, numbers.end()); std::cout << "Uniqued: "; for (int num : numbers) std::cout << num << " "; std::cout << std::endl; // 求和 int sum = std::accumulate(numbers.begin(), numbers.end(), 0); std::cout << "Sum: " << sum << std::endl; return 0; } -
智障指针
#include <iostream> #include <memory> class Resource { public: Resource(int id) : id(id) { std::cout << "Creating Resource " << id << std::endl; } ~Resource() { std::cout << "Destroying Resource " << id << std::endl; } void use() { std::cout << "Using Resource " << id << std::endl; } private: int id; }; class NodeA; class NodeB; class NodeA { public: // 错误1:未弱化的循环引用 // std::shared_ptr<NodeB> b_ptr;、 // 修改: std::weak_ptr<NodeB> b_ptr; ~NodeA() { std::cout << "NodeA destroyed" << std::endl; } }; class NodeB { public: // 错误1:未弱化的循环引用 // 不过改 NodeA 一处就好了 // 当然也可以像由崎夫妇一样两边都弱化 std::shared_ptr<NodeA> a_ptr; ~NodeB() { std::cout << "NodeB destroyed" << std::endl; } }; void test_smart_pointers() { // 错误2:多个unique_ptr管理同一个对象 int* raw_ptr = new int(42); std::unique_ptr<int> up1(raw_ptr); // 修改: // 要么不要这个多重管理,要么采用 shared_ptr, // 要么创建俩对象 // std::unique_ptr<int> up2(raw_ptr); // 错误3:使用已释放的unique_ptr std::unique_ptr<Resource> r1(new Resource(1)); std::unique_ptr<Resource> r2 = std::move(r1); // r1->use(); // 修改: if (r1 != nullptr){ r1->use(); } // 循环引用实例 // 已经在上面类定义中弱化了引用。 auto nodeA = std::make_shared<NodeA>(); auto nodeB = std::make_shared<NodeB>(); nodeA->b_ptr = nodeB; nodeB->a_ptr = nodeA; // 错误4:错误的数组删除 // std::unique_ptr<int> arr_ptr(new int[10]); // 修改: std::unique_ptr<int[]> arr_ptr(new int[10]); // 或者自己写(不推荐)删除函数对象 // std::unique_ptr<int, void(*)(int*)> arr_ptr(new int[10], [](int* p) { delete[] p; }); // 错误5:get()误用 std::shared_ptr<Resource> shared_res = std::make_shared<Resource>(2); Resource* raw_res = shared_res.get(); // delete raw_res; // 危险! // 修改:直接删掉这个,不需要手动删除 // 如果非要解除引用(这里是 shared_ptr,可能不会释放),使用shared_res.reset() // 对于unique_ptr,reset 和 release都可以释放 // 错误6:自管理资源冲突 // std::shared_ptr<FILE> file_ptr(fopen("test.txt", "w")); // fclose(file_ptr.get()); // 危险! // 修改:使用 fclose 作为释放函数,外边什么都不做 // 还是那句话,**绝对不要** 手动删除 get() 返回的指针! std::shared_ptr<FILE> file_ptr(fopen("test.txt", "w"), fclose); } int main() { test_smart_pointers(); return 0; }
丙加·第10章·答案
·647 字·4 分钟·