String类型使用scanf输入,printf输出

 示例:

#include <stdio.h>
#include <string>
using namespace std;

int main()
{
    string a;
    a.resize(100); //需要预先分配空间
    scanf("%s", &a[0]);
    printf("%s\n", a.c_str());
    return 0;
}

成员函数:

c_str

得到等效的C字符串数据

data得到等效的字符串数组
get_allocator得到分配器
copy从字符串中复制字符序列
find查找字符
rfind从后向前查找字符
find_first_of查找某个字符第一次出现的位置
find_last_of查找某个字符最后一次出现的位置
find_first_not_ofFind absence of character in string
find_last_not_ofFind absence of character in string from the end
substr生成子字符串
compare比较

   例题:成绩排序

使用scanf和printf进行输入输出

#include <stdio.h>
#include <algorithm>
#include <string>
using namespace std;

struct stu
{
    int grade;
    string name;
    int order;
};

bool CmpAscend(stu x,stu y)
{
    if(x.grade == y.grade) 
        return x.order < y.order;
    return x.grade < y.grade;
}

bool CmpDescend(stu x,stu y)
{
    if(x.grade == y.grade) 
        return x.order < y.order;
    return x.grade > y.grade;
}

int main()
{
    int m,n;
    while(scanf("%d %d",&m,&n) != EOF)
    {
        stu sss[m];
        for(int i=0;i<m;i++)
        {
            sss[i].name.resize(30);     //需要预先分配空间
            scanf("%s %d",&sss[i].name[0],&sss[i].grade);
            sss[i].order = i;
        }
        if(n==1)
        {
            sort(sss,sss+m,CmpAscend);
        }
        else
        {
            sort(sss,sss+m,CmpDescend);
        }
        for(int j=0;j<m;j++)
        {
            printf("%s %d\n",sss[j].name.c_str(),sss[j].grade);    //成员函数c_str()
        }
    }
    return 0;
}

     

 

 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值