C++记录

一、new 与delete

1.静态数组

#include <iostream>
using namespace std;
 
//new和delete运算符
int main()
{
    //使用new 分配一个int类型的空间
    int* p = new int;
    *p = 100;
    cout << *p << endl;
    //使用完了以后对内存进行释放
    delete p;
    //释放的是p指向的内存,但是不会删除指针变量p本身,p可以重新指向另一个新分配的内存块。
    return 0;
}

2. 动态数组 

#include <iostream>
using namespace std;
 
//创建动态数组
int main()
{
    //typeName* parr=new typeName[元素的个数]
    
    //int num = 10;
    // int arr[num];    //会报错,这种方式创建数组要求括号中是常量值
    //int* p = new int[num];   //这样是可以的
    int* p = new int[5];
 
    //对动态数组进行操作
    *p = 10;
    p[1] = 20;
 
    cout << p[0] << endl;//10
    cout << p[1] << endl;//20
    cout << p[2] << endl;//不确定值
    //释放动态数组的内存
    delete[] p;
    return 0;
}

3. 动态结构体

#include <iostream>
#include<string>
using namespace std;
 
//创建动态结构体
//定义一个结构体
struct student {
    string name;
    int age;
    double height;
};
 
int main()
{
    //student stu = { "tom",20,178.5 }; //普通结构体变量
    //student* p_stu = &stu;  //指针类型
    //动态结构体
    student* p_stu = new student;
    //访问方式1
    (*p_stu).name = "hello";
    (*p_stu).age = 18;
    (*p_stu).height = 178.5;
    cout << (*p_stu).name << "\t" << (*p_stu).age << "\t" << (*p_stu).height << endl;
 
    //访问方式2
    p_stu->name = "kitty";
    p_stu->age = 20;
    p_stu->height = 180.0;
    cout << p_stu->name << "\t" << p_stu->age << "\t" << p_stu->height << endl;
    //如果标识符是一个指针类型的变量,那么应该使用箭头运算符
 
    delete p_stu;
    return 0;
}

二、队列 std::queue<type> myqueue

myqueue.push(77);
int a=myqueue.front();
int b=myqueue.back();
myqueue.pop()

三、容器std::vector<type> myvector

myvector.resize(10);
myvector.push_back(0x0a);
myvector.clear();

四、std::pair

pair<double,float> a;
a.first=1.0;
a.second=2.00;

五、智能指针

{
  std::shared_ptr<int> foo = std::make_shared<int> (10);
}

六、文件读取写入

float rtk_x=0,rtk_y=0;
std::ofstream Out;
Out.open("/home/init.txt",std::ios::out);//清空并覆盖模式,Out.open("/home/init.txt",std::ios::app)//追加模式
Out.setf(std::ios::fixed,std::ios::floatfield);
Out.orecision(9);//小数点后9位
Out<<rtk_x<<" "<<rtk_y<<" "<<std::endl;

七、数据类型转换

7.1 doule->uint8_t

//将double rtk_x转化为uint_8模式,从十号地址开始存放
uint8_t send_date[36];
double rtk_x=100.00;
*(double*)&send_date[10]=rtk_x;

7.2 uint8_t->float

float fa=0;
uint8_t uc[4];
uc[0]=0x01;
uc[1]=0x01;
uc[2]=0x01;
uc[3]=0x01;
memcpy(&fa,uc,4)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值