C++ char 转 string

C++ char 转 string处理,提升速度,快速处理!

#include<iostream>
#include<cstring>

using namespace std;
const int N = 100;
char str1[N], str2[N];
string s1, s2;
int main()
{
    scanf("%s%s",str1,str2);
    s1 = str1, s2= str2;
    cout<<s1<<endl<<s2<<endl;
    //只能转为string类型处理,如果想输出sting类型,还是得用到cout,这样速度就又慢了。
    // printf("%s\n%s\n",s1,s2);
    return 0;
}

c++中 字符串的分隔符如何处理

1、先读取一整行,然后用sscanf()读取
如下题目:
地址: 航班时间
代码如下:

// 21:32~
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;


int get_second(int h, int m, int s)
{
    return h*3600 + m *60 + s;
}

int get_time()
{
    int h1, m1, s1, h2, m2, s2, d;
    string ss;
    getline(cin, ss);
    if(ss.back() != ')') ss += " (+0)";
    
    sscanf(ss.c_str(),"%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1, &h2, &m2, &s2, &d);
    return (get_second(h2, m2, s2) - get_second(h1, m1, s1) + d * 24 * 3600);
}

int main()
{
    int n;
    scanf("%d", &n);
    string line;
    getline(cin, line);
    while(n --)
    {
        int time1 = get_time(), time2 = get_time(), time = (time2 + time1)/2;
        printf("%02d:%02d:%02d\n",time/3600, time%3600/60, time%60);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值