c++ 字符串

C++ 字符串

 

 

C++ 提供了以下两种类型的字符串表示形式:

  • C 风格字符串
  • C++ 引入的 string 类类型

C 风格字符串

以字符’\0‘结尾的字符数组就是字符串,为什么会有这个东西呢?因为在处理处理一个字符数组时就好像是在处理一段字符,但是我们以什么来判断这段字符结束了呢,这个标志就是’\0‘,所以我们就说这种字符数组就叫字符串。

char g_caTest1[3]={'1','2','3'};//字符数组
char g_szTest[3]={'1','2','\0'};//字符串


像上述代码所示这两个都是char数组  但是c风字符串的特点 是以空字符结尾(\0),不是以空字符结尾的连续字符就不是一个字符串而是一个字符数组。为什么要这样区分呢?因为在c++中有很多函数如cout,他们时逐个处理字符直到空字符,因此我用字符串传递给他们 而不是让他们自己寻找空字符取结尾

 

 

对于上面输入是不是很烦,所以你可以这样输入.

 

char g_szTest[]="123";//"123"就是字符串常量,我们可以把一个字符串常量赋值给一个字符数组,因为字符串本身就是一个加'\0'的数组,所以这个数组的长度要加上1,为4.

 

双引号括起来的字符串称为字符串常量或是字符串字面量 ,一般字符串的长度是 已知长度加一(空字符)。如果数组很大但是填充的内容很小时多余的空位都会被填充为\0。

 

 

tips:c++很多函数都可以传递一个 指向字符串的指针(地址),来处理字符串,原因就是可以通过’\0‘来判断结尾。

 

tips:

char c [3]="123";//error


上面错误是因为,首先我们用双引号表明这个时数组长度为4(3+1),但是我们的数组就3个位子 ,所以错误

 

字符串会自动拼接

任意有空白(空格,制表,换行符)分隔的字符串常量都会被自动拼接为一个,拼接会删除前面字符串中的\0

 

cout<<"cout"
"cout"<<enl;

会输出

coutcout

 

其实也也可以怎么写
 
cout<<"cout\
"cout"<<enl
会这样是因为\是有特殊含义的,还记得转义字符吗。\告诉编译器我要换行来了,但是没写完。想输出\,请参考转义字符专题
 

char*[],char[],和字符串

char *a="123";
char a[]="123";
const char *a="123"
char (*p1)[]="dingwen";//[Error] cannot convert 'const char*' to 'char (*)[]' in initialization

上述代码都是ok的但是第三个,比第一个更严谨 。为什么可以这样呢

 

第一个可以是因为字符串表达式的值为一个指针(这个指针存储着字符串第一个元素的字母地址)所以可以把他赋值个指针,

第二个是因为字符串本身就是一个数组所以可初始化,

第三个可以根据第四个的错误可以了解符串表达式的值为const char*

另外有一个blog讲的很清楚 可以给大家参考)点击打开链接(

 

 

 

C++ 中有大量的函数用来操作以 null 结尾的字符串:

 

序号函数 & 目的
1strcpy(s1, s2);复制字符串 s2 到字符串 s1。
2strcat(s1, s2);连接字符串 s2 到字符串 s1 的末尾。
3strlen(s1);返回字符串 s1 的长度。
4strcmp(s1, s2);如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回小于 0;如果 s1>s2 则返回大于 0。
5strchr(s1, ch);返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。
6strstr(s1, s2);返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。

下面的实例使用了上述的一些函数:

#include <iostream>
#include <cstring>

using namespace std;

int main ()
{
   char str1[10] = "Hello";
   char str2[10] = "World";
   char str3[10];
   int  len ;

   // 复制 str1 到 str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;

   // 连接 str1 和 str2
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;

   // 连接后,str1 的总长度
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;

   return 0;
} <iostream>
#include <cstring>

using namespace std;

int main ()
{
   char str1[10] = "Hello";
   char str2[10] = "World";
   char str3[10];
   int  len ;

   // 复制 str1 到 str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;

   // 连接 str1 和 str2
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;

   // 连接后,str1 的总长度
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;

   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10

C++ 中的 String 类

C++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能。我们将学习 C++ 标准库中的这个类,现在让我们先来看看下面这个实例:

现在您可能还无法透彻地理解这个实例,因为到目前为止我们还没有讨论类和对象。所以现在您可以只是粗略地看下这个实例,等理解了面向对象的概念之后再回头来理解这个实例。

#include <iostream>
#include <string>

using namespace std;

int main ()
{
   string str1 = "Hello";
   string str2 = "World";
   string str3;
   int  len ;

   // 复制 str1 到 str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;

   // 连接 str1 和 str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;

   // 连接后,str3 的总长度
   len = str3.size();
   cout << "str3.size() :  " << len << endl;

   return 0;
} <iostream>
#include <string>

using namespace std;

int main ()
{
   string str1 = "Hello";
   string str2 = "World";
   string str3;
   int  len ;

   // 复制 str1 到 str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;

   // 连接 str1 和 str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;

   // 连接后,str3 的总长度
   len = str3.size();
   cout << "str3.size() :  " << len << endl;

   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

 

str3 : Hello
str1 + str2 : HelloWorld
str3.size() :  10: Hello
str1 + str2 : HelloWorld
str3.size() :  10

 

从上面克以知道string 的大体用法,总结以下

 

 

 

 

1、使用#include<string>

2、名称空间std中

3、可以用c风格字符串初始化

4、可以用cout,cin,输出输入string

5、还可以数组数组表示发访问string(重载符号

写出代码如下

 

char a[20];
char b[20]="345"
string a1;
sting a2="345"
cin<<a<<a1;
cout<<b[1]<<b1[1];

 

 

那到底string接受什么类型呢?下面代码编译器都没有报错

 

 

const char *p="*p";
char arraychar[]="arraycohar[]";
char *pp=arraychar;//数组名为第一个元素的地址
string s1=arraychar;
string s2="字符串";
string s3=p;
string s4=pp;

 

 

 

综上string可以被赋值为 char*,char[]类型的字符串常量,数组名, (其实这些都是char*,下面链接中展示了string重载了=就是接受char*)

还有一点就是string中重定义了很多运算符  比如+(链接连个字符串,并生成一个新的字符串)。这样就不用调用strcpy()函数来拼接两个字符串了

详细的string 有一个链接个大家点击打开链接

 

其他形式的字符串字面值

wchar_t  w  []=L"this is wchar_t";
char16_t c_16 []=u"this is char16_t";
char32_t c_32 []=U"this is chat32_t";  
上面我们在基本类型的时候提到了,但是因为本人dev c++5.11不支持c++11的 char16_t,和char32_t所以一直是报错的,请大家注意。
 
c++11还提供一种原始字符串,在原始字符串中字符表示的就是他自己,如\n 和\"就是\n \",不是转义字符 .c++11使用"()"和前缀R,下面等价
 
cout<<R"(this is the "raw" string "\n")"<<'\n';
 cout<<"\"(this is the \"raw\" string \"\\n\""<<'\n'
 
但是如果想使用)"呢怎么班,其实c++允许在“(之间加上任意字符如下
 
 cout<<R"+*(this is the "raw" string "\n")+*"<<'\n';
 
 
除了上面那些其实还可以制定字符类型wchar_t char16_t char32_t
 
 cout<<RL"+*(this is the "raw" string "\n")+*"<<'\n';
 cout<<Ru"+*(this is the "raw" string "\n")+*"<<'\n';
 cout<<UR"+*(this is the "raw" string "\n")+*"<<'\n';
 
但是非常遗憾,还是那句话本人的dev c++5.11不支持这个原始字符串

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值