AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

string字符串处理函数

作者: 作者的头像   bug一个不六 ,  2023-03-19 10:16:53 ,  所有人可见 ,  阅读 45


1


#include"bits/stdc++.h"
#include<sstream>
using namespace std;

inline int sum(int i) //inline 关键字
{
     return i*i;
}

int main()
{
     //rand()
     srand(time(NULL));
     int x = 5;
     printf("%d",rand()%x); //随机输出0 ~ (x - 1)

     //sting to int,long long,float,double
     string s = "45";
     x = stoi(s);
     long long ll = stoll(s);
     cout<<x<< " " << ll <<endl;
     s = "0.001";
     double d = stod(s);
     cout<<d<<endl;




     //stringstream
     stringstream ssin;
     string S = "123 0.4 abc";
     ssin << S;
     int a;
     float b;
     string c;
     ssin >>a>>b>>c;
     cout<<a<<" "<<b<<" "<<c<<endl;

     //getline(istream &is , string &str , char delim)
     string data = "1_2_3_4_5_6";
     stringstream ss(data);
     string item;
     cout << data << endl;
     while (getline(ss, item, '_')) 
          cout << item << ' ';

     //sscanf(char *str,"%d%d%s", &i,&i2, &s);
     string t = "20030121";
     int year,month,day;
     sscanf(t.c_str(),"%04d%02d%02d",&year,&month,&day);
     printf("year = %d, month = %d, day = %d\n",year,month,day);

     //sprintf(char *str,""%d%d%s", &i,&i2, &s");
     char cc[30];
     sprintf(cc,"year = %d, month = %d, day = %d",year,month,day);
     puts(cc);

     //substr(pos,len);
     string g = "1234567890abcd";
     t = g.substr(0,4);
     cout<<t<<endl;


     //strstr(char *str1,char *str2) //kmp算法 O(n)
     string j = "0123456789";
     string sj = "345";
     char *sttt = strstr(j.c_str(),sj.c_str());
     printf("c = %c\n",*sttt);

     //s.find('a'/"abc") ,O(n^2)
     int yy = j.find(sj);
     cout<<yy<<endl;

     //isdigit()
     cout<<isdigit('9')<<endl;

     //tolower()
     cout<<(char)tolower('A')<<endl;
     //toupper()
     cout<<(char)toupper('a')<<endl;

     //atoi,atoll,atof
     char str[] = "123456";
     int xx = atoi(str);
     cout<<xx<<endl;


     return 0;
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息