您的位置:首页 > 编程语言 > C语言/C++

C风格字符串[C++]

2011-01-04 11:12 330 查看
1.字符串字面值
  字符串字面值是一串常量字符,字符串字面值常量用双引号括起来的零个或多个字符表示,为兼容C语言,C++中所有的字符串字面值都由编译器自动在末尾添加一个空字符
 
"Hello World!" //simple string literal
 
""             //empty string literal
 
"/nCC/toptions/tfile.[cC]/n"  //string literal
using newlines and tabs

  字符字面值: 'A'  //single quoto:character literal
  字符串字面值: "A" 
//double quote:character string
literal.包含字母A和空字符的字符串

  字符串字面值的连接:
  std::out << "a
multi-line "
              "string literal"
              " using
concatenation"
           << std::endl;
  输出:a multi-line string literal
using concatenation

  多行字面值:
  std::out << "a
multi-line  /
   string literal/
using a backslash"
           <<
std::endl;
  输出:a
multi-line       string literalusing a
backslash

2.C风格字符串
   字符串字面值的类型实质是const
char类型的数组。C++从C语言继承下来的一种通用结构是C风格字符串,而字符串字面值就是该类型的实例。C风格字符串是以空字符null结束的字符数组:
  
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结束的数组。
  
2.1 C风格字符串的使用
       C++语言通过(const) char *类型的指针来操纵C风格字符串。
       const
char *cp = "some value";  //
一个C风格字符串
       while(*cp) //判断cp当前指向的字符是true还是false,true表明这是除null外的任意字符
       {
           
// do something to *cp
           
++cp;
       }
   2.2 C风格字符串的标准库函数
       #include <cstring> 
//
cstring是string.h头文件中的C++版本,而string.h是C语言提供的标准库
       操纵C风格字符串的标准库函数:
      
strlen(s)           //
返回s的长度,不包括字符串结束符null
       strcmp(s1, s2)
       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
   2.3
永远不要忘记字符串结束符null
       char ca[]={'C', '+', '+'};  // not null-terminated
       cout <<
strlen(ca) << endl; // disaster: ca isn't
null-terminated
   2.4 调用者必须确保目标字符串具有足够的大小
       // 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;
   2.5 使用strn函数处理C风格字符串
       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
   2.6 尽可能使用标准库类型string
       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
      
此时,标准库负责处理所有的内在管理问题。

 

 

转自:http://www.cnblogs.com/JCSU/articles/1027654.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息