


C++ written test questions to implement simple error recording function
本篇文章,小编将和大家分享一个有意思的C++笔试题,要求实现简单错误记录的功能,感兴趣的朋友可以了解一下,希望能对你的C++开发学习有所帮助。
题目介绍:
开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。 要求:
1.记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加;(文件所在的目录不同,文件名和行号相同也要合并);
2.超过16个字符的文件名称,只记录文件的最后有效16个字符;(如果文件名不同,而只是文件名的后16个字符和行号相同,也不要合并);
3.输入的文件可能带路径,记录文件名称(只记录文件名)不能带路径 。
例:
输入:
E:\\linuas\\desktop\\demo.txt 1234
E:\\linuas\\desktop\\demo.doc 1234
E:\\linuas\\desktop\\demo.txt 1234
输出:
demo.txt 1234 2
demo.doc 1234 1
分析:
解决方案里用到了pair(包含两种格式的数据结构)、make_pair(构造pair)、vector(栈)、find(找到字符串中指定字符位置)、erase(删除部分字符串)、substr(截取字符串)等用法,值得记录一下,免得后期学习忘记。解决方案高效而使用,不可多得。
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; bool compare(pair<string, int> a, pair<string, int> b) { return a.second > b.second; } int main(void) { string input, file; vector<pair<string, int>> errors; while (getline(cin, input)) { if (input.size() == 0) break; unsigned int f = input.rfind('\\'); file = input.substr(f + 1); errors.push_back(make_pair(file, 1)); for (int i = 0; i<(errors.size() - 1); i++) { if (errors[i].first == file) { errors[i].second++; errors.pop_back(); break; } } } stable_sort(errors.begin(), errors.end(), compare); int idx = 0; while (idx<8 && idx<errors.size()) { string check = errors[idx].first; int t = check.find(' '); if (t>16) errors[idx].first.erase(0, t - 16); cout << errors[idx].first << ' ' << errors[idx].second << endl; idx++; } }
结果:
相关教程:C++视频教程
The above is the detailed content of C++ written test questions to implement simple error recording function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Writing C in VS Code is not only feasible, but also efficient and elegant. The key is to install the excellent C/C extension, which provides functions such as code completion, syntax highlighting, and debugging. VS Code's debugging capabilities help you quickly locate bugs, while printf output is an old-fashioned but effective debugging method. In addition, when dynamic memory allocation, the return value should be checked and memory freed to prevent memory leaks, and debugging these issues is convenient in VS Code. Although VS Code cannot directly help with performance optimization, it provides a good development environment for easy analysis of code performance. Good programming habits, readability and maintainability are also crucial. Anyway, VS Code is

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version
