首页 > 编程语言 >我的C++常用函数

我的C++常用函数

时间:2023-04-26 13:57:49浏览次数:38  
标签:std 常用 return 函数 ss C++ source prev string

/* 根据多个分隔符来分隔字符串. 比如 " :,]" */
std::vector<std::string> SplitString(const std::string& str, const std::string& delimiters) {
    std::vector<std::string> tokens;
    size_t prev = 0, pos;
    while ((pos = str.find_first_of(delimiters, prev)) != std::string::npos) {
        if (pos > prev) {
            tokens.push_back(str.substr(prev, pos-prev));
        }
        prev = pos + 1;
    }
    if (prev < str.length()) {
        tokens.push_back(str.substr(prev, std::string::npos));
    }
    return tokens;
}

static std::string Trim(std::string source)
{
        std::replace( source.begin(), source.end(), '\t', ' '); // replace all '\t' to ' '
        std::replace( source.begin(), source.end(), '\n', ' ');
        std::replace( source.begin(), source.end(), '\r', ' ');

    std::string result = source.erase(source.find_last_not_of(" ") + 1);
    return result.erase(0, result.find_first_not_of(" "));
}

static double Stod(std::string s) {
    std::string ss = Trim(s);
    //return ss.empty() ? 0.0 : std::stod(ss);
    return ss.empty() ? 0.0 : std::atof(ss.c_str());
}

static int Stoi(std::string s) {
    std::string ss = Trim(s);
    return ss.empty() ? 0 : std::stoi(ss);
}

static std::string get_curr_format_hhmmss() { // eg: 09:30:00
    time_t t = time(NULL); 
    struct tm *p = localtime(&t);
    char s[64];
    strftime(s, sizeof(s), "%H:%M:%S", p);
    return std::string(s);
}

 

标签:std,常用,return,函数,ss,C++,source,prev,string
From: https://www.cnblogs.com/xiaouisme/p/17355671.html

相关文章

  • 2022 蓝桥杯国赛 C++ B 组
    A\(\text{379187662194355221}\)。\(\text{dp}\)。C++Code#include"bits/stdc++.h"usingnamespacestd;usingi64=longlong;intmain(){ios::sync_with_stdio(false);cin.tie(nullptr);vector<vector<i64>>f......
  • 12 | C语言中的函数类型和函数指针类型
    函数类型和函数指针类型的区别,让我们先看一个例子#include<iostream>usingnamespacestd;typedefint(Func)(int);typedefint(*Func_p)(int);intf(inta){returna;}intmain(){Func_pp=f;Func*p_ptr=f;cout<<p(0)<<endl;cout<<p_ptr(1)......
  • pgsql 常用SQL
     ##查看数据库版本。selectversion();##查看表空间select*frompg_tablespace;##大小相关的。1)查看表空间大小selectpg_tablespace_size('pg_default');2)查看各个表空间的大小selectspcname,pg_size_pretty(pg_tablespace_size(spcname))frompg_tables......
  • web前端开发常用的代码编写工具有哪些?
    不同类型的开发人员使用的工具大有不同,所以说没有绝对好,对任何人员都适用的工具,我们只能以友好度,功能性,扩展性,界面/体验,跨平台等等这些来作为评判标准。下面我们就给它分类并一一介绍:大师级别vivi对于使用过unix的朋友来说,绝对是再熟悉不过的代码编辑器,有多少伟大的程序和代码......
  • Linux常用命令redis相关
    一、查询文件中的内容vim文件名使用/xxx即可查询文件中的xxx单词,n下一个选中单词,N上一个选中单词。一、防火墙1.查看防火墙状态:firewall-cmd--state2.启动防火墙systemctlstartfirewalld3.关闭防火墙systemctlstopfirewalld二、redis1、开启redis服......
  • pid算法函数实现,c语言版
     #include<stdio.h>floatpid(floatsetpoint,floatprocess_variable,floatkp,floatki,floatkd,floatdt,float*integral,float*last_error){//Calculateerrorfloaterror=setpoint-process_variable;//Calculateintegral......
  • c++,x11,linux查找窗口
    如题点击查看代码#include<X11/Xlib.h>#include<stdio.h>voidfindWindow(Display*display,Windowwindow,char**windowName,Window*result){Windowroot,parent,*children;unsignedintnChildren;if(XFetchName(display,window,windo......
  • C++ 类和对象1
    1.初步认识面向对象与面向过程C语言是面向过程的语言,关注的是解决问题的过程比如,首先对问题进行拆解,然后用函数的方式对每个部分逐个实现,最后通过函数之间的相互调用将问题解决C++是面向对象的,关注的是对象,如何从一个问题中抽离出不同的对象,然后靠对象之间的交互解决......
  • PB常用函数
    弹出窗口:messagebox()基本写法:Messagebox('标题','内容')完整写法: MessageBox('标题','内容',图标,按键,默认值)           (1)其中标题与内容为要显示的字符串,不可省略,但可以省略,即什么也不显示,例如Messagebox('','')这样也是正确的单里面的东西一样也不能少!   ......
  • PowerBuilder常用字符串函数
    Fill()功能建立一个由指定字符串填充的指定长度的字符串。语法Fill(chars,n)参数chars:string类型,指定用于重复填充的字符串n:long类型,指定由该函数返回的字符串的长度返回值String。函数执行成功时返回n个字符的字符串,该字符串以参数chars中的字符串重复填充而成。如果参数cha......