memcpy c++结构体运算符重载

 memcpy是拷贝一段内存地址,在一些和设备打交道的程序中使用的较多,和memset配合使用,特别需要注意“\0”,如果字符数组转成字符串,而字符串是以"\0"作为结尾符,如果使用错误会造成:

1.越界,取到脏数据,或者下一块本不该属于它的内存地址;

2.没有取到预想的数据;

 重载等于号运算符时,特别是注意空和空的对比是能够匹配到的,所以有些遍历在写完后要做单元测试或者打桩测试,避免任何意外的情况发生(比如别人传了个空指针,而自己没有做空指针判断,很可能造成程序崩溃);

下面贴一个简单的qt控制台应用程序做说明:

#-------------------------------------------------
#
# Project created by QtCreator 2020-12-11T21:28:39
#
#-------------------------------------------------

QT       += core

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Demo
TEMPLATE = app


SOURCES += main.cpp

HEADERS  += CGlobal.h
#ifndef GLOBAL_H
#define GLOBAL_H

#include <string>
using namespace std;

struct DEVICE_INFO
{
    int index = 0;
    string name = {};
    string strVarUUID = {};
    string strTaskUUID = {};

    DEVICE_INFO(){
        index = 0;
        name = {};
        strVarUUID = {};
        strTaskUUID = {};
    }

    bool operator==(const DEVICE_INFO &other) const {
        if ((this->strTaskUUID == other.strTaskUUID) && (this->strVarUUID == other.strVarUUID))
            return true;
        else
            return false;
    }

    void operator=(const DEVICE_INFO &other) {
        this->index = other.index;
        this->name = other.name;
        this->strVarUUID = other.strVarUUID;
        this->strTaskUUID = other.strTaskUUID;
    }
};

#endif
#include <QApplication>
#include <QDebug>

#include "CGlobal.h"

#include <string>
#include <vector>
#include <algorithm>    //使用find()函数要包含该头文件
using  namespace  std;

using namespace std;

#define FILENAME_LEN 10

void * my_memcpy(void *dst,const void *src,int count)
{
    void * ret = dst;
    while (count--)
    {
        *(char *)dst = *(char *)src;
        dst = (char *)dst + 1;
        src = (char *)src + 1;
    }
    return(ret);
}

int main(int argc, char*argv[])
{
    QApplication app(argc, argv);
    string strName = "times12_times12_times12_times12_";
    char cName[FILENAME_LEN]={};
    memset(cName, 0, FILENAME_LEN);
    memcpy(cName, strName.c_str(), strName.length() > FILENAME_LEN ? FILENAME_LEN - 1 : strName.length());
    // my_memcpy(cName, strName.c_str(), strName.length() > FILENAME_LEN ? FILENAME_LEN : strName.length());
    qInfo() << cName;
    /********************************/
    DEVICE_INFO device1;
    device1.index = 1;
    device1.name = "shidian";
    device1.strTaskUUID = "123";
    device1.strTaskUUID = "456";

    DEVICE_INFO device2;
    device2.index = 2;
    device2.name = "UPS";
    device2.strTaskUUID = "123";
    device2.strTaskUUID = "456";

    if (device1 == device2)
        qInfo() << "equal";
    else
        qInfo() << "not equal";

    DEVICE_INFO device3;
    device2.index = 3;
    device2.name = "KGDY";

    DEVICE_INFO device4;
    device2.index = 4;
    device2.name = "Battery";

    //特别注意(空等于空,还能相等,这在有些判断的时候特别重要)
    if (device3 == device4)
        qInfo() << "equal";
    else
        qInfo() << "not equal";

    /****************************/
    vector<int> num_list = { 2,4,6,8,10,12 };
    int find_num = 11;    //要查找的元素,类型要与vector中的元素类型一致
    vector<int>::iterator num = find(num_list.begin(), num_list.end(), find_num);  //返回一个迭代器指针

    if (num != num_list.end())   //查找成功
    {
        qInfo() << "元素 " << *num << " 在num_list中"<<endl;   //注意指针的输出方式
        qInfo() << "其索引为 " << distance(num_list.begin(), num) << endl;
        //distance()函数用于计算两个迭代器表示的范围内包含元素的个数
    }
    else
        qInfo() << "元素 " << find_num << " 不在num_list中" << endl;

    return app.exec();

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值