常用c++函数1 example

1.float 转字符串方法 

#include<string.h>
#include<sstream>


stringstream ss;
ss<<angle[i/2]*180/3.141516;
string s =ss.str();

2 传入结构体的数组

#include <iostream>
#include<sstream>
#include <vector>
#include <fstream>
#include<string.h>
#include<algorithm>
using namespace std;

typedef struct nn_output
{
    // double *outbuf;
    double outbuf[2];
    int count;
}output_nn;

int get_output(output_nn ouput2[])
{
    double out_men[2]={7.88,8.99};
    double out_men1[2]={1.88,2.99};

    ouput2[0].count=6;
    for(int i=0;i<2;i++)
    {
        ouput2[i].outbuf[0]=out_men[0];
        ouput2[i].outbuf[1]=out_men[1];
    }
    // ouput2[0].outbuf=out_men; 
    ouput2[1].count=0;
    return 0;
}

int main() 
{   
    output_nn* output3=NULL;
    output3 = (output_nn*)malloc(2 * sizeof(output_nn));
    if(output3){
    int a=get_output(output3);
    for(int i=0;i<2;i++)
    {
        cout<<output3[i].count<<"_"<<endl;
        for(int j=0;j<2;j++)
            cout<<"i:"<<i<<"  j:"<<j<<" "<<output3[i].outbuf[j]<<endl;
    }}
    free(output3);
    return 0;
}


output :
6_
i:0  j:0 7.88
i:0  j:1 8.99
0_
i:1  j:0 7.88
i:1  j:1 8.99

3 结构体指针数组使用

#include<iostream>
#include<sstream>
#include <vector>
#include <fstream>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    float* num1[1]={NULL};
    float* num2[2]={NULL,NULL};
    num2[1]=(float*)malloc(sizeof(float)*10);
    num1[0]=(float*)malloc(sizeof(float)*5);
    float num3[5]={0,0,0,0,3};
    for(int i=0;i<5;i++)
    {
        num1[0][i]=i;
    }
    memset(num2[1],9.0,sizeof(float)*10);
    memcpy(num2[1]+5,num1[0],sizeof(float)*5);
    num2[0]=num1[0];
    for(int i=0;i<5;i++)
    {
        cout<<"num1[0]_data_d "<<*(num1[0]+i)<<endl;
        cout<<"num2[0]_data "<<*(num2[0]+i)<<endl;
    }
    for(int i=0;i<10;i++)
        cout<<"num2[0]_data_d "<<*(num2[1]+i)<<endl;
    cout<<"the len is :"<<*num1[0]<<endl;
    cout<<"the num3 len is :"<<sizeof(num3)/sizeof(num3[0])<<endl;

    if (num1[0]) free(num1[0]);
    if (num2[1]) free(num2[1]);
    return 0;
}


output:
num1[0]_data_d 0
num2[0]_data 0
num1[0]_data_d 1
num2[0]_data 1
num1[0]_data_d 2
num2[0]_data 2
num1[0]_data_d 3
num2[0]_data 3
num1[0]_data_d 4
num2[0]_data 4
num2[0]_data_d 1.6495e-33
num2[0]_data_d 1.6495e-33
num2[0]_data_d 1.6495e-33
num2[0]_data_d 1.6495e-33
num2[0]_data_d 1.6495e-33
num2[0]_data_d 0
num2[0]_data_d 1
num2[0]_data_d 2
num2[0]_data_d 3
num2[0]_data_d 4
the len is :0
the num3 len is :5

4 NMS c++实现

#include <vector>
#include <algorithm>
#include <cmath>
#include<iostream>

using namespace std;

//定义结构体
struct Box
{
    double x;
    double y;
    double w;
    double h;
    double score;
};

//计算IOU
double IOU(Box box1, Box box2)
{
    double x1 = max(box1.x, box2.x);
    double y1 = max(box1.y, box2.y);
    double x2 = min(box1.x + box1.w, box2.x + box2.w);
    double y2 = min(box1.y + box1.h, box2.y + box2.h);
    double w = max(0.0, x2 - x1);
    double h = max(0.0, y2 - y1);
    double intersection = w * h;
    double area1 = box1.w * box1.h;
    double area2 = box2.w * box2.h;
    return intersection / (area1 + area2 - intersection);
}

//NMS函数
vector<Box> NMS(vector<Box> boxes, double thresh)
{
    vector<Box> res;
    if (boxes.size() == 0)
        return res;
    //按照score排序
    sort(boxes.begin(), boxes.end(), [](Box a, Box b) { return a.score > b.score; });
    //把第一个box加入结果
    res.push_back(boxes[0]);
    for (int i = 1; i < boxes.size(); i++)
    {
        bool flag = true;
        for (int j = 0; j < res.size(); j++)
        {
            if (IOU(boxes[i], res[j]) > thresh)
            {
                flag = false;
                break;
            }
        }
        if (flag)
            res.push_back(boxes[i]);
    }
    return res;
}

int main()
{
    vector<Box> boxes;
    boxes.push_back({1, 1, 2, 2, 0.9});
    boxes.push_back({2, 2, 2, 2, 0.8});
    boxes.push_back({3, 3, 2, 2, 0.7});
    boxes.push_back({4, 4, 2, 2, 0.6});
    boxes.push_back({5, 5, 2, 2, 0.5});
    boxes.push_back({6, 6, 2, 2, 0.4});
    boxes.push_back({7, 7, 2, 2, 0.3});
    boxes.push_back({8, 8, 2, 2, 0.2});
    boxes.push_back({9, 9, 2, 2, 0.1});
    vector<Box> res = NMS(boxes, 0.1);
    for (auto box : res)
    {
        cout << box.x << " " << box.y << " " << box.w << " " << box.h << " " << box.score << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值