C++字符串

  1. 字符串与整数
    每个常用字符都对应一个-128~127的数字,二者之间可以相互转化。
    注意:目前负数没有与之对应的字符
    字符是char
#include <iostream>

using namespace std;

int main(){
	char c = 'a';
	cout << (int)c <<endl;
	int x = 66;
	cout << (char)x << 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 = 'A' * 'B';
    char c = 'A'+2;
    int d = '0'+1;
    cout << a << b << c << d <<endl;
}

2.字符数组
字符串就是字符数组加上结束符’\0’。
可以使用字符串来初始化字符数组,但此时要注意,每个字符串结尾会暗含一个’\0’ 字符,因此字符数组的长度至少要比字符串的长度多1。

  • 2.1字符数组的输入输出
#include <iostream>

using namespace std;

int main(){
    char str[100];
    cin >> str;//输入字符串时,遇到空格或者回车就会停止
    cout << str << endl;//输出字符串时,遇到空格或者回车不会停止,遇到'\0'时停止。
    printf("%s\n",str);
    return 0;
}

读入一行字符串,包括空格:

#include <iostream>

using namespace std;

int main(){
    char str[100];
    fgets(str,100,stdin);
    cout << str << endl;

    return 0;
}
  • 字符数组的常用操作
    下面几个函数需要引入头文件:
#include <string.h>
strlen(str)求字符串长度
strcmp(a,b)比较两个字符串的大小,a<b返回-1,a==b返回0,a>b返回1。这里的比较方式是字典序
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;
}
  • 遍历字符数组中的字符
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    char a[100] = "hello world!";

    // 注意:下述for循环每次均会执行strlen(a),运行效率较低,最好将strlen(a)用一个变量存下来
    for (int i = 0; i < strlen(a); i ++ )
        cout << a[i] << endl;

    return 0;
}
  1. 标准库类型string
    可变长的字符序列,比字符数组更加好用。需要引入头文件:
#include <string>
  • 定义和初始化
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1;              // 默认初始化,s1是一个空字符串
    string s2 = s1;         // s2是s1的副本,注意s2只是与s1的值相同,并不指向同一段地址
    string s3 = "hiya";     // s3是该字符串字面值的副本
    string s4(10, 'c');     // s4的内容是 "cccccccccc"

    return 0;
}
  • string上的操作
    (1)string的读写:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1, s2;

    cin >> s1 >> s2;
    cout << s1 << s2 << endl;

    return 0;
}

注意:不能用printf直接printf直接输出string,需要写成:

printf(%s”, s.c_str()
  • 使用getline读取一整行
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s;

    getline(cin, s);

    cout << s << endl;

    return 0;
}
  • 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;
}
  • string的比较
    支持 >, <, >=, <=, ==, !=等所有比较操作,按字典序进行比较。
  • 为string对象赋值
string s1(10, 'c'), s2;     // s1的内容是 cccccccccc;s2是一个空字符串
s1 = s2;                    // 赋值:用s2的副本替换s1的副本
                            // 此时s1和s2都是空字符串
  • 两个string对象相加
string s1 = "hello,  "", s2 = "world\n";
string s3 = s1 + s2;                    // s3的内容是 hello, world\n
s1 += s2;                               // s1 = s1 + s2
  • 字面值和string对象相加:
做加法运算时,字面值和字符都会被转化成string对象,因此直接相加就是将这些字面值串联起来:

string s1 = "hello", s2 = "world";      // 在s1和s2中都没有标点符号
string s3 = s1 + ", " + s2 + '\n';
当把string对象和字符字面值及字符串字面值混在一条语句中使用时,必须确保每个加法运算符的两侧的运算对象至少有一个是string:

string s4 = s1 + ", ";  // 正确:把一个string对象和有一个字面值相加
string s5 = "hello" + ", "; // 错误:两个运算对象都不是string

string s6 = s1 + ", " + "world";  // 正确,每个加法运算都有一个运算符是string
string s7 = "hello" + ", " + s2;  // 错误:不能把字面值直接相加,运算是从左到右进行的

  • 处理string对象中的字符
可以将string对象当成字符数组来处理:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s = "hello world";

    for (int i = 0; i < s.size(); i ++ )
        cout << s[i] << endl;

    return 0;
}
或者使用基于范围的for语句:

#include <iostream>
#include <string>

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;
}
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值