C++字符数组、字符数组指针和string类

Virginia ·
更新时间:2024-09-20
· 1485 次阅读

目录

1、字符数组和字符数组的指针

2、标准库string类

3、补充

3.1C++自带string类的常用方法

C++中字符串的表示方式有很多种,根据自己目前掌握的有三种:

字符数组;

字符数组指针;

标准库string类;

上面几种方式各有优点和缺点,按照自己的观点,如果处理的字符串的任务比较简单,则使用前两种方法所占用内存小,因而较为实用;如果需要进行字符串拼接和比较等功能,则使用string类比较合适,因为字符数组不含有处理函数。

1、字符数组和字符数组的指针

定义字符数组即使用char类型,字符数组的声明和初始化例子如下:

char duckWords[5] = "Eat";

给字符数组定义指针的语法如下,字符数组名依旧表示首地址:

char *pointerWords = duckWords;

字符数组和字符数组指针的使用方式,和普通数组与普通数组指针的使用方式完全相同:

printf("%c \n", duckWords[5]); printf("%c \n", *pointerWords);

需要注意的是,字符串的以“\0”结尾,所以对于“Say it”这个字符串实际上含有7个字符,因为表示字符串结尾标志的”\0“是自动添加的。此外,字符串创建含有多种语法,比较重要的一点是初始化时可以不指定数组长度:

char duckName[6]={'D','a','v', 'i', 'd'}; char duckName[6]="David"; char duckName[] = "David"; 2、标准库string类

从面向对象的角度看,string类才是更符合字符串操作的。必须注意,string是一个类而不是基本数据类型。

string类的功能主要体现在下面三个发面:

含有多个构造函数,所以能采用多种方式进行初始化;

包含众多的重载操作符;

多种用于字符串处理的成员函数;

下面的第一个例子采用“+”运算符进行字符串拼接:

string duckName = "David"; string duckAge = " 12";  string duckDescribe = duckName + duckAge;  /// 需要使用string.c_str()才能输出完整字符串  printf("%s \n", duckDescribe.c_str());

第二个例子是使用string的成员函数length()进行字符串长度统计:

string duckName = "David"; printf("%d \n", duckName.length());

当然,string类重载的操作符和含有的成员函数还有很多,但是使用方法都是类似的,不属于语法范畴,所以不做具体介绍。

3、补充 3.1C++自带string类的常用方法  #include<iostream>    #include<string>    using namespace std;    int main()    {        string str1 = "hello";        string* str2 = new string("hello");       string str3 = "world";      //获取字符串长度      int length = str1.length();      cout << "调用str.length()函数获取字符串长度:" << length << endl;      cout << endl;      //字符串连接       string str4 = str1 + str3;       cout << "字符串连接结果:" << str4 << endl;       cout << endl;       //字符串比较       if (str1 < str3)           cout << "字符串比较:" << "str1<str2" << endl;       cout << endl;       //获取字符串的第一个字符       string::const_iterator it = str1.begin();       cout << *it << endl;       cout << endl;       //获取字符串的最后一个字符      it = str1.end();//end是指向最后一个字符后面的元素,而且不能输出,所以cout << *it << endl;这样输出会报错       it--;       cout << *it << endl;       cout << endl;     //倒置串      reverse(str1.begin(), str1.end());    cout << "倒置串:" << str1 << endl;     cout << endl;     //字符串转字符数组      //不推荐的用法,但是需要了解     string a = "abc123";      const char *b;//这里必须为const char *,不能用char *,不然下一句会报错      b = a.c_str();      cout << "a:" << a << endl;      cout << "b:" << b << endl;      a = "asd456";      cout << "a:" << a << endl;      cout << "b:" << b << endl;       //推荐用法       string c = "abc123";      char *d = new char[20];       strcpy(d, c.c_str());//因为这里没有直接赋值,所以指针类型可以不用const char *     cout << "c:" << c << endl;       cout << "d:" << d << endl;      c = "asd456";       cout << "c:" << c << endl;     cout << "d:" << d << endl;     cout << endl;       //查找串      //find-从指定位置起向后查找,直到串尾       string st1("babbabab");     cout << st1.find('a') << endl;//1,默认从位置0(即第1个字符)开始查找      cout << st1.find('a', 2) << endl;//4   在st1中,从位置2(b,包括位置2)开始,查找a,返回首次匹配的位置      cout << (st1.find('c', 0) == -1) << endl;//1        cout << (st1.find('c', 0) == 4294967295) << endl;//1   两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制)      string st2("aabcbcabcbabcc");      str1 = "abc";      cout << st2.find(str1, 2) << endl;//6,从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回-1       cout << st2.find("abcdefg", 2, 3) << endl;//6   取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2)       //rfind-从指定位置起向前查找,直到串首      cout << st1.rfind('a', 7) << endl;//6      //find_first_of-在源串中从位置pos起往后查找,只要在源串中遇到一个字符,该字符与目标串中任意一个字符相同,就停止查找,返回该字符在源串中的位置;若匹配失败,返回-1       string str6("bcgjhikl");      string str7("kghlj");      cout << str6.find_first_of(str7, 0) << endl;//2,从str1的第0个字符b开始找,g与str2中的g匹配,停止查找,返回g在str1中的位置2      //find_last_of-与find_first_of函数相似,只不过查找顺序是从指定位置向前      string str("abcdecg");      cout << str.find_last_of("hjlywkcipn", 6) << endl;//5,从str的位置6(g)开始向前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5      //find_first_not_of-在源串中从位置pos开始往后查找,只要在源串遇到一个字符,与目标串中的任意字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满足条件的字符,则返回-1      cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3   从源串str的位置0(a)开始查找,目标串中有a,匹配,..,找d,目标串中没有d(不匹配),停止查找,返回d在str中的位置3       //find_last_not_of-与find_first_not_of相似,只不过查找顺序是从指定位置向前      cout << str.find_last_not_of("kiajbvehfgmlc", 6) << endl;//3      system("pause");      return 0;  }

 运行结果:

到此这篇关于C++字符数组、字符数组指针和string类的文章就介绍到这了,更多相关C++字符数组和string类内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



数组指针 c+ 字符数组 string类 C++ 指针 数组 字符 string

需要 登录 后方可回复, 如果你还没有账号请 注册新账号