string的运用

本文展示了C++中对字符串的各种操作,包括遍历、输出、连接、插入、删除、查找、替换等基本操作,以及使用C++标准库中的string类成员函数进行字符串处理。通过实例代码,深入理解C++字符串处理的常用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<string>
using namespace std;
int main(){
    string str="abcd";
    for(int i=0;i<str.length();i++){
        printf("%c",str[i]);
    }
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str;
    cin>>str;
    cout<<str;
    return 0;
}
#include<stdio.h>
#include<string>
using namespace std;
int main(){
    string str="abcd";
    printf("%s\n",str.c_str());
    return 0;
}
#include<stdio.h>
#include<string>
using namespace std;
int main(){
    string str="abcd";
    for(string::iterator it=str.begin();it!=str.end();it++){
        printf("%c",*it);
    }
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str1="abc",str2="xyz";str3;
    str3=str1+str2;
    str1+=str2;
    cout<<str1<<endl;
    cout<<str3<<endl;
    return 0;
}
#include<stdio.h>
#include<string>
using namespace std;
int main(){
    string str1="aa",str2="aaa",str3="abc",str4="xyz";
    if(str1<str2)printf("ok1\n");
    if(str1!=str3)printf("ok2\n");
    if(str4>=str3)printf("ok3\n");
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str1="abcxyz",str2="opq";
    str.insert(str.begin()+3,str2.begin(),str2.end());
    cout<<str<<endl;
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str="abcdefg";
    str.erase(str.begin()+4);
    cout<<str<<endl;
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str="abcdefg";
    str.erase(str.begin()+2,str.end()-1);
    cout<<str<<endl;
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str="abcdefg";
    str.erase(3,2);
    cout<<str<<endl;
    return 0;
}
#include<stdio.h>
#include<string>
using namespace std;
int main(){
    string str="abcd";
    str.clear();
    printf("%d\n",str.length());
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str="Thank you for your smile.";
    cout<<str.substr(0,5)<<endl;
    cout<<str.substr(14,4)<<endl;
    cout<<str.substr(19,5)<<endl;
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
    if(string::npos==-1){
        cout<<"-1 is true."<<endl;
    }
    if(string::npos==4294967295){
        cou<<"4294967295 is also true."<<endl;
    }
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str="Thank you for your smile.";
    string str2="you";
    string str3="me";
    if(str.find(str2)!=string::npos){
        cout<<str.find(str2)<<endl;
    }
    if(str.find(str2,7)!=string::npos){
        cout<<str.find(str2,7)<<endl;
    }
    if(str.find(str3)!=string::npos){
        cout<<str.find(str3)<<endl;
    }else{
        cout<<"I know there is no position for me."<<endl;
    }
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str="Maybe you will turn around.";
    string str2="will not";
    string str3="surely";
    cout<<str.replace(10,4,str2)<<endl;
    cout<<str.replace(str.begin(),str.begin()+5,str3)<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超翔之逸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值