zoukankan      html  css  js  c++  java
  • C++:char数组和string类


    参考文章:https://www.runoob.com/cplusplus/cpp-strings.html

    概述

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

    C 风格字符串

    定义

    C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持。字符串实际上是使用 null 字符 '' 终止的一维字符数组。因此,一个以 null 结尾的字符串,包含了组成字符串的字符。

    下面的声明和初始化创建了一个 "Hello" 字符串。由于在数组的末尾存储了空字符,所以字符数组的大小比单词 "Hello" 的字符数多一个。

    char greeting[6] = {'H', 'e', 'l', 'l', 'o', ''};
    

    依据数组初始化规则,您可以把上面的语句写成以下语句:

    char greeting[] = "Hello";
    

    字符串操作函数

    代码示例

    #include "stdafx.h"
    #include "stdlib.h"
    #include <string>
    #pragma warning( disable : 4996) 
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char str1[11] = "Hello";
    	char str2[11] = "World";
    	char str3[11];
    	int  len;
    
    	// 复制 str1 到 str3
    	strcpy(str3, str1);
    	cout << "strcpy( str3, str1) : " << str3 << endl;  // Hello
    
    	// 连接 str1 和 str2
    	strcat(str1, str2);
    	cout << "strcat( str1, str2): " << str1 << endl;   // HelloWorld
    
    	// 连接后,str1 的总长度
    	len = strlen(str1);
    	cout << "strlen(str1) : " << len << endl;    // 10
    	system("pause");
    	return 0;
    }
    

    C++ 风格字符串

    C++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能。

    基本操作

    字符串复制:“=”
    字符串拼接:“+”

    代码示例

    #include "stdafx.h"
    #include "stdlib.h"
    #include <string>
    #pragma warning( disable : 4996) 
    #include <iostream>
    
    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;
    	system("pause");
    	return 0;
    }
    

    获得字符串的长度

    对于char数组,可以使用sizeof()和strlen()函数;对于string类,可以使用str.size()和str.length()函数

    #include "stdafx.h"
    #include "stdlib.h"
    #include <string>
    #pragma warning( disable : 4996) 
    using namespace std;
    
    int main()
    {
    	int size;
    	const char* str1 = "0123456789";
    	const char str2[] = "0123456789";
    	string str3 = "0123456789";
    
    	size = sizeof(str1);
    	printf("str1 的长度为:%d
    ", size);  // 打印指针的长度  4
    	size = sizeof(str2);
    	printf("str2 的长度为:%d
    ", size); // 打印字符数组的长度,包括字符串结束字符"/0"   11
    	size = strlen(str2);
    	printf("str2 的长度为:%d
    ", size); // 打印字符数组的长度,不包括字符串结束字符   10
    
    	size = str3.length();
    	printf("str3 的长度为:%d
    ", size); // 打印字符串的长度,不包括字符串结束字符   10
    	size = str3.size();
    	printf("str3 的长度为:%d
    ", size); // 打印字符串的长度,不包括字符串结束字符   10
    
    	system("pause");
            return 0;
    }
    
  • 相关阅读:
    启动ASM 数据库的步骤
    ora15031 DBCA找不到ASM磁盘组
    ORA12514 解决方法
    Rhel Linux 5.1 (32 位)上安装 Oracle ASM数据库 11g 第 1 版
    Oracle ASM for linx as 4 x8664 创建过程
    在安腾AI64 RHEL3 U3服务器上安装ORACLE10G
    ora12528 : message 12528 not found; product=RDBMS ; facility=ora
    linux下启动oracle服务和监听程序
    linux内核升级RPM包安装问题!!!!
    AS4 下安装Oracle 10g(ASM & RAW)
  • 原文地址:https://www.cnblogs.com/wt-seu/p/12831259.html
Copyright © 2011-2022 走看看