c++ 常用小函数

#include  <fstream>

using namespace std;
int main(int argc,char* argv[])
{
    map<string, string> data;

    ifstream fin(argv[1]);
    //ifstream in("...", ios::in); 
    if(!fin)
    {
        cout<<"can not open"<< argv[1]<<endl;
        return -1;
    }

    while(!fin.eof())
    {
        string str;
        getline(fin,str);

        if (str[0] == '#')
        {
            // 以‘#’开头的是注释
            continue;
        }

        int pos = str.find(":");
        if (pos == -1)
            continue;
        std::string key = str.substr( 0, pos );
        std::string value = str.substr( pos+2, str.length()-pos-2 );
        data[key] = value;

        if ( !fin.good() )
            break;
     }
}

#include  <fstream>

ofstream out; 
outputDistMap.open("../txtFiles/DistMaps.txt");
//ofstream out("...");
out << "idx=" <<idx<< endl;

string to float

std::string resolution_=data["resolution"];
resolution=atof(resolution_.c_str());

int to string

#include <stdlib.h>
int a=2;
char temp[4];
sprintf(temp,"%d",a);
string b=temp;

时间测试

#include "time.h"

clock_t start, finish;
double duration;
start = clock();
...
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC; 

从路径中读取所有文件

utils.h

#ifndef CUIZHOU_JNI_OPENCV_SSD_MOBILENET_DEMO_UTIL_H
#define CUIZHOU_JNI_OPENCV_SSD_MOBILENET_DEMO_UTIL_H

#include <stdlib.h>
#include <dirent.h>
#include <string>
#include <vector>

std::vector<std::string> readFileList(char *basePath);
#endif //CUIZHOU_JNI_OPENCV_SSD_MOBILENET_DEMO_UTIL_H

utils.cpp


std::vector<std::string> readFileList(char *basePath)
{
    std::vector<std::string> result;
    DIR *dir;
    struct dirent *ptr;
    char base[1000];

    if ((dir=opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }

    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)
            continue;
        else if(ptr->d_type == 8)    ///file
        {printf("d_name:%s/%s\n",basePath,ptr->d_name);
            result.push_back(std::string(ptr->d_name));}
        else if(ptr->d_type == 10)    ///link file
        {printf("d_name:%s/%s\n",basePath,ptr->d_name);
            result.push_back(std::string(ptr->d_name));}
        else if(ptr->d_type == 4)    ///dir
        {
            memset(base,'\0',sizeof(base));
            strcpy(base,basePath);
            strcat(base,"/");
            strcat(base,ptr->d_name);
            result.push_back(std::string(ptr->d_name));
            readFileList(base);
        }
    }
    closedir(dir);
    return result;
}

关于字符串的操作:
https://www.renfei.org/blog/introduction-to-cpp-string.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值