一、一维数组
由于字符串结束多了‘\0’。
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[20];
cin>>a;
cout<<a<<endl;
int n=sizeof(a);
cout<<n<<endl;
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[20];
cin>>a;
cout<<a<<endl;
int n=strlen(a);
cout<<n<<endl;
return 0;
}
可以看到sizeof和strlen的区别,一个是指所有,一个是指占用。
#include<iostream>//一维数组和字符串的长度区别
#include<string.h>
using namespace std;
int main()
{
char a[]={'h','e','l','l','o'};
int n=sizeof(a);
cout<<n<<endl;
char b[]="hello";
int m=sizeof(b);
cout<<m<<endl;
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string a;
getline(cin,a);
cout<<sizeof(a);//得用a.length()才行
}
二、二维数组
静态定义
#include<iostream>
using namespace std;
int main()
{
const int m=2,n=3;
int a[m*n],b[m][n];
int i,j;
for(i=0;i<m*n;i++)
{
a[i]=i+1;
cout<<a[i]<<'\t';
}
cout<<endl;
int x=1;//初始值
for(int j=0;j<m;j++)
{
for(int k=0;k<n;k++)
{
b[j][k]=x;
x++;
cout<<b[j][k]<<'\t';
}
cout<<endl;
}
return 0;
}
二维字符串的长度与输出
#include<iostream>
using namespace std;
int main()
{
char month[3][5]={"Jan","Feb","Mar"};
int n=sizeof(month);
cout<<n<<endl;
cout<<month[1]<<endl;
cout<<month[1][0];
return 0;
}
三、结构体
这里的date类似于int,而today类似于自己定义的a,b,c。
#include<iostream>
using namespace std;
int main()
{
struct telelist{char name[8];char sex;char num1[5];char
num2[5];}list1[2];//telelist 是新定义的结构体类型,list1是声明的结构体名称
int i;
for(i=0;i<2;i++)
{
cin>>list1[i].name>>list1[i].sex>>list1[i].num1>>list1[i].num2;
}
for(i=1;i>=0;i--)
{
cout<<list1[i].name<<"/"<<list1[i].sex<<"/"<<list1[i].num1<<"/"<<list1[i].num2
<<endl;
}
return 0;
}
三、枚举
四、
(1)
统计一段文本中字母,空格,数字和其他字符的个数
#include<iostream>
using namespace std;
int main()
{
string str;
while(getline(cin,str))//循环输入
{
int letter=0,space=0,num=0,others=0;
int n=str.length();
for(int i=0;i<n;i++)
{
if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
{
letter++;
}
else if(str[i]==' ')
{
space++;
}
else if(str[i]>='1'&&str[i]<='9')
{
num++;
}
else
{
others++;
}
}
cout<<letter<<endl;
cout<<space<<endl;
cout<<num<<endl;
cout<<others<<endl;
}
return 0;
}
(2)
#include<iostream>
#include<string>
using namespace std;
int main()
{
string line;
while(getline(cin,line))
{
int alpha = 0, space = 0, digit = 0,other = 0;
for(auto i:line)
{
if(isalpha(i))
alpha++;
else if (isdigit(i))
digit++;
else if(i==' ')
space++;
else
other++;
}
cout<<alpha<<endl<<space<<endl<<digit<<endl<<other<<endl;
}
return 0;
}
这里auto这个功能,直接读取了line中的字符串。
isalpha判断是否是字母,isdigit判断是否是数字,可以简化判断代码。
(3)
#include<iostream>
#include<string>
using namespace std;
int main()
{
string line;
while(getline(cin,line))
{
for(auto i:line)
{
cout<<i;
}
}
return 0;
}
五、
1、
#include<iostream>
#include<string>
using namespace std;
int main()
{
string text1{"Heavy rains are pushing water levels beyond the limit."};
string text2,text3;
int k;
text2="Sluice gates at three Gorges Dam opened to dischange water.";
text3=text1+text2;
k=text3.find("Heavy");//返回H所在的位置
text3.erase(k,sizeof("Heavy"-1));//由于heavy后有\0,sizeof求出“heavy”长度是6,所以需要减1,意思是从k位置开始,删除后面5个字符
text3.insert(k,"Strong");//从k开始插入
cout<<text3<<endl;
return 0;
}
注意字符串的连接,删除,插入。string 字符串的初始化。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string text1{"Heavy rains are pushing water levels beyond the limit."};
string text2,text3;
int k;
text2={"Sluice gates at three Gorges Dam opened to dischange water."};
text3=text1+text2;
string m="Heavy";
k=text3.find(m);
text3.erase(k,m.length());
text3.insert(k,"Strong");
cout<<text3<<endl;
return 0;
}
改用length求长度
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char text1[]="Heavy rains are pushing water levels beyond the limit.";
int k;
char text2[]="Sluice gates at three Gorges Dam opened to dischange water.";
char text3;
//text3=text1+text2;//报错,char无法这样连接
strcat(text1,text2);
string m="Heavy";
cout<<text1<<endl;
return 0;
}
可以看到string功能更强大,char就会受限很多,例如插入,删除等。
五、
#include<iostream>
#include<string.h>
using namespace std;
struct WordList//定义字典结构体
{
char word[20];//单词
int freq;//频率
};
int main()
{
WordList list[1000];//声明结构体
int N=0;//记录不重复的个数
int i,j,k,s;
char tmp[20];//临时的中间变量
WordList box;
cin>>tmp;
while(strcmp(tmp,"xyz")!=0)
{
for(i=0;i<N;i++)
{
if(strcmp(list[i].word,tmp)==0)
{
list[i].freq++;
break;
}
}
if(i>=N)
{
strcpy(list[i].word,tmp);
list[i].freq=1;
N++;
}
cin>>tmp;
}
cout<<"原始输出:"<<endl;
for(i=0;i<N;i++)
{
cout<<list[i].word<<"\t"<<list[i].freq<<endl;
}
//**************排序********************
for(j=0;j<N-1;j++)
{
s=j;//假设最小的下标
for(k=j+1;k<N;k++)
{
if(list[s].freq>list[k].freq)
{
s=k;//最小的变化
}
}
box=list[s];//这里只需最后交换,不用每次都交换
list[s]=list[j];
list[j]=box;
}
cout<<"排序后输出:"<<endl;
for(i=0;i<N;i++)
{
cout<<list[i].word<<"\t"<<list[i].freq<<endl;
}
return 0;
}
注意排序,只需最后交换,不用每次比较完就交换。再有注意char 里的一些函数使用。
#include<iostream>
#include<string.h>
using namespace std;
struct WordList//定义字典结构体
{
char word[20];//单词
int freq;//频率
};
int main()
{
WordList list[2];
char a[]="wang";
char b[]="zhang";
strcpy(list[0].word,a);//复制
list[0].freq=5;
strcpy(list[1].word,b);//复制
list[1].freq=3;
int c=strcmp(list[1].word,list[0].word);
cout<<c<<endl;
return 0;
}
按照ascall码顺序比较。