美文网首页
Abseil常用函数

Abseil常用函数

作者: 何亮hook_8285 | 来源:发表于2023-01-21 20:32 被阅读0次

Abseil常用函数

简介

Abseil是基于STL标准往上封装的公共库,它是较轻量级的库。

Abseil 简要组成部分如下:

  • base Abseil Fundamentals :包含初始化代码和其它部分依赖的代码。除了 C++ 标准库外不依赖外部代码
  • algorithm :C++ <algorithm> 库的增强
  • container :STL 风格容器
  • debugging :内存泄露检查
  • memory :智能指针和内存管理
  • meta :用 C++11 兼容代码支持 C++14 和 C++17 版本的 <type_traits> 库
  • numeric :支持 C++11 兼容的 128 位整数
  • strings :string 相关函数增强
  • synchronization :同步原语和抽象支持
  • time :时间方面的计算
  • types :非容器类型的工具类型

字符串

#include <iostream>
#include <absl/strings/str_join.h>
#include <absl/strings/str_split.h>
#include <absl/strings/str_cat.h>
#include <absl/strings/str_replace.h>
#include <absl/strings/str_format.h>
#include <absl/strings/substitute.h>
#include <absl/strings/string_view.h>



int main() {

    //字符串分隔数组
    std::cout << "---split----" << std::endl;
    std::vector<std::string> v=absl::StrSplit("abc,123",",");
    for(int i=0;i<v.size();i++)
    {
        std::cout << v[i] << "";
    }

    std::cout << std::endl;

    //将数组合并成字符串
    std::cout << "---join----" << std::endl;
    std::vector<std::string> strVector={"zhangsan","lisi","wangwu"};
    std::string  newStr=absl::StrJoin(strVector,"|");
    std::cout << newStr << std::endl;


    //将两个字符串拼接在一起
    std::cout << "---cat----" << std::endl;
    std::string strCat1="hello,";
    std::string strCat2="world";
    std::string newStrCat=absl::StrCat(strCat1,strCat2);
    std::cout << newStrCat << std::endl;


    //替换字符串第一种方式
    std::cout << "---Replace1----" << std::endl;
    std::string strReplace="zhangsan hello world";
    std::string newStrReplace1=absl::StrReplaceAll(strReplace,{{"zhangsan","lisi"}});
    std::cout << newStrReplace1 << std::endl;

    //替换字符串第二种方式
    std::cout << "---Replace2----" << std::endl;
    std::vector<std::pair<const absl::string_view, std::string>> replacements;
    replacements.push_back({"&", "&amp;"});
    replacements.push_back({"<", "&lt;"});
    replacements.push_back({">", "&gt;"});
    replacements.push_back({"&", "&amp;"});
    std::string newStrReplace2 = absl::StrReplaceAll("if (ptr < &foo)", replacements);
    std::cout << newStrReplace2 << std::endl;


    //字符串格式化
    //格式化占位符参考:https://abseil.io/docs/cpp/guides/format
    std::cout << "---StrFormat----" << std::endl;
    std::string newFormat = absl::StrFormat("Welcome to %s, Number %d!", "The Village", 6);
    std::cout << newFormat << std::endl;

    //替代字符串
    std::cout << "---Substitute----" << std::endl;
    std::string newSubStitute = absl::Substitute("$1 purchased $0 $2 for $$10. Thanks $1!", 5, "Bob", "Apples");
    std::cout << newSubStitute << std::endl;


    //string_view 它提供一个字符串的视图,即可以通过这个类以各种方法“观测”字符串,但不允许修改字符串
    absl::string_view strView="hello world";
    return 0;
}

容器

无序容器

flat_hash_map和flat_hash_set可替换到 std::unordered_set和 std::unordered_map ,内部使用hash实现。

#include <iostream>
#include <absl/container/flat_hash_map.h>
#include <absl/container/flat_hash_set.h>
#include <vector>

int main() {

    // 默认构造函数
    //没有添加元素进行。
    absl::flat_hash_set<std::string> set1;

    absl::flat_hash_map<int, std::string> map1;

    // 构造函数初始化列表
    absl::flat_hash_set<std::string> set2 = {{"huey"}, {"dewey"}, {"louie"},};

    absl::flat_hash_map<int, std::string> map2 =
            {{1, "huey"}, {2, "dewey"}, {3, "louie"},};

    //将set2复制到set3中
    absl::flat_hash_set<std::string> set3(set2);

    absl::flat_hash_map<int, std::string> map3(map2);

    //赋值操作
    absl::flat_hash_set<std::string> set4;
    set4 = set3;

    absl::flat_hash_map<int, std::string> map4;
    map4 = map3;

    // 移动构造函数
    //移动是有效的
    absl::flat_hash_set<std::string> set5(std::move(set4));

    absl::flat_hash_map<int, std::string> map5(std::move(map4));

   //移动赋值运算符
   //如果分配器是兼容的,可能是有效的
    absl::flat_hash_set<std::string> set6;
    set6 = std::move(set5);

    absl::flat_hash_map<int, std::string> map6;
    map6 = std::move(map5);

    // 范围的构造函数
    std::vector<std::string> v = {"a", "b"};
    absl::flat_hash_set<std::string> set7(v.begin(), v.end());

    std::vector<std::pair<int, std::string>> v1 = {{1, "a"}, {2, "b"}};
    absl::flat_hash_map<int, std::string> map7(v1.begin(), v1.end());
    return 0;
}

BTREE容器

#include <iostream>
#include <absl/container/btree_map.h>
#include <absl/container/btree_set.h>
#include <vector>

//heliang
int main() {

    absl::btree_set<std::string> set1;

    absl::btree_map<int, std::string> map1;

    // Initializer List constructor
    absl::btree_set<std::string> set2 = {{"huey"}, {"dewey"}, {"louie"},};

    absl::btree_map<int, std::string> map2 =
            {{1, "huey"}, {2, "dewey"}, {3, "louie"},};

    // Copy constructor
    absl::btree_set<std::string> set3(set2);

    absl::btree_map<int, std::string> map3(map2);

    // Copy assignment operator
    // Hash functor and Comparator are copied as well
    absl::btree_set<std::string> set4;
    set4 = set3;

    absl::btree_map<int, std::string> map4;
    map4 = map3;

    // Move constructor
    // Move is guaranteed efficient
    absl::btree_set<std::string> set5(std::move(set4));

    absl::btree_map<int, std::string> map5(std::move(map4));

    // Move assignment operator
    // May be efficient if allocators are compatible
    absl::btree_set<std::string> set6;
    set6 = std::move(set5);

    absl::btree_map<int, std::string> map6;
    map6 = std::move(map5);

    // Range constructor
    std::vector<std::string> v = {"a", "b"};
    absl::btree_set<std::string> set7(v.begin(), v.end());

    std::vector<std::pair<int, std::string>> v1 = {{1, "a"}, {2, "b"}};
    absl::btree_map<int, std::string> map7(v1.begin(), v1.end());
    return 0;
}

时间

#include <iostream>
#include <absl/time/time.h>
#include <absl/time/clock.h>


//heliang
int main() {
    //当前的绝对时间
    absl::Time t1= absl::Now();


    //转换Time对象
    time_t tt = time(NULL);
    absl::Time timeT1 = absl::FromTimeT(tt);

    auto tp = std::chrono::system_clock::from_time_t(123);
    absl::Time chronoT2 = absl::FromChrono(tp);

    int64_t unix_micros = 1674387176;
    absl::Time unixT3= absl::FromUnixMicros(unix_micros);

    //输出时间
    absl::TimeZone utc =  absl::UTCTimeZone();


    //转换unix时间戳
    uint64_t millis=absl::ToTimeT(t1);
    std::cout << millis  << "\n";
    absl::Duration hour=absl::Hours(8);
    //+8小时的误差
    std::string  newTime=absl::FormatTime("%Y-%m-%d %H:%M:%S",t1+hour, utc);
    std::cout << newTime  << "\n";

    //将字符串转时间
    absl::Time parseTime;
    std::string strDate="2023-01-03 21:00:21";
    absl::ParseTime("%Y-%m-%d %H:%M:%S",strDate,&parseTime, nullptr);
    std::cout << absl::FormatTime("%Y-%m-%d %H:%M:%S",parseTime, utc) << std::endl;


    return 0;
}

官网文档

https://abseil.io/docs/cpp/guides

相关文章

  • php-常用函数

    常用函数 常用函数: 数组常用函数

  • absl

    Google开源的C++库 Abseil 库https://blog.csdn.net/heiyeshuwu/ar...

  • excel 常用快捷键及函数

    1.常用快捷键 2.常用函数 ①零件函数 日期函数 文本函数 统计函数 随机函数 ②if函数

  • 函数进阶_2

    目录 常用内置函数 匿名函数 高阶函数 闭包 装饰器 1. 常用内置函数 1.1 range()函数 语法:ran...

  • MySQL基本使用

    函数 常用函数 数学函数 字符串函数 日期函数

  • C++常用库函数

    1.常用数学函数 #include 2.常用字符串处理函数 #include 3.其他常用函数 ...

  • 机器学习

    常用激活函数(激励函数) Sigmoid函数 Relu函数

  • python常用时间函数

    常用函数 日常写代码,经常用到时间相关的函数,以下整理了python常用的时间函数: 执行结果 此外datatim...

  • c++ 7、字符串

    1、字符串常用函数(原生) 2、字符串常用函数(扩展)

  • iOS-GCD常用函数和栅栏函数

    GCD常用函数 GCD栅栏函数

网友评论

      本文标题:Abseil常用函数

      本文链接:https://www.haomeiwen.com/subject/gxnhhdtx.html