文章目录
1. 字符与整数的联系 —— ASCII码
2. 字符数组(记得多'\0') 2.1 字符数组的输入输出 2.2 字符数组的常用操作 2.3 遍历字符数组中的字符
3. 标准库类型 string 3.1 定义和初始化 3.2 string 上的操作 3.2.1 string的读写 3.2.2 使用getline读取一整行 3.2.3 string的empty和size操作 3.2.4 string的比较 3.2.5 为string对象赋值 3.2.6 两个string对象相加 3.2.7 字面值和string对象相加
3.3 处理string对象中的字符(重点)
1. 字符与整数的联系 —— ASCII码
每个常用字符都对应一个-128~127的数字,二者之间可以相互转化。
# include <iostream>
using namespace std;
int main ( )
{
char c = 'a' ;
cout << ( int ) c << endl;
int a = 66 ;
cout << ( char ) a << endl;
return 0 ;
}
常用ASCII值:’A’-‘Z’ 是65~90,’a’-‘z’是97-122,’0’-‘9’是48-57 字符可以参与运算,运算时会将其当做整数:
# include <iostream>
using namespace std;
int main ( )
{
int a = 'B' - 'A' ;
int b = 'B' * 'A' ;
char c = 'A' + 2 ;
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0 ;
}
1.1 练习 — 统计数字和字母个数
输入一行字符,统计出其中数字字符的个数,以及字母字符的个数
# include <iostream>
using namespace std;
int main ( )
{
char c;
int numbers = 0 , chars = 0 ;
while ( cin >> c)
{
if ( c >= '0' && c <= '9' ) numbers++ ;
else if ( c >= 'A' && c <= 'z' || c >= 'a' && c <= 'z' ) chars++ ;
}
printf ( "numbers:%d\nchars:%d" , numbers, chars) ;
return 0 ;
}
2. 字符数组(记得多’\0’)
字符串就是字符数组加上结束符’\0’ 可以使用字符串来初始化字符数组,但此时要注意,每个字符串结尾会暗含一个’\0’字符,因此字符数组的长度至少要比字符串的长度多1!
# include <iostream>
using namespace std;
int main ( )
{
char a1[ ] = { 'C' , '+' , '+' } ;
char a2[ ] = { 'C' , '+' , '+' , '\0' } ;
char a3[ ] = "C++" ;
char a4[ 6 ] = "Daniel" ;
return 0 ;
}
2.1 字符数组的输入输出
# include <iostream>
using namespace std;
int main ( )
{
char str[ 100 ] ;
cin >> str;
cout << str << endl;
printf ( "%s\n" , str) ;
return 0 ;
}
# include <iostream>
using namespace std;
int main ( )
{
char str[ 100 ] ;
gets ( str) ;
cout << str << endl;
return 0 ;
2.2 字符数组的常用操作
下面几个函数需要引入头文件:#include <string.h> (1) strlen(str),求字符串的长度 (2) strcmp(a, b),比较两个字符串的大小,a < b 返回-1,a == b 返回0,a > b返回1。这里的比较方式是字典序! (3) strcpy(a, b),将字符串b复制给从a开始的字符数组
# include <iostream>
# include <string.h>
using namespace std;
int main ( )
{
char a[ 100 ] = "hello world!" , b[ 100 ] ;
cout << strlen ( a) << endl;
strcpy ( b, a) ;
cout << strcmp ( a, b) << endl;
return 0 ;
}
2.3 遍历字符数组中的字符
# include <iostream>
# include <string.h>
using namespace std;
int main ( )
{
char a[ 100 ] = "hello world!" ;
for ( int i = 0 ; i < strlen ( a) ; i++ ) cout << a[ i] << endl;
return 0 ;
}
3. 标准库类型 string
可变长的字符序列,比字符数组更加好用。需要引入头文件:#include
3.1 定义和初始化
# include <iostream>
# include <string>
using namespace std;
int main ( )
{
string s1;
string s2 = s1;
string s3 = "hiya" ;
string s4 ( 10 , 'C' ) ;
return 0 ;
}
3.2 string 上的操作
3.2.1 string的读写
# include <iostream>
# include <string>
using namespace std;
int main ( )
{
string s1, s2;
cin >> s1 >> s2;
cout << s1 << s2 << endl;
return 0 ;
}
注意:不能用printf直接输出string,需要写成:printf(“%s”, s.c_str())
3.2.2 使用getline读取一整行
# include <iostream>
# include <string>
using namespace std;
int main ( )
{
string s;
getline ( cin, s) ;
cout << s << endl;
return 0 ;
}
3.2.3 string的empty和size操作
注意size是无符号整数,因此 s.size() <= -1一定成立
# include <iostream>
# include <string>
using namespace std;
int main ( )
{
string s1, s2 = "abc" ;
cout << s1. empty ( ) << endl;
cout << s2. empty ( ) << endl;
cout << s2. size ( ) << endl;
return 0 ;
}
3.2.4 string的比较
支持 > < >= <= == !=等所有比较操作,按字典序进行比较
3.2.5 为string对象赋值
string s1 ( 10 , ‘c’) , s2;
s1 = s2;
3.2.6 两个string对象相加
string s1 = “hello, ”, s2 = “world\n”;
string s3 = s1 + s2;
s1 += s2;
3.2.7 字面值和string对象相加
做加法运算时,字面值和字符都会被转化成string对象,因此直接相加就是将这些字面值串联起来。
string s1 = “hello”, s2 = “world”;
string s3 = s1 + “, “ + s2 + ‘\n’;
当把string对象和字符字面值及字符串字面值混在一条语句中使用时,必须确保每个加法运算符的两侧的运算对象至少有一个是string。
string s4 = s1 + “, “;
string s5 = “hello” + ”, “;
string s6 = s1 + “, “ + “world”;
string s7 = “hello” + “, “ + s2;
3.3 处理string对象中的字符(重点)
可以将string对象当成字符数组来处理 char字符数组长度用strlen() string字符串长度使用size()
# include <iostream>
# include <string.h>
using namespace std;
int main ( )
{
string s = "hello world" ;
for ( int i = 0 ; i < s. size ( ) ; i ++ ) cout << s[ i] << endl;
return 0 ;
}
# include <iostream>
# include <string.h>
using namespace std;
int main ( )
{
string s = "hello world" ;
for ( char c : s) cout << c << endl;
for ( char & c : s) c = 'a' ;
cout << s << endl;
return 0 ;
}