• boost 库记录


    windows 安装 boost

    VS2017编译配置Boost库_煮雪品茶的博客-CSDN博客

    boost 库安装的时候,要 cd 到库目录里面编译,如果没有 cd 进去,而是直接绝对路径编译,可能导致找不到 build.bat 的错误,如果库目录垮盘符,cd 是不能直接切盘符的,参考下面

    Windows的cmd中cd指令无法转换路径怎么办? 【百科全说】

    1、boost::split

    boost::split 是把 string 切分到 vector 里,下面我是按照空格切分的,但是如果有多个连续空格的情况,会把空串扔进 string 里,如果想提取 string 里的单词的话,切分完记得判断一下 vector 里面的 string 是不是空串,也就是长度为零

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main() {
    6. std::string s1("aa bb cc");
    7. std::vector split_string;
    8. boost::split(split_string, s1, boost::is_any_of(" "));
    9. for (auto i : split_string) {
    10. cout << "size:" << i.length() << ",i:" << i << endl;
    11. }
    12. cout << "======" << endl;
    13. std::string s2(" aa bb cc");
    14. split_string.clear();
    15. boost::split(split_string, s2, boost::is_any_of(" "));
    16. for (auto i : split_string) {
    17. cout << "size:" << i.length() << ",i:" << i << endl;
    18. }
    19. return 0;
    20. }
    21. /*
    22. 输出
    23. size:2,i:aa
    24. size:2,i:bb
    25. size:2,i:cc
    26. ======
    27. size:0,i:
    28. size:0,i:
    29. size:0,i:
    30. size:0,i:
    31. size:0,i:
    32. size:2,i:aa
    33. size:2,i:bb
    34. size:2,i:cc
    35. */

    2、boost::trim

    boost::trim 可以删除字符串前后空格、换行符等

    1. #include
    2. #include
    3. using namespace std;
    4. int main() {
    5. string str = "\r\n\t aa bb cc\r\n\t";
    6. boost::trim(str);
    7. cout << str << endl;
    8. cout << str.size();
    9. return 0;
    10. }
    11. /*
    12. 输出
    13. aa bb cc
    14. 8
    15. */

    3、boost::lexical_cast

    lexical_cast 的数值转换对标C语言的 atoi 等,字符串转换对标 C++ 的 to_string,据说类型检查会更严格

    1. #include
    2. #include
    3. using namespace std;
    4. int main() {
    5. long y = boost::lexical_cast<long>("2000");
    6. cout << y << endl;
    7. string str = boost::lexical_cast(3000);
    8. cout << str << endl;
    9. return 0;
    10. }
    11. /*
    12. 输出
    13. 2000
    14. 3000
    15. */

    4、boost::algorithm::erase_all

    字符串擦除可以删除所有我们指定的子字符串

    1. #include
    2. #include
    3. using namespace std;
    4. int main() {
    5. string line = "a,,b,,c";
    6. boost::algorithm::erase_all(line, ",,");
    7. cout << line << endl;
    8. return 0;
    9. }
    10. /*
    11. 输出
    12. abc
    13. */

    5、boost::optional

    有时候,我们对于一些变量是否初始化会定义一个特殊的值作为没初始化的标志,比如:0,但是万一 0 也是一个正常值怎么办,这个时候就可以使用我们的 boost::optional,里面有个初始化的函数判断,帮助我们判断该变量是否已经初始化

    像编译完的 protobuf,也有类似的函数

    1. #include
    2. #include "boost/optional.hpp"
    3. using namespace std;
    4. void print(boost::optional<int> &i) {
    5. // 是否初始化判断
    6. if (i.is_initialized()) {
    7. cout << i.get() << endl;
    8. } else {
    9. cout << "该值未初始化" << endl;
    10. }
    11. }
    12. int main() {
    13. boost::optional<int> a = 1;
    14. boost::optional<int> b;
    15. print(a);
    16. print(b);
    17. }
    18. /*
    19. 输出
    20. 1
    21. 该值未初始化
    22. */

    如果我们想直接标记某个变量没有初始化(标记异常情况,但是函数又必须返回一个值),我们就可以使用 boost::none,这个在一些返回值的函数中使用比较多

    1. #include
    2. #include "boost/optional.hpp"
    3. using namespace std;
    4. boost::optional<int> set(int i) {
    5. // 假设 i > 0 是正常情况
    6. if (i > 0) {
    7. boost::optional<int> x = i;
    8. return x;
    9. }
    10. return boost::none;
    11. }
    12. void print(boost::optional<int> &i) {
    13. // 是否初始化判断
    14. if (i.is_initialized()) {
    15. cout << i.get() << endl;
    16. } else {
    17. cout << "该值未初始化" << endl;
    18. }
    19. }
    20. int main() {
    21. boost::optional<int> a = set(1);
    22. boost::optional<int> b = set(-1);
    23. print(a);
    24. print(b);
    25. }
    26. /*
    27. 输出
    28. 1
    29. 该值未初始化
    30. */

    6、std::transform

    std::transform 就是把 A  数组的数据倒腾到 B 数组,如下面,A 和 B 可以不是同一个类型,但是接收者 B 的容量必须大于等于 A,不过我们可以在倒腾的过程中对参数进行处理,如下面的 op 函数

    1. #include // std::transform
    2. #include // std::cout
    3. #include // std::vector
    4. struct temp {
    5. int x;
    6. float y;
    7. };
    8. int op(temp t) { // 参数必须为 A 的元素类型
    9. // 这里可以对参数进行处理
    10. return t.x * 2;
    11. }
    12. int main() {
    13. std::vector A;
    14. std::vector<int> B;
    15. // set some values:
    16. for (int i = 1; i < 6; i++) {
    17. temp t = {i, 0};
    18. A.push_back(t); // A: 1 2 3 4 5
    19. }
    20. B.resize(A.size()); // B 的容量不能比 A 小
    21. std::transform(A.begin(), A.end(), B.begin(), op); // B: 2 4 6 8 10
    22. for (int &it : B) {
    23. std::cout << it << ' '; // 2 4 6 8 10
    24. }
    25. return 0;
    26. }

  • 相关阅读:
    手把手教你语音识别(四)
    poi读取word中的目录大纲,导入
    Redis 实现 用户输入密码错误5次锁定
    数据结构与算法之美读书笔记12
    关于K8s中资源配置范围管理(LimitRange)的一些笔记
    变量和函数提升(js的问题)
    初步了解ES7-ES12
    NewStarCTF2023week2-游戏高手
    火车头采集GPT改写插件/txt数据GPT改写软件说明文档
    C: is too small to hold all values of ‘enum ABC‘
  • 原文地址:https://blog.csdn.net/qq_38781075/article/details/133238505