BWS2000倾角传感器c++测试代码

2023-12-20 13:36

本文主要是介绍BWS2000倾角传感器c++测试代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用瑞芬的倾角传感器配置的时候,数据手册一下就配置好了,但是BWS2000倾角传感器总是出错,这里进行一下记录出现的问题与解决方式。

1.初步测试

在配置BWS2000倾角传感器读取帧数据的时候,总是出现一个问题,就是进行输出的时候,得到的数据与实际设置的频率并不是不一致的。
其中,command_R1是对于输出频率的设置,command_R2是对于波特率的设置,command_R3保存设置命令。

问题一:注意当COM口大于10的时候,需要在前面添加\\\\.\\,否则会出现连接不上的情况,具体原因见博客:https://www.cnblogs.com/pandamohist/p/13681042.html

问题二:读出来的数据是16进制的,因此需要进行自己转换,转换过程代码如下所示(这个代码不是最优方式,比较麻烦,懒得再思考了):

if (sizeof(response_R) >= 13 && response_R[0] == 0x77 && response_R[1] == 0x10)//&& response[3] == 0x84)
{//x轴int hexValues[] = { response_R[5] & 0xFF, response_R[6] & 0xFF, response_R[7] & 0xFF };int numValues = sizeof(hexValues) / sizeof(hexValues[0]);std::stringstream ss;ss << std::setfill('0') << std::setw(2) << std::hex << hexValues[0];//std::cout << "String representation: " << numValues << std::endl;for (int i = 1; i < numValues; i++) {ss << "." << std::setw(2) << std::hex << hexValues[i];}std::string resultString = ss.str();std::cout << "X轴 String representation: " << resultString << std::endl;std::string inputString = resultString;std::string outputString;size_t firstDotIndex = inputString.find('.');size_t secondDotIndex = inputString.find('.', firstDotIndex + 1);if (firstDotIndex != std::string::npos && secondDotIndex != std::string::npos) {outputString = inputString.substr(0, secondDotIndex) + inputString.substr(secondDotIndex + 1);}else {outputString = inputString;}double resultDouble = std::stod(outputString);if (response_R[4] == 0x00){Sensor_Angle_RX0 = resultDouble;}else{Sensor_Angle_RX0 = -resultDouble;}std::cout << "x轴角度" << std::fixed << std::setprecision(4) << resultDouble << std::endl;//Y轴int hexValues1[] = { response_R[9] & 0xFF, response_R[10] & 0xFF, response_R[11] & 0xFF };int numValues1 = sizeof(hexValues1) / sizeof(hexValues1[0]);// Convert hexadecimal values to a string "01.0102"std::stringstream ss1;ss1 << std::setfill('0') << std::setw(2) << std::hex << hexValues1[0];std::cout << "String representation: " << numValues << std::endl;for (int i = 1; i < numValues1; i++) {ss1 << "." << std::setw(2) << std::hex << hexValues1[i];}std::string resultString1 = ss1.str();//std::cout << "Y轴 String representation: " << resultString1 << std::endl;std::string inputString1 = resultString1;std::string outputString1;size_t firstDotIndex1 = inputString1.find('.');size_t secondDotIndex1 = inputString1.find('.', firstDotIndex1 + 1);if (firstDotIndex1 != std::string::npos && secondDotIndex1 != std::string::npos) {outputString1 = inputString1.substr(0, secondDotIndex1) + inputString1.substr(secondDotIndex1 + 1);}else {outputString1 = inputString1;}double resultDouble1 = std::stod(outputString1);if (response_R[8] == 0x00){Sensor_Angle_RY0 = resultDouble1;}else{Sensor_Angle_RY0 = -resultDouble1;}std::cout << "y轴: " << std::fixed << std::setprecision(4) << resultDouble1 << std::endl;}

我写的测试代码如下所示:

#include <Windows.h>
#include <sstream> 
#include <iomanip>
#include <iostream>
#include <chrono>
#include <thread>
#include <Windows.h>int main() {HANDLE hSerial;DCB dcbSerialParams = { 0 };COMMTIMEOUTS timeouts = { 0 };DWORD bytesRead, bytesWritten, bytesWritten1, bytesWritten2, bytesWritten3, response_R1;char command_R[] = { 0x77, 0x04, 0x00, 0x04, 0x08 };//读X、Y轴角度 发送命令: 77 04 00 04 08char response_R[18] = { 18 };double Sensor_Angle_RX0;double Sensor_Angle_RY0;// 设置角度输出频率模式  77 05 00 0C 06(100Hz 05 50Hz 04 25Hz) 11 100Hz char command_R1[] = { 0x77, 0x05, 0x00, 0x0C, 0x04, 0x11 };//25Hz// 发送命令: 77 05 00 0B 04(115200 03 19200 02 9600) 12 115200  2 char command_R2[] = { 0x77, 0x05, 0x00, 0x0B, 0x02, 0x12 };// 保存设置命令: 77 04 00 0A 0E char command_R3[] = { 0x77, 0x04, 0x00, 0x0A, 0x0E };char dataReceived[100];wchar_t portName[] = L"\\\\.\\COM11"; // Note the 'L' before the stringhSerial = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);if (hSerial == INVALID_HANDLE_VALUE) {if (GetLastError() == ERROR_FILE_NOT_FOUND) {std::cout << "Serial port not available." << std::endl;}return 1;}dcbSerialParams.DCBlength = sizeof(dcbSerialParams);if (!GetCommState(hSerial, &dcbSerialParams)) {std::cout << "Error getting serial port state." << std::endl;CloseHandle(hSerial);return 1;}dcbSerialParams.BaudRate = CBR_9600;dcbSerialParams.ByteSize = 8;dcbSerialParams.StopBits = ONESTOPBIT;dcbSerialParams.Parity = NOPARITY;if (!SetCommState(hSerial, &dcbSerialParams)) {std::cout << "Error setting serial port state." << std::endl;CloseHandle(hSerial);return 1;}timeouts.ReadIntervalTimeout = 50;timeouts.ReadTotalTimeoutConstant = 50;timeouts.ReadTotalTimeoutMultiplier = 10;timeouts.WriteTotalTimeoutConstant = 50;timeouts.WriteTotalTimeoutMultiplier = 10;if (!SetCommTimeouts(hSerial, &timeouts)) {std::cout << "Error setting timeouts." << std::endl;CloseHandle(hSerial);return 1;}if (!WriteFile(hSerial, command_R1, sizeof(command_R1), &bytesWritten1, NULL)) {std::cout << "Error writing to serial port." << std::endl;CloseHandle(hSerial);return 1;}//std::cout << "Data sent successfully." << std::endl;/*if (!ReadFile(hSerial, dataReceived, sizeof(dataReceived), &bytesRead, NULL)) {std::cout << "Error reading from serial port." << std::endl;CloseHandle(hSerial);return 1;}if (!WriteFile(hSerial, command_R2, sizeof(command_R2), &bytesWritten2, NULL)) {std::cout << "Error writing to serial port." << std::endl;CloseHandle(hSerial);return 1;}if (!ReadFile(hSerial, dataReceived, sizeof(dataReceived), &bytesRead, NULL)) {std::cout << "Error reading from serial port." << std::endl;CloseHandle(hSerial);return 1;}if (!WriteFile(hSerial, command_R3, sizeof(command_R3), &bytesWritten3, NULL)) {std::cout << "Error writing to serial port." << std::endl;CloseHandle(hSerial);return 1;}int i = 0;while (1){for (int i = 0; i < sizeof(response_R); i++) {printf("%02X ", response_R[i]);}std::cout << std::endl;std::cout << i++ << response_R<< std::endl;/*//右倾角传感器if (sizeof(response_R) >= 13 && response_R[0] == 0x77 && response_R[1] == 0x10)//&& response[3] == 0x84){*///x轴// Given hexadecimal valuesint hexValues[] = { response_R[5] & 0xFF, response_R[6] & 0xFF, response_R[7] & 0xFF };int numValues = sizeof(hexValues) / sizeof(hexValues[0]);// Convert hexadecimal values to a string "01.0102"std::stringstream ss;ss << std::setfill('0') << std::setw(2) << std::hex << hexValues[0];//std::cout << "String representation: " << numValues << std::endl;for (int i = 1; i < numValues; i++) {ss << "." << std::setw(2) << std::hex << hexValues[i];}std::string resultString = ss.str();//std::cout << "X轴 String representation: " << resultString << std::endl;std::string inputString = resultString;std::string outputString;size_t firstDotIndex = inputString.find('.');size_t secondDotIndex = inputString.find('.', firstDotIndex + 1);if (firstDotIndex != std::string::npos && secondDotIndex != std::string::npos) {outputString = inputString.substr(0, secondDotIndex) + inputString.substr(secondDotIndex + 1);}else {outputString = inputString;}// Convert the string "01.0102" to double 01.0102double resultDouble = std::stod(outputString);if (response_R[4] == 0x00){Sensor_Angle_RX0 = resultDouble;}else{Sensor_Angle_RX0 = -resultDouble;}std::cout << "x轴角度" << std::fixed << std::setprecision(4) << resultDouble << std::endl;//Y轴int hexValues1[] = { response_R[9] & 0xFF, response_R[10] & 0xFF, response_R[11] & 0xFF };int numValues1 = sizeof(hexValues1) / sizeof(hexValues1[0]);std::stringstream ss1;ss1 << std::setfill('0') << std::setw(2) << std::hex << hexValues1[0];//std::cout << "String representation: " << numValues << std::endl;for (int i = 1; i < numValues1; i++) {ss1 << "." << std::setw(2) << std::hex << hexValues1[i];}std::string resultString1 = ss1.str();std::string inputString1 = resultString1;std::string outputString1;size_t firstDotIndex1 = inputString1.find('.');size_t secondDotIndex1 = inputString1.find('.', firstDotIndex1 + 1);if (firstDotIndex1 != std::string::npos && secondDotIndex1 != std::string::npos) {outputString1 = inputString1.substr(0, secondDotIndex1) + inputString1.substr(secondDotIndex1 + 1);}else {outputString1 = inputString1;}double resultDouble1 = std::stod(outputString1);if (response_R[8] == 0x00){Sensor_Angle_RY0 = resultDouble1;}else{Sensor_Angle_RY0 = -resultDouble1;}std::cout << "y轴: " << std::fixed << std::setprecision(4) << resultDouble1 << std::endl;}getchar();CloseHandle(hSerial);return 0;
}

在上面的代码之中,我是使用的自动输出的方式,配置的25Hz进行输出,但是出现的问题就是当把下面的代码进行打开之后:

if (sizeof(response_R) >= 13 && response_R[0] == 0x77 && response_R[1] == 0x10)//&& response[3] == 0x84){//x轴int hexValues[] = { response_R[5] & 0xFF, response_R[6] & 0xFF, response_R[7] & 0xFF };int numValues = sizeof(hexValues) / sizeof(hexValues[0]);// Convert hexadecimal values to a string "01.0102"std::stringstream ss;ss << std::setfill('0') << std::setw(2) << std::hex << hexValues[0];//std::cout << "String representation: " << numValues << std::endl;for (int i = 1; i < numValues; i++) {ss << "." << std::setw(2) << std::hex << hexValues[i];}std::string resultString = ss.str();//std::cout << "X轴 String representation: " << resultString << std::endl;std::string inputString = resultString;std::string outputString;size_t firstDotIndex = inputString.find('.');size_t secondDotIndex = inputString.find('.', firstDotIndex + 1);if (firstDotIndex != std::string::npos && secondDotIndex != std::string::npos) {outputString = inputString.substr(0, secondDotIndex) + inputString.substr(secondDotIndex + 1);}else {outputString = inputString;}double resultDouble = std::stod(outputString);if (response_R[4] == 0x00){Sensor_Angle_RX0 = resultDouble;}else{Sensor_Angle_RX0 = -resultDouble;}std::cout << "x轴角度" << std::fixed << std::setprecision(4) << resultDouble << std::endl;//Y轴int hexValues1[] = { response_R[9] & 0xFF, response_R[10] & 0xFF, response_R[11] & 0xFF };int numValues1 = sizeof(hexValues1) / sizeof(hexValues1[0]);std::stringstream ss1;ss1 << std::setfill('0') << std::setw(2) << std::hex << hexValues1[0];//std::cout << "String representation: " << numValues << std::endl;for (int i = 1; i < numValues1; i++) {ss1 << "." << std::setw(2) << std::hex << hexValues1[i];}std::string resultString1 = ss1.str();std::string inputString1 = resultString1;std::string outputString1;size_t firstDotIndex1 = inputString1.find('.');size_t secondDotIndex1 = inputString1.find('.', firstDotIndex1 + 1);if (firstDotIndex1 != std::string::npos && secondDotIndex1 != std::string::npos) {outputString1 = inputString1.substr(0, secondDotIndex1) + inputString1.substr(secondDotIndex1 + 1);}else {outputString1 = inputString1;}// Convert the string "01.0102" to double 01.0102double resultDouble1 = std::stod(outputString1);if (response_R[8] == 0x00){Sensor_Angle_RY0 = resultDouble1;}else{Sensor_Angle_RY0 = -resultDouble1;}std::cout << "y轴: " << std::fixed << std::setprecision(4) << resultDouble1 << std::endl;
}

发现明显输出的频率是和25Hz是不一致的。

然后直接将相应的报文进行打印,调用

for (int i = 0; i < sizeof(response_R); i++) {printf("%02X ", response_R[i]);}

输出如下结果:

真正想用的的数据是0x77 0x10 0x00数据之后的,也就是尽量是要求每一帧的数据都是这个情况,由于设置了自动输出的频率为25Hz,所以你读取相应的response是不知道在哪一个时刻开始的,需要进行相应的频率进行设置。

2.应答测试

在每个帧信号之后,输出一个应答信号,检测是否输出正确?

#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <iostream>
#include <chrono>
#include <thread>
#include <Windows.h>
#include <iostream>#include <iomanip>
double convertStringToDouble(const std::string& str) {double result;std::istringstream stream(str);stream >> result;return result;
}
int main() {HANDLE hSerial;DCB dcbSerialParams = { 0 };COMMTIMEOUTS timeouts = { 0 };DWORD bytesRead, bytesWritten, bytesWritten1, bytesWritten2, bytesWritten3, response_R1;char command_R[] = { 0x77, 0x04, 0x00, 0x04, 0x08 };//读X、Y轴角度 发送命令: 77 04 00 04 08char response_R[18] = { 18 };double Sensor_Angle_RX0;double Sensor_Angle_RY0;// 设置角度输出频率模式  77 05 00 0C 06(100Hz 05 50Hz 04 25Hz) 11 100Hz char command_R1[] = { 0x77, 0x05, 0x00, 0x0C, 0x00, 0x11 };//25Hz// 发送命令: 77 05 00 0B 04(115200 03 19200 02 9600) 12 115200  2 char command_R2[] = { 0x77, 0x05, 0x00, 0x0B, 0x02, 0x12 };// 保存设置命令: 77 04 00 0A 0E char command_R3[] = { 0x77, 0x04, 0x00, 0x0A, 0x0E };char dataReceived[20];wchar_t portName[] = L"\\\\.\\COM10"; // Note the 'L' before the stringhSerial = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);if (hSerial == INVALID_HANDLE_VALUE) {if (GetLastError() == ERROR_FILE_NOT_FOUND) {std::cout << "Serial port not available." << std::endl;}return 1;}dcbSerialParams.DCBlength = sizeof(dcbSerialParams);if (!GetCommState(hSerial, &dcbSerialParams)) {std::cout << "Error getting serial port state." << std::endl;CloseHandle(hSerial);return 1;}dcbSerialParams.BaudRate = CBR_9600;dcbSerialParams.ByteSize = 8;dcbSerialParams.StopBits = ONESTOPBIT;dcbSerialParams.Parity = NOPARITY;if (!SetCommState(hSerial, &dcbSerialParams)) {std::cout << "Error setting serial port state." << std::endl;CloseHandle(hSerial);return 1;}timeouts.ReadIntervalTimeout = 500;timeouts.ReadTotalTimeoutConstant = 500;timeouts.ReadTotalTimeoutMultiplier = 100;timeouts.WriteTotalTimeoutConstant = 500;timeouts.WriteTotalTimeoutMultiplier = 100;if (!SetCommTimeouts(hSerial, &timeouts)) {std::cout << "Error setting timeouts." << std::endl;CloseHandle(hSerial);return 1;}if (!WriteFile(hSerial, command_R1, sizeof(command_R1), &bytesWritten1, NULL)) {std::cout << "Error writing to serial port." << std::endl;CloseHandle(hSerial);return 1;}std::cout << "输出频率 " << std::endl;if (!ReadFile(hSerial, dataReceived, sizeof(dataReceived), &bytesRead, NULL)) {std::cout << "Error reading from serial port." << std::endl;CloseHandle(hSerial);return 1;}for (int i = 0; i < sizeof(dataReceived); i++) {printf("%02X ", dataReceived[i]);}if (!WriteFile(hSerial, command_R2, sizeof(command_R2), &bytesWritten2, NULL)) {std::cout << "Error writing to serial port." << std::endl;CloseHandle(hSerial);return 1;}std::cout << " 发送命令" << std::endl;if (!ReadFile(hSerial, dataReceived, sizeof(dataReceived), &bytesRead, NULL)) {std::cout << "Error reading from serial port." << std::endl;CloseHandle(hSerial);return 1;}for (int i = 0; i < sizeof(dataReceived); i++) {printf("%02X ", dataReceived[i]);}if (!WriteFile(hSerial, command_R3, sizeof(command_R3), &bytesWritten3, NULL)) {std::cout << "Error writing to serial port." << std::endl;CloseHandle(hSerial);return 1;}if (!ReadFile(hSerial, dataReceived, sizeof(dataReceived), &bytesRead, NULL)) {std::cout << "Error reading from serial port." << std::endl;CloseHandle(hSerial);return 1;}std::cout << "保存设置 " << std::endl;for (int i = 0; i < sizeof(dataReceived); i++) {printf("%02X ", dataReceived[i]);}std::cout << " " << std::endl;std::cout << "开始采集 " << std::endl;int i = 0;while (1){if (!WriteFile(hSerial, command_R, sizeof(command_R), &bytesWritten, NULL)) {std::cout << "Error writing to serial port." << std::endl;CloseHandle(hSerial);return 1;}if (!ReadFile(hSerial, response_R, sizeof(response_R), &response_R1, NULL)) {std::cout << "Error reading from serial port." << std::endl;//CloseHandle(hSerial);//return 1;}std::cout << i++ << std::endl;for (int i = 0; i < sizeof(response_R); i++) {printf("%02X ", response_R[i]);}std::cout << std::endl;/*//右倾角传感器if (sizeof(response_R) >= 13 && response_R[0] == 0x77 && response_R[1] == 0x10)//&& response[3] == 0x84){*///x轴int hexValues[] = { response_R[5] & 0xFF, response_R[6] & 0xFF, response_R[7] & 0xFF };int numValues = sizeof(hexValues) / sizeof(hexValues[0]);std::stringstream ss;ss << std::setfill('0') << std::setw(2) << std::hex << hexValues[0];//std::cout << "String representation: " << numValues << std::endl;for (int i = 1; i < numValues; i++) {ss << "." << std::setw(2) << std::hex << hexValues[i];}std::string resultString = ss.str();std::string inputString = resultString;std::string outputString;size_t firstDotIndex = inputString.find('.');size_t secondDotIndex = inputString.find('.', firstDotIndex + 1);if (firstDotIndex != std::string::npos && secondDotIndex != std::string::npos) {outputString = inputString.substr(0, secondDotIndex) + inputString.substr(secondDotIndex + 1);}else {outputString = inputString;}double resultDouble = std::stod(outputString);if (response_R[4] == 0x00){Sensor_Angle_RX0 = resultDouble;}else{Sensor_Angle_RX0 = -resultDouble;}std::cout << "x轴角度" << std::fixed << std::setprecision(4) << resultDouble << std::endl;//Y轴int hexValues1[] = { response_R[9] & 0xFF, response_R[10] & 0xFF, response_R[11] & 0xFF };int numValues1 = sizeof(hexValues1) / sizeof(hexValues1[0]);std::stringstream ss1;ss1 << std::setfill('0') << std::setw(2) << std::hex << hexValues1[0];//std::cout << "String representation: " << numValues << std::endl;for (int i = 1; i < numValues1; i++) {ss1 << "." << std::setw(2) << std::hex << hexValues1[i];}std::string resultString1 = ss1.str();std::string inputString1 = resultString1;std::string outputString1;size_t firstDotIndex1 = inputString1.find('.');size_t secondDotIndex1 = inputString1.find('.', firstDotIndex1 + 1);if (firstDotIndex1 != std::string::npos && secondDotIndex1 != std::string::npos) {outputString1 = inputString1.substr(0, secondDotIndex1) + inputString1.substr(secondDotIndex1 + 1);}else {outputString1 = inputString1;}double resultDouble1 = std::stod(outputString1);if (response_R[8] == 0x00){Sensor_Angle_RY0 = resultDouble1;}else{Sensor_Angle_RY0 = -resultDouble1;}std::cout << "y轴: " << std::fixed << std::setprecision(4) << resultDouble1 << std::endl;std::this_thread::sleep_for(std::chrono::milliseconds(400));}getchar();CloseHandle(hSerial);return 0;
}

当设置波特率为9600,应答模式的情况下,可以见到输出完完全全正确:

但是我需要高频率的输出,见到技术手册里面存在这样一句话,

给定的软件是可以正常运行的。

3.解决高频率输出

当我把频率直接设置成115200的时候,发现还是存在错误,应答信号直接出错了。

我想了很久,然后技术手册之中有一句话是这样说的,

每次变更通信波特率成功之后,会以原来的波特率发送应答信号,然后立即改变通信波特率。

也就是说初始的时候,波特率还是应该设置为9600,发送command_R2完毕之后,才会发生改变。因此,下面这个代码值是不能够发生变化的

只需要将command_R2的值进行变化就是可以的。

运行1348次数据仍然是正确的。

4.界面化设计

此处使用的应答模式

因为代码之中使用自动输出模式,会出现检测代码的问题,前面已经提到过。

后面的思路是在界面之中设定一个定时器,以50Hz的方式进行写读,不会出现读出来的帧数据出错的问题,规避了自动输出的局限性。之后使用线程对于这个定时器进行实时调用就可以了。

这篇关于BWS2000倾角传感器c++测试代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/516233

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

C++读写word文档(.docx)DuckX库的使用详解

《C++读写word文档(.docx)DuckX库的使用详解》DuckX是C++库,用于创建/编辑.docx文件,支持读取文档、添加段落/片段、编辑表格,解决中文乱码需更改编码方案,进阶功能含文本替换... 目录一、基本用法1. 读取文档3. 添加段落4. 添加片段3. 编辑表格二、进阶用法1. 文本替换2

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c