C++:常用函数

#ifndef _UTILS_HPP_
#define _UTILS_HPP_

#include <unistd.h>	// access F_OK
#include <stdlib.h>	// system
#include <sstream>	// ostringstream
#include <assert.h>
#include <vector>
#include <dirent.h>
#include <cstring>

namespace utils {



// 路径是否以正斜杠结束
std::string pathWithSlash(const std::string path, bool withSlash=true) {
	if(withSlash == true && path[path.length() - 1] != '/')
		return path + '/';
	if(withSlash == false && path[path.length() - 1] == '/')
		return path.substr(0, path.length() - 1);
	return path;
}


// 获取路径中包含的文件名,默认不带后缀
std::string getFileName(std::string path, bool withExtension=false) {
	size_t a = path.find_last_of('/');
	size_t b = path.find_last_of('.');
	assert(b < path.length());

	if(withExtension)
		return path.substr(a+1);
	return path.substr(a+1, b-a-1);
}


// 获取文件或目录的绝对路径
std::string getAbsolutePath(const char* pathOrFile) {
    char abs_path_buff[PATH_MAX];
    if(realpath(pathOrFile, abs_path_buff))
        return abs_path_buff;
    return NULL;
}


// 递归创建文件夹
void mkdirp(const std::string Path) {
	if(access(Path.c_str(), F_OK) == -1) {
		std::string dir = "mkdir -p " + Path;
		system(dir.c_str());
	}
}


// 转为字符串类型
template<typename T>
std::string toString(const T& t){
	std::ostringstream oss;
	oss << t;
	return oss.str();  
}


// 类型转换
// 用法:utils::typeCovert<string, int>("111")
// 用法:utils::typeCovert<int, string>(123)
template<class in, class out>
out typeCovert(const in &value) {
	std::stringstream ss;
	ss << value;
	out res;
	ss >> res;
	return res;
}


// 搜索文件夹
std::vector<std::string> listdir(const char *path, const std::string extension="") {
        std::vector<std::string> res;

        int lex = extension.length();

        DIR *dir;
	struct dirent *dirp;
        std::stringstream ss;
 
        assert((dir=opendir(path)) != NULL);

	while((dirp=readdir(dir)) != NULL) {
                if(strcmp(dirp->d_name,".") == 0 || strcmp(dirp->d_name,"..") == 0)	// current dir OR parrent dir
			continue;
                else if(dirp->d_type == 8 || dirp->d_type == 10) {			// file OR link file
			ss.str("");
			ss << path << "/" << dirp->d_name;
			if(extension == "")
				res.push_back(ss.str());
			else if(ss.str().substr(ss.str().size()-lex, lex) == extension)
				res.push_back(ss.str());		
		}
                else if(extension == "") {						// folder
                    ss.str("");
                    ss << path << "/" << dirp->d_name;
                    res.push_back(ss.str());
                }
	}
	closedir(dir);
        return res;
}


void _recurseListdir(const char *path, const std::string extension, std::vector<std::string>& res) {
    static int lex = extension.length();

    DIR *dir;
    struct dirent *dirp;
    std::stringstream ss;

    assert((dir=opendir(path)) != NULL);

    while ((dirp=readdir(dir)) != NULL) {
            if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..") == 0)
                    continue;
            else if(dirp->d_type == 8 || dirp->d_type == 10) {
                    ss.str("");
                    ss << path << "/" << dirp->d_name;
                    if(extension == "")
                            res.push_back(ss.str());
                    else if(ss.str().substr(ss.str().size()-lex, lex) == extension)
                            res.push_back(ss.str());
            }
    }
    dir=opendir(path);
    while ((dirp=readdir(dir)) != NULL) {
            if(strcmp(dirp->d_name,".") == 0 || strcmp(dirp->d_name,"..")==0)   // current dir OR parrent dir
                    continue;
            else if(dirp->d_type == 4){                                         // dir
                    ss.str("");
                    ss<<path<<"/"<<dirp->d_name;
                    if(extension == "")
                            res.push_back(ss.str());
                    _recurseListdir(ss.str().c_str(), extension, res);
            }
    }
    closedir(dir);
}


// 递归搜索文件夹
std::vector<std::string> walkdir(const char *path, const std::string extension="") {
        static std::vector<std::string> res;
        _recurseListdir(path, extension, res);
        return res;
}



} // namespace utils

#endif
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sstream是C++标准库中的一个类,它提供了将字符串作为流来处理的功能。我们可以使用sstream来进行字符串的输入输出,以及字符串和其他类型的转换。其中,stringstream继承自iostream类,因此它可以使用iostream中的输入输出操作符(<<和>>),也可以使用iostream中的流操作(如getline函数)。 使用stringstream时,我们需要先创建一个stringstream对象,并将需要处理的字符串传递给它。然后,我们可以使用iostream中的输入输出操作符或流操作来对字符串进行处理。例如: ```c++ #include <sstream> #include <iostream> #include <string> using namespace std; int main() { stringstream ss("123 4.56"); // 创建一个stringstream对象,并初始化为"123 4.56" int num; double dbl; ss >> num >> dbl; // 从stringstream中读取数据到变量num和dbl中 cout << "num = " << num << ", dbl = " << dbl << endl; stringstream ss2; ss2 << "Hello, " << "world!"; // 将字符串拼接到stringstream中 string str; getline(ss2, str); // 从stringstream中读取拼接后的字符串 cout << str << endl; return 0; } ``` 输出结果为: ``` num = 123, dbl = 4.56 Hello, world! ``` 在这个例子中,我们首先创建了一个stringstream对象,并将其初始化为"123 4.56"。然后,我们使用iostream中的输入操作符(>>)将stringstream中的数据读取到变量num和dbl中。接下来,我们创建了另一个stringstream对象,并使用iostream中的输出操作符(<<)将字符串拼接到stringstream中。最后,我们使用iostream中的流操作(getline函数)从stringstream中读取拼接后的字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值