自用的C++代码

CMAKE

命令行里指定前缀

cmake .. -G "MinGW Makefiles" -DCMAKE_PREFIX_PATH="C:/Users/xd15zhn/Documents/cpplibraries"

set(CMAKE_PREFIX_PATH "C:/Users/xd15zhn/Documents/cpplibraries")

带库链接

set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

功能函数

读csv文件

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
vector<vector<double>> read_csv(const string &filename) {
    fstream fin;
    fin.open(filename, ios::in);
    vector<vector<double>> data;
    string line;
    while (getline(fin, line)) {
        istringstream sin(line);
        vector<double> values;
        string value;
        while (getline(sin, value, ','))
            values.push_back(stod(value));
        data.push_back(values);
  }
    return data;
}
Mat read_csv(const string &filename) {
    fstream fin;
    fin.open(filename, ios::in);
    vector<double> values;
    string line;
    int row=0, column;
    while (getline(fin, line)) {
        istringstream sin(line);
        string value;
        column = 0;
        while (getline(sin, value, ',')) {
            values.push_back(stod(value));
            column++;
        }
        row++;
    }
    return Mat(row, column, values);
}

读字典文件

map<string, double> readKeyValuePairs(const string& fileName) {
    map<string, double> myMap;
    ifstream file(fileName);
    string line;
    while (getline(file, line)) {
        stringstream ss(line);
        string key;
        double value;
        getline(ss, key, ',');
        ss >> value;
        myMap[key] = value;
    }
    return myMap;
}
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <sstream>

void readKeyValuePairs(std::map<std::string, double>& myMap, const std::string& fileName) {
    std::ifstream file(fileName);
    std::string line;
    while (std::getline(file, line)) {
        std::stringstream ss(line);
        std::string key;
        double value;
        std::getline(ss, key, ',');
        ss >> value;
        myMap[key] = value;
    }
}
int main() {
    std::map<std::string, double> myMap;
    readKeyValuePairs(myMap, "data.txt");
    for (const auto& pair : myMap)
        std::cout << pair.first << ": " << pair.second << std::endl;
    return 0;
}

合并成单个头文件

#ifndef READCSV_H
#define READCSV_H
#include <zhnmat.hpp>
#include <map>
zhnmat::Mat read_csv(const std::string &filename);
std::map<std::string, double> readKeyValuePairs(const std::string& fileName);
#endif // READCSV_H

#define READCSV_IMPLEMENTATION

#ifdef READCSV_IMPLEMENTATION
#include <fstream>
#include <sstream>
#include <string>

zhnmat::Mat read_csv(const std::string &filename) {
    using namespace std;
    fstream fin;
    fin.open(filename, ios::in);
    vector<double> values;
    string line;
    int row=0, column=0;
    while (getline(fin, line)) {
        istringstream sin(line);
        string value;
        column = 0;
        while (getline(sin, value, ',')) {
            values.push_back(stod(value));
            column++;
        }
        row++;
    }
    fin.close();
    if (row==0 || column==0)
        return zhnmat::Mat(3, 1);
    return zhnmat::Mat(row, column, values);
}

std::map<std::string, double> readKeyValuePairs(const std::string& fileName) {
    using namespace std;
    map<string, double> myMap;
    ifstream file(fileName);
    string line;
    while (getline(file, line)) {
        stringstream ss(line);
        string key;
        double value;
        getline(ss, key, ',');
        ss >> value;
        myMap[key] = value;
    }
    return myMap;
}
#endif // READCSV_IMPLEMENTATION

OpenCV系列

文件写

Mat mapl1, mapl2, mapr1, mapr2;
initUndistortRectifyMap(cameraMatrix1, distCoeffs1, R1, P1, imageSize, CV_16SC2, mapl1, mapl2);
initUndistortRectifyMap(cameraMatrix2, distCoeffs2, R2, P2, imageSize, CV_16SC2, mapr1, mapr2);
FileStorage fs("maplr.xml", FileStorage::WRITE);
fs << "mapl1" << mapl1;
fs << "mapl2" << mapl2;
fs << "mapr1" << mapr1;
fs << "mapr2" << mapr2;
fs.release();

文件读

FileStorage fs("E:/vsproject/Project1/maplr.xml", FileStorage::READ);
fs["mapl1"] >> mapl1;
fs["mapl2"] >> mapl2;
fs["mapr1"] >> mapr1;
fs["mapr2"] >> mapr2;
fs.release();

其他

计时

#include <windows.h>
long t1 = GetTickCount();
sim1.Simulate();
long t2 = GetTickCount();
cout << t2 - t1 << endl;

lambda表达式

    connect(slidPosture[0],&QSlider::valueChanged,this,[=](int a){lblPosture[0]->setNum(a*6-300);});

函数作为参数

int fa(int a, int b)
{
	return a + b;
}
int g(int a, int b, int(*f)(int a, int b))
{
	return f(a, b);
}
int main()
{
	int ans = g(3, 4, fa);
	return 0;
}

类继承数组

#include<iostream>
using namespace std;

class Parent{
public:
    virtual void show()	{ cout << "parent" << endl; };
    virtual ~Parent() {};
};

class Son : public Parent{
public:
    virtual void show() { cout << "son" << endl; };
    virtual ~Son() {};
};

void fun(Parent* p){
    p->show();
}

int main()
{
    Parent *p[3];
    Son son0, son1, son2;
    p[0] = &son0;
    p[1] = &son1;
    p[2] = &son2;
//    Parent p1;
    fun(p[0]);
    fun(p[1]);
    fun(p[2]);
//    delete p[0];
//    delete p[1];
//    delete p[2];
    return 0;
}

打乱

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void randperm(int Num){
    vector<int> temp;
    for (int i = 0; i < Num; ++i)
        temp.push_back(i + 1);
    random_shuffle(temp.begin(), temp.end());
    for (int i = 0; i < temp.size(); i++)
        cout << temp[i] << " ";
}

宏定义中字符串拼接#与##

C语言宏定义的几个坑和特殊用法
##表示把字符串联在一起。

#include <iostream>
#define STOP_TIMER(x)  TIM##x##A
int main()
{
    int TIM1A = 1;
    std::cout<<STOP_TIMER(1)<<std::endl;
    return 0;
}

#表示将其变为字符串。
g++ -E m.cpp -o m.i可以查看生成的预编译文件,也就是查看宏展开的结果。

#define MACRO_TEST(x)  Test x(#x)
int main()
{
    MACRO_TEST(insname1);
}

滤波器

class LowPassFilter
{
public:
    double filter(double DataIn) {
        _delay[0] = DataIn + 1.7*_delay[1] - 0.7325*_delay[2];
        double DataOut = (_delay[0] + 2*_delay[1] + _delay[2]) * 0.008125;
        _delay[2] = _delay[1];
        _delay[1] = _delay[0];
        return DataOut;
    };
private:
    double _delay[3];
};

#ifdef SUPPORT_DEBUG
    #include <iostream>
#elif defined(USE_TRACELOG)
    #include "tracelog.h"
#else
    #include <iostream>
#endif  // SUPPORT_DEBUG

一行并查集

有没有一段代码,让你为人类的智慧击节叫好?

int find(int x){
        return x==Fa[x]?x:Fa[x]=find(Fa[x]);
}

随机数

/**********************
随机数生成器
**********************/
class RandEngine {
public:
    RandEngine() {
        _gen.seed(time(0));
        _gen = std::default_random_engine((unsigned int)time(0));
        _NormDis = std::normal_distribution<double>(0, 1);
        _UniFloatDis = std::uniform_real_distribution<double>(0, 1);
    }
    double rand01() { return _UniFloatDis(_gen); }  // [0,1]均匀分布
    double randn() { return _NormDis(_gen); }  // 正态分布
    double randint(int n) {
        _UniIntDis = std::uniform_int_distribution<unsigned>(0, n);
        return _UniIntDis(_gen); }  // 整数均匀分布
private:
    std::default_random_engine _gen;  // 生成初始化种子
    std::normal_distribution<double> _NormDis;
    std::uniform_real_distribution<double> _UniFloatDis;
    std::uniform_int_distribution<unsigned> _UniIntDis;
};
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值