C++经典code片段之学习总结

1. C++宏定义中的#/##/#@含义

  • define 中 # 的含义

    “#” 是字符串化的意思,即:加双引号,作用是把宏定义中#后边的参数转化成一个字符串;

  • define 中 ## 的含义

    “##” 宏拼接,是字符串宏拼接的意思,作用是把##前后边的参数拼接在一起

  • define 中的 #@ 的含义

    “#@” 单字符标记符号,即加单引号

  • “#”实例
    #define STRING(X) (#X)
    则下边两条语句等价:
    char *char_p1 = “hello”
    char *char_p2 = STRING(hello);

  • “#” C++ code 实例

#include <iostream>
using namespace std;

#define OUTPUT(A) cout<<#A<<":"<<A<<endl;

int main()
{
	int a = 1, b = 2;
	OUTPUT(a);
	OUTPUT(b);
	OUTPUT(a+b);
	return 0;
}
  • 运行结果

    a:1
    b:2
    a+b:3
    [Finished in 0.9s]

  • “##” C code 实例

#include <stdio.h>

#define printToken(n) printf( "Print the value of token"#n" = %d\n", token##n )

int main()
{
	int token9 = 100;
	printToken(9);
}

运行结果:
Print the value of token9 = 100

2. C++友元函数重载<<输出流

  • 什么是友元函数
    在类中声明的对外的接口,可以是private属性,也可以是public属性。通过友元函数可以访问类的私有成员变量。
  • 示例代码
#include <cstdio>
#include <iostream>
using namespace std;

class CPoint
{
public:
    CPoint(int x_,int y_):x(x_),y(y_){}

private:
	//declare the friend function
    friend ostream & operator <<(ostream & os,const CPoint & p);

private:
    int x,y;
};

//重载<<输出流
ostream & operator <<(ostream & os,const CPoint & p){
        return os << "x = "<<p.x  << " y = "<< p.y << endl;
}

int main()
{
	CPoint point(1, 2);
	cout << point << endl;
	return 0;
}

3. C++ 文件流的读取

std::ifstream 文件流类的使用

#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <stdint.h>
#include <iostream>


int main()
{
    std::ifstream cmn_cfg_file("bphy_cmn_cfg.txt");
    std::string cmn_string;

    printf("come here!\n");
    if (cmn_cfg_file.is_open())
    {
        while(cmn_cfg_file.peek() != EOF)
        {
            cmn_cfg_file >> cmn_string;
            std::string string_value;
            string_value = cmn_cfg_file.peek();
            std::cout << cmn_string << std::endl;
            printf("dbgGuo: %s\n", cmn_string.c_str());
        }
        cmn_cfg_file.close();
    }
    else
    {
        printf("[ERROR:] open file fail!\n");
    }
}

4. C++ 二进制文件流读取数据

输入流:std::fstream fin;
输出流:std::ostream fout;

#include <stdio.h>
#include <stdint.h>
#include <fstream>

typedef char (*pArr)[100];

int main()
{
   uint8_t IqArray[10] = {1, 2, 3, 4, 5};

   std::fstream fin;
   fin.open("D:\\Desktop\\testVec_nr\\dspVec\\case522\\out\\dlSymbPortData.bin", std::ios::in | std::ios::binary);
   if (!fin.is_open())
   {
      printf("[ERROR]: File open failed!!!\n");
   }
   else
   {
      printf("[INFO]: File open sucess!\n");
   }

   fin.read((char*)IqArray, sizeof(uint8_t) * 4);
   //close file
   fin.close();

   printf("dbgGuo: IqArray[0]: 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X\n", IqArray[0], IqArray[1], IqArray[2], IqArray[3], IqArray[4]);
   return 0;
}

xxxx

5. xxxx

xxxx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值