• C++ 字符串用法大全


    一、字符串介绍与特征

    字符串简单说就是能够存储字符的数组,用法很多,我们来谈几个
    重点:字符串总是以\0为结尾,不管是什么类型的字符串都是这样
    字符串的头文件是iomanip
    #include <iomanip>

    二、字符串输入

    在C++中,字符串分为两种类型:charstring
    它们的不同点很多,例如:
    输入输出
    char 类型输入方式:直接cin

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	char ch;
    	cin>>ch;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    但是,这样写程序只会输入一个数,咱们写一个输出试试

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	char a;
    	cin>>a;
    	cout<<a;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行结果:

    coding1
    好,问题来了,怎样才能完整的输出呢
    只要把a设大亿点就可以了

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	char a[1000000];
    	cin>>a;
    	cout<<a;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行结果:

    coding2
    也许会发现,字符串其实是数组!

    当然,也可以指定输出哪一位

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	char a[1000000];
    	cin>>a;
    	cout<<a[1]; //输出第2位
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    对比stringchar真的是太逊啦
    string的输入输出
    输入方式:直接cin
    输出方式:直接cout

    乍一看,好像跟char没什么不同的
    BUT,看看这个示例程序,你就知道string好在哪里了

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	string a;
    	cin>>a;
    	cout<<a;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行结果:

    coding3
    我们发现:string类型允许用户输入很长的字符串,而char类型却做不到这一点,这也是为什么大家都喜欢用string-----方便!


    重点!!!字符串输入都以 (空格)、TabEnter(回车)为输入结尾

    Example:

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	string ch;
    	cin>>ch;
    	cout<<ch;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    normal

    但是,用getline函数却可以解决空格和Tab的问题

    示例代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	string ch;
    	getline(cin,ch); //注意!getline函数后括号内一定要打括号!而不是 >>
    	cout<<ch;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    example


    三、字符串长度

    charstring都有求长度函数

    1、char类型求长度

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	char a[1000];
    	cin>>a;
    	cout<<strlen(a); //strlen函数返回字符串的长度
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行结果:

    coding4
    当然,还有一个厉害的方法求长度…

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	char s[1000];
    	int len=0;
    	cin>>s;
    	for(int i=0;s[i]!='\0';i++) {
    		len++;
    	}
    	cout<<len;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    这个程序的原理是利用字符串的结尾为\0,循环求长度

    2、string类型求长度

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	string b;
    	cin>>b;
    	int len2=b.size(); 
    	int len3=b.length();
    	cout<<len2<<" "<<len3;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    string类型求长度有两个函数:.size().length()

    运行结果:
    coding5

    四、字符串查找

    Word中,有可以查找字符或单词的功能,那么我们能不能用C++实现一个底层呢?
    字符串查找函数:.find()
    示例:

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	string ch="hello abc";
    	cout<<ch.find("hello");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    find
    可见,程序在第1个位置找到了hello

    现在,我们把每个出现的位置都输出出来

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	string ch,a;
    	int pos=0;
    	getline(cin,ch);
    	getline(cin,a);
    	while(ch.find(a,pos)!=string::npos) {
    		pos=ch.find(a,pos);
    		cout<<pos<<' ';
    		pos++;
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    find1


    五、字符串删除

    ch.erase(m,n)
    ch是字符串变量名,此句代码的意思是删除从m开始的第n

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	string ch="Hello ,nice to meet you.";
    	cout<<ch.erase(6,1); //删除从第六项开始的一项
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    coding


    六、熟悉ASCII码

    ASCII
    规律:每一个大写字母与相应的小写字母相差32,且两个相邻的大小写字母的ANSCII码都相差1

    七、实际应用

    1、洛谷P5733

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
        string ch;
        cin>>ch;
        for(int i=0;i<ch.length();i++) {
        	if(ch[i]>='a' && ch[i]<='z') ch[i]-=32; //是小写字母,就转换
    	}
    	cout<<ch;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2、洛谷P1914

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	string ch;
    	int n;
    	cin>>n>>ch;
    	for(int i=0;i<ch.length();i++) {
    		if(ch[i]+n>'z') ch[i]=ch[i]+n-26;
    		else ch[i]=ch[i]+n;
    	}
    	cout<<ch;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3、洛谷P1308

    #include <bits/stdc++.h>
    using namespace std;
    
    int main ()
    {
    	string ch,src;
    	int cnt=0,pos=0,k=0;
    	getline(cin,src);
    	getline(cin,ch);
    	
    	for(int i=0;i<ch.length();i++) {
    		if(ch[i]>='A'&&ch[i]<='Z') ch[i]+=32;
    	}
    	
    	for(int i=0;i<src.length();i++) {
    		if(src[i]>='A'&&src[i]<='Z') src[i]+=32;
    	}
    	
    	ch=" "+ch+" ";
    	src=" "+src+" ";
    	
    	while(ch.find(src,pos)!=string::npos) { //found the word in scope
    		pos=ch.find(src,pos);
    		cnt++;
    		if(cnt==1) k=pos;
    		pos++;
    	}
    	
    	if(cnt==0) cout<<-1;
    	else cout<<cnt<<" "<<k;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    UPDATE:本文章最后更新于2020/7/1


  • 相关阅读:
    【软件设计师21天-考点整理】7)计算机系统构成及硬件基础知识
    web-view 调用微信小程序的方法
    学习路之PHP--laravel postman 提交表单出现419错误
    UOS 开启 VisualStudio 远程调试 .NET 应用之旅
    React组件设计,仿米游社首页频道设置页面
    npm install 报错解决方法
    【HTML5期末大作业】制作一个简单HTML我的班级网页(HTML+CSS+JS)
    图机器学习(GML)&图神经网络(GNN)原理和代码实现(前置学习系列二)
    Open Interpreter:OpenAI Code Interpreter的开源实现|本地化|可联网
    FIddler抓手机的通讯包的设置记录
  • 原文地址:https://blog.csdn.net/weixin_45122104/article/details/125628155