241207-C06-string
原题链接:33265.笔记 汇总2024-12-07 18:51:29
发布于:江苏
一、string 的基本定义以及操作
1. string的输入输出
string s = "hello"; //STL
// cin >> s;
// getline(cin, s); //可以获取带有空格的字符串
// fget(s, 1005, stdin);
cout << s << endl;
for (int i=0; i<s.length(); i++){ //s.size()
cout << s[i] << endl;
}
// s.find()
2. string的基本操作
char s1[] = "234", s2[] = "23";
// cout << s1 + s2 << endl; //不合法的加法操作
// strcat(s1, s2); //concatenate 连接
// strcpy(s1, s2); //copy 覆盖复制
cout << strcmp(s1, s2) << endl;
/*
s1 < s2: -1
s1 == s2: 0
s1 > s2: 1
*/
// cout << s1 << endl;
string s1 = "", s2 = "zpl";
if(s1.empty()){
cout << "s1是一个空串";
}
else {
cout << "非空串";
}
s1 += s2;
cout << s1 << endl;
s1 = s2;
cout << s1 << endl;
if (s1 > s2){
cout << s1 << endl;
}
else{
cout << s2 << endl;
}
3. 关于sizeof
char s[105] = "hello";
cout << strlen(s) << endl; //实际大小
cout << sizeof(s) << endl; //贪婪的家伙, 包含'\0' 【大小】
int a[100] = {}; //int 4 Byte
long long b[100] = {};
cout << sizeof (a) << endl; //400 Byte
cout << sizeof (b) << endl; //800 Byte
cout << sizeof (char) << endl; //1 Byte
cout << sizeof (bool) << endl; //1 Byte
cout << sizeof (double) << endl; // 8
cout << sizeof (float) << endl; // 4
cout << sizeof (short) << endl; // 2
cout << sizeof (unsigned short) << endl;// 2
cout << sizeof (int) << endl; //4
cout << sizeof (long long) << endl; //8
4. 关于计算机的存储单位
计算机中的常用单位是 Byte(字节), 最小单位是 bit (比特)二进制 1 B = 8 bit
KB 千字节 1KB = 1024 B
MB 兆字节 1MB = 1024 KB
GB 吉字节 1024
TB
PB
二、string的5个方法
string s = "zpl666";
string a = " was ";
//1.insert(pos, string)下标为pos的位置前 插入
s.insert(3, a);
cout << s << endl;
s = "zpl666";
//2.substr(pos, len); 从pos(包含)的位置开始往后截取len个字符长度
string ns = s.substr(3, 5);
cout << ns << endl; //666
//3. erase(pos, len); 从pos(包含)的位置开始往后擦除len个字符长度
s.erase(0, 3);
cout << s << endl;
//4. replace(pos, len, string); 替换, 删除len个长度再插入 string
s.replace(0, 1, a);
cout << s << endl; //666
//5. a.find(b, pos) 在a串中从pos的位置开始往后找字符串b, 找不到返回-1
cin>>a>>b;
cout<<(int)a.find(b); //string::npos
三、string练习
实例1.A671.回文字符串
思路:首尾往中间比较
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
cin>>s;
int len=s.size()-1;
for(int i=0,j=len;i<=len,j>=0;i++,j--){
if(s[i]!=s[j]){
cout<<"No";
return 0;
}
}
cout<<"Yes";
return 0;
}
实例2.A2604.字符串比较
#include<bits/stdc++.h>
using namespace std;
string a,b;
int main(){
getline(cin,a);
getline(cin,b);
for(int i=0;i<a.size();i++){
if(a[i]>='a'&&a[i]<='z'){
a[i]-=32;
}
}
for(int i=0;i<b.size();i++){
if(b[i]>='a'&&b[i]<='z'){
b[i]-=32;
}
}
if(a>b)
cout<<">";
else if(a==b)
cout<<"=";
else cout<<"<";
return 0;
}
实例3.A31054.【字符串】【入门】输出亲朋字符串
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
cin>>s;
int len=s.size();
char t=s[0];
for(int i=0;i<len-1;i++){
s[i]+=s[i+1];
}
s[len-1]+=t;
cout<<s;
return 0;
}
实例4.A31063.【字符串】【入门】整理药名
多组测试数据的处理方式,输入一次输出一次。
#include<bits/stdc++.h>
using namespace std;
string s;
int n;
int main(){
cin>>n;
while(n--){
cin>>s;
if(s[0]>='a'&&s[0]<='z'){
s[0]-=32;
}
for(int i=1;i<s.size();i++){
if(s[i]>='A'&&s[i]<='Z'){
s[i]+=32;
}
}
cout<<s<<endl;
}
return 0;
}
实例5.A31052.【字符串】【入门】基因相关性
涉及到除法,注意数据类型。
#include<bits/stdc++.h>
using namespace std;
double n,cnt;
string a,b;
int main(){
cin>>n;
cin>>a>>b;
for(int i=0;i<a.size();i++){
if(a[i]==b[i]){
cnt++;
}
}
if(cnt/a.size()>=n){ //注意小数
cout<<"yes";
}
else cout<<"no";
return 0;
}
这里空空如也
有帮助,赞一个