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

简介: 文章目录1. fixed2. C++中结构体内重载运算符3. reserve() resize()4. 优先队列重载运算符的三种方式方式1 友元函数方式2 常引用方式3 结构体之外5. OJ数据制作(文件读写)读取文件写入文件

1. fixed


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

下面是两个例子:

微信图片_20220608203647.png

顺便提一下,在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 友元函数


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为标准输出到文件
目录
相关文章
|
10月前
|
C++
C++编码规范——日积月累、持续更新
C++编码规范——日积月累、持续更新
180 0
|
1月前
|
存储 自然语言处理 C++
刷题用到的非常有用的函数c++(持续更新)
刷题用到的非常有用的函数c++(持续更新)
35 1
|
1月前
|
C++
C++核心编程三:函数提高(持续更新)
C++核心编程三:函数提高(持续更新)
|
1月前
|
编译器 C++
C++核心编程二:引用(持续更新)
C++核心编程二:引用(持续更新)
|
1月前
|
程序员 编译器 C++
C++核心编程一:内存分区模型(持续更新)
C++核心编程一:内存分区模型(持续更新)
|
1月前
|
C++
C++基础学习:通讯录管理系统(持续更新)
C++基础学习:通讯录管理系统(持续更新)
|
1月前
|
网络协议 关系型数据库 MySQL
[mysql 基于C++实现数据库连接池 连接池的使用] 持续更新中
[mysql 基于C++实现数据库连接池 连接池的使用] 持续更新中
|
1月前
|
关系型数据库 MySQL C++
[mysql C++ 简单连接到深入] 持续更新中
[mysql C++ 简单连接到深入] 持续更新中
|
10月前
|
编译器 C语言 C++
【C/C++】从 C 到 C++ (持续更新)
从 C语言转到 C++ 需要注意的事项
73 1
|
安全 IDE 数据挖掘
C/C++中常用必会的专业单词(持续更新 目前165个)
C/C++中常用必会的专业单词(持续更新 目前165个)
204 0

相关实验场景

更多