源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

c字符串,string对象,字符串字面值的区别详解

  • 时间:2021-10-19 19:02 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:c字符串,string对象,字符串字面值的区别详解
[b]一、字符串字面值 [/b]字符串字面值是一串常量字符,字符串字面值常量用双引号括起来的零个或多个字符表示,为兼容C语言,C++中所有的字符串字面值都由编译器自动在末尾添加一个空字符。 字符串没有变量名字,自身表示自身
[u]复制代码[/u] 代码如下:
"Hello World!" //simple string literal "" //empty string literal "\nCC\toptions\tfile.[cC]\n" //string literal using newlines and tabs
字符字面值: 'A' //single quote:character literal 字符串字面值: "A" //double quote:character string literal.包含字母A和空字符的字符串 [b]字符串字面值的连接: [/b]
[u]复制代码[/u] 代码如下:
std::out << "a multi-line "+ "string literal"+ " using concatenation" << std::endl;
输出:a multi-line string literal using concatenation [b]多行字面值: [/b]
[u]复制代码[/u] 代码如下:
std::out << "a multi-line \n string literal\n using a backslash" << std::endl;
输出:a multi-line string literalusing a backslash ================================================= 1. string literal:字符串直接量:
[u]复制代码[/u] 代码如下:
cout<<"hello"<<endl;
代码中通过包含"hello"字符串自身来将其输出,并未包含该字符串的变量。 2. 字符串直接量可以赋值给变量,但是与字符串直接量相关联的内存空间位于只读部分,因此它是常量字符数组。
[u]复制代码[/u] 代码如下:
char* ptr = "hello"; ptr[1] = 'a';//crash! attemps to write to read-only memory.
因此,当引用字符串直接量的时候使用指向const的字符数组:
[u]复制代码[/u] 代码如下:
const char* ptr = "hello"; ptr[1] = 'a';//bug! attempts to write to read-only memory.
3. 当将字符串直接量赋值给字符数组的初始值的时候。由于字符数组存放与栈中,不允许引用其他地方的内存,因此编译器会将字符串直接量复制到站的数组内存中。因此,可以进行相应的修改。
[u]复制代码[/u] 代码如下:
char stackArray[] = "hello"; stackArray[1] = 'a';
[b]二、C++风格字符串 [/b]C++风格字符串:使用C++风格字符串的时候,要将它当做是一个普通的类型,如int,这样反而会避免将string作为一个类来理解所带来的很多问题。 1. 支持<cstring>中许多函数完成的同样操作。 2. 字符串定义:
[u]复制代码[/u] 代码如下:
string myString = “hello”;
3. 操作符 = :复制字符串;比如:
[u]复制代码[/u] 代码如下:
string newone = original;
会将后者复制给前者,不会出现两个变量同样指向一个内存的情况。 4. 可以像int一样使用 == 之类的操作符 5. 可以改变字符串中的某一个字符。 如
[u]复制代码[/u] 代码如下:
string myString = "hello"; mystring[0] = 'l';
这中操作是允许的。 [b]2.1 C风格字符串的使用 [/b]C++语言通过(const) char *类型的指针来操纵C风格字符串。
[u]复制代码[/u] 代码如下:
#include <cstring> // cstring是string.h头文件中的C++版本,而string.h是C语言提供的标准库 //操纵C风格字符串的标准库函数(参数类型省略,都是char *类型): strlen(s) // 返回s的长度,不包括字符串结束符null strcmp(s1, s2) //当s1<s2时,返回值<0 ,当s1=s2时,返回值=0 ,当s1>s2时,返回值>0 strcat(s1, s2) // 将字符串s2连接到s1后,并返回s1 strcpy(s1, s2) // 将s2复制给s1,并返回s1 strncat(s1, s2, n) // 将s2的前n个字符连接到s1后面,并返回s1 strncpy(s1, s2, n) // 将s2的前n个字符复制给s1,并返回s1 if(cp1 < cp2) // compares address, not the values pointed to const char *cp1 = "A string example"; const char *cp2 = "A different string"; int i=strcmp(cp1, cp2); // i is positive i=strcmp(cp2, cp1); // i is negative i=strcmp(cp1, cp1); // i is zero
[b]2.3 永远不要忘记字符串结束符null [/b]
[u]复制代码[/u] 代码如下:
char ca[]={'C', '+', '+'}; // not null-terminated cout << strlen(ca) << endl; // disaster: ca isn't null-terminated
[b]2.4 调用者必须确保目标字符串具有足够的大小 [/b]
[u]复制代码[/u] 代码如下:
// Dangerous:What happens if we miscalculate the size of largeStr? char largeStr[16+18+2]; // will hold cp1 a space and cp2 strcpy(largeStr, cp1); // copies cp1 into largeStr strcat(largeStr, " "); // adds a space at end of largeStr strcat(largeStr, cp2); // concatenates cp2 to largeStr // prints A string example A different string cout << largeStr << endl;
[b]2.5 使用strn函数处理C风格字符串 [/b]
[u]复制代码[/u] 代码如下:
char largeStr[16+18+2] // to hold cp1 a space and cp2 strncpy(largeStr, cp1, 17); // size to copy includes the null strncat(largeStr, " ", 2); // pedantic, but a good habit strncat(largeStr, cp2, 19); // adds at most 18 characters, plus a null
[b]2.6 尽可能使用标准库类型string [/b]
[u]复制代码[/u] 代码如下:
string largeStr = cp1; // initialize largeStr as a copy of cp1 largeStr += " "; // add space at end of largeStr largeStr += cp2; // concatenate cp2 onto end of largeStr
此时,标准库负责处理所有的内在管理问题。 [b]三、C风格字符串 [/b]字符串字面值的类型实质是const char类型的数组。C++从C语言继承下来的一种通用结构是C风格字符串,而字符串字面值就是该类型的实例。C风格字符串是以空字符null结束的字符数组:
[u]复制代码[/u] 代码如下:
char ca1[]={'C', '+', '+'}; // no null, not C-style string char ca2[]={'C', '+', '+', '\0'}; // explicit null char ca3[]="C++"; // null terminator added automatically const char *cp="C++"; // null terminator added automatically char *cp1=ca1; // points to first element of a array, but not C-style string char *cp2=ca2; // points to first element of a null-terminated char array
ca1和cp1都不是C风格字符串:ca1是一个不带结束符null的字符数组,而指针cp1指向ca1,因此,它指向的并不是以null结束的数组。 [b]字符串的连接: 1.c++中string可以替代c中的char数组且前者用起来更方便[/b]。连接两个string对象只需用'+';c字符串是用char数组实现的。以下都称c字符串为char数组 例如:
[u]复制代码[/u] 代码如下:
string s1="hello",s2="world";           string s3=s1+s2;                  //也可以s3=s1+"world" cout<<s3<<endl;//结果为helloworld
当然还可以用+=连接。 [b]2.还可以这样连接一个string对象和char数组。[/b] 例如:
[u]复制代码[/u] 代码如下:
string s1="hello";           char s2[]="world"; cout<<s1+s2<<endl;//输出结果为helloworld
但不能这样连接两个char数组或字符字面值。 例如:
[u]复制代码[/u] 代码如下:
string s1="hello";            string s2="world";            string s3=s1+"world";//正确,可以连接一个string对象和字符串字面值            string s4="hello"+"world";//错误,不能这样连接连个字符串字面值            char s5[]="world";            string s6=s1+s5;//正确,可以连接一个string对象和char数组            char s7[]="hello";            stirng s8=s7+s5;//错误,不能这样连接两个char数组。
总而言之只能用+或+=连接两个string对象或一个string对象和字符串字面值或一个string对象和char数组。 连接一个string对象和字符串字面值或char数组或返回的都是string对象,所以可以连接一个string对象和字符串字面值(或char数组)后再连接一个字符串字面值(或char数组)。 例如:
[u]复制代码[/u] 代码如下:
string s;//初始化为空 char s1[]="hello"; char s2[]="world"; s=s+s1+s2;//正确
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部