STL 常用

void read_file(const std::string& path, std::string& out)
{
    std::ifstream fs(path, std::ios_base::binary);
    fs.seekg(0, std::ios_base::end);
    auto size = fs.tellg();
    fs.seekg(0);
    out.resize(static_cast<size_t>(size));
    fs.read(&out[0], size);
}

std::string file_extension(const std::string& path)
{
    std::smatch m;
    auto pat = std::regex("\\.([a-zA-Z0-9]+)$");
    if (std::regex_search(path, m, pat)) {
        return m[1].str();
    }
    return std::string();
}
#ifndef S_ISREG
#define S_ISREG(m)  (((m)&S_IFREG)==S_IFREG)
#endif
#ifndef S_ISDIR
#define S_ISDIR(m)  (((m)&S_IFDIR)==S_IFDIR)
#endif

bool is_file(const std::string& path)
{
    struct stat st;
    return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode);
}

bool is_dir(const std::string& path)
{
    struct stat st;
    return stat(path.c_str(), &st) >= 0 && S_ISDIR(st.st_mode);
}
template <class Fn>
void split(const char* b, const char* e, char d, Fn fn)
{
    int i = 0;
    int beg = 0;

    while (e ? (b + i != e) : (b[i] != '\0')) {
        if (b[i] == d) {
            fn(&b[beg], &b[i]);
            beg = i + 1;
        }
        i++;
    }

    if (i) {
        fn(&b[beg], &b[i]);
    }
}
bool is_valid_path(const std::string& path) {
    size_t level = 0;
    size_t i = 0;

    // Skip slash
    while (i < path.size() && path[i] == '/') {
        i++;
    }

    while (i < path.size()) {
        // Read component
        auto beg = i;
        while (i < path.size() && path[i] != '/') {
            i++;
        }

        auto len = i - beg;
        assert(len > 0);

        if (!path.compare(beg, len, ".")) {
            ;
        } else if (!path.compare(beg, len, "..")) {
            if (level == 0) {
                return false;
            }
            level--;
        } else {
            level++;
        }

        // Skip slash
        while (i < path.size() && path[i] == '/') {
            i++;
        }
    }

    return true;
}
bool is_hex(char c, int& v)
{
    if (0x20 <= c && isdigit(c)) {
        v = c - '0';
        return true;
    } else if ('A' <= c && c <= 'F') {
        v = c - 'A' + 10;
        return true;
    } else if ('a' <= c && c <= 'f') {
        v = c - 'a' + 10;
        return true;
    }
    return false;
}
inline std::string to_lower(const char* beg, const char* end)
{
    std::string out;
    auto it = beg;
    while (it != end) {
        out += ::tolower(*it);
        it++;
    }
    return out;
}

size_t writeFileContent(const std::string & filename, const char * buff, size_t buffLen, bool isAppend)
{
	std::string mod = "ab+";
	if (!isAppend)
	{
		mod = "wb+";
	}

	FILE * f = fopen(filename.c_str(), mod.c_str());
	if (f == NULL)
	{
		return 0;
	}
	size_t writeLen = fwrite(buff, 1, buffLen, f);
	fclose(f);
	return writeLen;
}






### C++ STL 常用功能及示例使用方法 #### 容器 (Containers) STL 提供了多种容器来存储和管理数据。以下是常用的几种: - **Vector**: 动态数组,支持随机访问。 ```cpp std::vector<int> vec = {1, 2, 3}; vec.push_back(4); for(auto& elem : vec){ std::cout << elem << " "; } ``` 上述代码展示了向量的创建、添加元素以及遍历操作[^2]。 - **List**: 双向链表,适合频繁插入删除场景。 ```cpp std::list<int> lst; lst.push_front(10); lst.push_back(20); ``` - **Map**: 键值对集合,基于红黑树实现,默认按键排序。 ```cpp std::map<std::string, int> m; m["one"] = 1; m["two"] = 2; ``` #### 迭代器 (Iterators) 迭代器用于遍历容器中的元素,不同类型的容器可能有不同的迭代器形式。 ```cpp std::vector<int>::iterator it = vec.begin(); while(it != vec.end()){ std::cout << *it << " "; ++it; } ``` 上述例子演示了如何通过迭代器遍历 `vector` 中的所有元素[^4]。 #### 算法 (Algorithms) STL 提供了大量的通用算法,这些算法独立于具体的数据结构。 - 排序: ```cpp #include <algorithm> std::sort(vec.begin(), vec.end()); ``` 此处调用了标准库中的 sort 函数对向量进行升序排列[^3]。 - 查找: ```cpp if(std::find(lst.begin(), lst.end(), 10) != lst.end()) std::cout << "Found"; ``` - 转换: ```cpp std::transform(vec.begin(), vec.end(), vec.begin(), [](int i){return i*2;}); ``` 以上介绍了部分 STL 的基础组件及其简单应用实例。掌握它们能够极大地简化程序设计过程并增强代码可读性和维护性[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值