C和C++的一些常用案例学习之一

  1. fprintf、sprintf 函数的作用,如下图代码所示:
    fprintf(fp, "name: %s, phone: %s\n", "hello","world"); //fp是一个打开的文件指针,既把数据写到文件中
    sprintf(buf, "name: %s, phone: %s\n", "hello","world");//buf是一个打开的字符数组,既把数据写到数组中
    printf("%s\r\n,"hello"); //这个都懂,就不说了

2)在linux线程安全的时候,何时用自旋锁,何时用互斥锁,如下图所示:
在这里插入图片描述
从上图可知,如果需要锁的内容比较少的话,如 count++,则使用自旋锁,这样可以减少线程切换(以为线程的切换代价远远大于自旋锁的等待时间);
如果需要锁的内容比较多的话,如排序、列表插入等,需要互斥锁更好;

3)线程池的作用
1.避免线程太多,使得内存耗尽;
2.避免创建与销毁线程所带来的代价;
线程池三要素:任务队列,执行队列,管理组件;

4)链表的插入和删除,用宏定义实现,以后可以直接使用,代码如下:

#define LIST_INSERT(item, list) do {	\
    item->prev = NULL;					\
    item->next = list;					\
    if ((list) != NULL) (list)->prev = item; \
    (list) = item;						\
} while(0)


#define LIST_REMOVE(item, list) do {	\
    if (item->prev != NULL) item->prev->next = item->next; \
    if (item->next != NULL) item->next->prev = item->prev; \
    if (list == item) list = item->next; 					\
    item->prev = item->next = NULL;							\
} while(0)
  1. setw函数的使用,需要包含iomanip这个头文件;
    说明:setw函数是右对齐,如下代码:
cout << "Hello World!" <<setw(5) << "aaaa"<< endl;

测试效果,如下:
在这里插入图片描述
6)C++中引用变量的学习总结
1.引用地址的操作,如下图所示:
在这里插入图片描述
测试代码如下:

#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
    int a = 0;
    int* ptr = NULL;
    int& m = a;
    ptr = &m; //把m的地址赋给p,相当于把a的地址赋给p
    *ptr = 20;
    cout<<a<<endl;
    return 0;
}

2.可以用动态分配的内存空间初始化一个引用变量;
测试代码如下:

#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
    int& refp = *new int; //这个空间只有别名,但程序可以引用到 2)注意书写格式:“ *new ”
    refp = 222;
    cout<<refp<<endl;
    delete& refp; //注意书写格式:“ delete& ”
    return 0;
}

输出结果:

222

3.引用的作用:主要用作函数的参数和函数的返回值;
7)C++中,类的嵌套,代码如下:

#include <iostream>

using namespace std;

class CC1
{
public:
    int x;
    void Func();
    class CC2
    {
    public: //必须写public,否则会编译出错
        int x;
        void Func();
    }obj; //这个对象必须有,否则会编译错误;
    //如果没有obj这个对象,需要 CC2 obj定义一个对象
};

void CC1::Func(){ x  = 20; }
void CC1::CC2::Func(){ x = 10; }

int main()
{
    CC1 c1;

    c1.Func();
    cout<<c1.x<<endl;
    c1.obj.Func();
    cout<<c1.obj.x<<endl;

    return 0;
}

7)友元函数
友元函数的作用:主要是可以访问类的数据成员,提高了程序的运行效率;
在类中,访问限定对友元是不起作用的;
用法: 通常使用友元函数来取类的数据成员值,而不是修改;
测试代码如下:

#include <iostream>
using namespace std;

class Test
{
public:
    Test(int x, int y)
    {
        this->x = x;
        this->y = y;
    }
    friend int handle(const Test& obj); //友元函数的声明
private:
    int x;
    int y;
};
int handle(const Test& obj) //友元函数的实现
{
    return obj.x + obj.y;
}
int main(void)
{
    Test t1(2,3);
    cout<<handle(t1)<<endl;
    return 0;
}

8)友元类的代码案例

#include <iostream>
using namespace std;

class A
{
public:
    A(int tma,int tmb) : aa(tma),bb(tmb){ }
    friend class B; //A把B当朋友,B可以访问A的数据成员;(我把你当朋友,你可以访问我,但我不可以访问你)
private:
    int aa;
    int bb;
};

class B
{
public:
    B(int tmp = 1) { mm = tmp; }
    void handle(const A& obj)
    {
        cout<< obj.aa + obj.bb + mm <<endl;
    }
private:
    int mm;
};

int main(void)
{
    A a(33,44);
    B b(10);
    b.handle(a); //打印A类和B类的数据成员之和
    return 0;
}

输出结果:

87

9)new的使用
用new开辟的内存空间没有名字,指向其首地址的指针是使用其空间的唯一途径;
10)运算符重载相关代码案例

#include <iostream>
using namespace std;

class Test
{
public:
    Test(int a = 0, int b = 0)
    {
        x = a;
        y = b;
    }
    Test operator+(const Test& obj)
    {
        Test tmp;
        tmp.x = this->x + obj.x;
        tmp.y = this->y + obj.y;
        return tmp;
    }
    int getX() { return x;}
    int getY() { return y;}
private:
    int x;
    int y;
};
int main()
{
    Test t1(4,10), t2(4,5);
    Test ret = t1 + t2;
    cout<<ret.getX()<<endl;
    cout<<ret.getY()<<endl;
    return 0;
}

输出结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值