C++使用小细节--持续更新

1. fixed

在C++中,如果是直接使用cout,对于一些情况可能会出现科学计数法的情况,但是如果是使用fixed之后,就可以按照比较符合平常的方式来进行输出
下面是两个例子:

在这里插入图片描述
顺便提一下,在C++中==setprecision()==可以设置输出长度(小数点后)也就是保留多少位小数

2. C++中结构体内重载运算符

以下代码来自本人博客

struct node{
    int a;
    int b;
    friend bool operator < (const node& x,const node& y){
        return x.a < y.a;
    }
};
priority_queue<node> que;

不仅可以在优先队列里面进行设置优先的顺序,还可以在对结构体进行排序的时候不用再另外写出cmp函数。
比如说对于 一 个 结构体进行排序时可以对cmp函数和重载运算符之间先选择一个即可

struct node{
    int a;
    int b;
    friend bool operator < (const node& x,const node& y){
        return x.a < y.a;
    }
};

bool cmp(node a,node b){
    return a.a < b.a;
}

3. reserve() resize()

官方解释
参考链接
vector 的reserve增加了vector的capacity,但是它的size没有改变!而resize改变了vector的capacity同时也增加了它的size!
reserve 会给vector预分配内存,但是不会对其进行初始化
resize会重新分配大小,改变容器的容量大小,还会创建对象

#include <cstddef>
#include <new>
#include <vector>
#include <iostream>
 
// minimal C++11 allocator with debug output
template <class Tp>
struct NAlloc {
    typedef Tp value_type;
    NAlloc() = default;
    template <class T> NAlloc(const NAlloc<T>&) {}
 
    Tp* allocate(std::size_t n)
    {
        n *= sizeof(Tp);
        std::cout << "allocating " << n << " bytes\n";
        return static_cast<Tp*>(::operator new(n));
    }
 
    void deallocate(Tp* p, std::size_t n) 
    {
        std::cout << "deallocating " << n*sizeof*p << " bytes\n";
        ::operator delete(p);
    }
};
template <class T, class U>
bool operator==(const NAlloc<T>&, const NAlloc<U>&) { return true; }
template <class T, class U>
bool operator!=(const NAlloc<T>&, const NAlloc<U>&) { return false; }
 
int main()
{
    int sz = 100;
    std::cout << "using reserve: \n";
    {
        std::vector<int, NAlloc<int>> v1;
        v1.reserve(sz);
        for(int n = 0; n < sz; ++n)
            v1.push_back(n);
    }
    std::cout << "not using reserve: \n";
    {
        std::vector<int, NAlloc<int>> v1;
        for(int n = 0; n < sz; ++n)
            v1.push_back(n);
    }
}

输出:

using reserve: 
allocating 400 bytes
deallocating 400 bytes
not using reserve: 
allocating 4 bytes
allocating 8 bytes
deallocating 4 bytes
allocating 16 bytes
deallocating 8 bytes
allocating 32 bytes
deallocating 16 bytes
allocating 64 bytes
deallocating 32 bytes
allocating 128 bytes
deallocating 64 bytes
allocating 256 bytes
deallocating 128 bytes
allocating 512 bytes
deallocating 256 bytes
deallocating 512 bytes

4. 优先队列重载运算符的三种方式

  1. 非本人原创,来自下面这位大佬的文章
    原文链接

方式1 友元函数

struct node {
    int val, deep;
    friend bool operator < (node a, node b) {   
        if(a.val == b.val) {
            return a.deep > b.deep;     
        }
        return a.val > b.val;
    }
};

得到的顺序和自己意愿相反

方式2 常引用

struct node {
    int val, deep;
    bool operator < (const node &a) const {   
        if(val == a.val) {
            return deep > a.deep;     
        }
        return val > a.val;
    }
};

方式3 结构体之外

struct node {
    int val, deep;
    
};

bool operator < (const node &a, const node &b) {   
        if(a.val == b.val) {
            return a.deep > b.deep;     
        }
        return a.val > b.val;
}

5. OJ数据制作(文件读写)

经常使用的比较简单的方式:

读取文件

freopen("name","r",stdin);
name为文件名
"r"为读取权限
"w"为写入权限
stdin为标准从文件中读取

写入文件

freopen("name","w",stdout);
name为文件名
"r"为读取权限
"w"为写入权限
stdout为标准输出到文件
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值