C++标准库string类型
来源:互联网 发布:js 字符串转date 编辑:程序博客网 时间:2024/06/14 16:25
string类型支持长度可变的字符串,C++标准库将负责管理与存储与字符串相关的类容,以及提供各种有用的操作。标准库string类型的目的就是满足对字符串的一般应用。
包含头文件
#include<string>
1.string对象的定义与初始化
string s1; 默认构造函数string s2(s1); 将s2初始化为s1的一个副本string s3("value") 将s3初始化为一个字符串字面值副本string s4(n,'c') 将s4初始化为字符'c'的n个副本
2.string对象的读写
写
cout<<s<<endl;
读
cin>>s;
注意:读取忽略开头的所有空白字符(如空格,换行符,制表符)
读取字符直至再次遇到空白字符,读取终止。
如:输入"Hello World!",实际上读取到了"Hello"。
3.string对象的操作
s.empty() 如果s为空串,则返回true,否则返回falses.size() 返回s中的字符个数s[n] 返回s中位置为n的字符,位置从0开始计数s1+s2 把s1和s2连接成一个新的字符串,返回新生成的字符串s1=s2 把s1的内容替换成s2的副本s1==s2 比较s1和s2的内容,相等返回true,否则返回false!=,<,<=,>,>= 保持这些操作符贯有的含义
注意:
1.s.size()返回的类型是string::size_type。
2.比较string实际上是比较string的字符。
3.string对象相加,可以是string对象与string对象相加,string对象与字符串字面值相加,不允许字符串字面值相与字符串字面值相加。
"Hello"+" World" //error
4.字符串对象中的字符处理
我们经常要对string对象中的单个字符进行处理,例如,通常要知道某个特殊字符是否为空白字符,字母或数字。在cctype头文件中定义了对string的操作。下面是ctype.h头文件
/****ctype.h - character conversion macros and ctype macros** Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.**Purpose:* Defines macros for character classification/conversion.* [ANSI/System V]** [Public]*****/#if _MSC_VER > 1000#pragma once#endif#ifndef _INC_CTYPE#define _INC_CTYPE#if !defined(_WIN32) && !defined(_MAC)#error ERROR: Only Mac or Win32 targets supported!#endif#ifdef __cplusplusextern "C" {#endif/* Define _CRTIMP */#ifndef _CRTIMP#ifdef _DLL#define _CRTIMP __declspec(dllimport)#else /* ndef _DLL */#define _CRTIMP#endif /* _DLL */#endif /* _CRTIMP *//* Define __cdecl for non-Microsoft compilers */#if ( !defined(_MSC_VER) && !defined(__cdecl) )#define __cdecl#endif/* Define _CRTAPI1 (for compatibility with the NT SDK) */#ifndef _CRTAPI1#if_MSC_VER >= 800 && _M_IX86 >= 300#define _CRTAPI1 __cdecl#else#define _CRTAPI1#endif#endif#ifndef _WCHAR_T_DEFINEDtypedef unsigned short wchar_t;#define _WCHAR_T_DEFINED#endif#ifndef _MAC#ifndef _WCTYPE_T_DEFINEDtypedef wchar_t wint_t;typedef wchar_t wctype_t;#define _WCTYPE_T_DEFINED#endif#ifndef WEOF#define WEOF (wint_t)(0xFFFF)#endif#endif /* ndef _MAC */#ifndef _CTYPE_DISABLE_MACROS_CRTIMP extern unsigned short _ctype[];_CRTIMP extern unsigned short *_pctype;#ifndef _MAC_CRTIMP extern wctype_t *_pwctype;#endif /* ndef _MAC */#endif /* _CTYPE_DISABLE_MACROS *//* set bit masks for the possible character types */#define _UPPER 0x1 /* upper case letter */#define _LOWER 0x2 /* lower case letter */#define _DIGIT 0x4 /* digit[0-9] */#define _SPACE 0x8 /* tab, carriage return, newline, */ /* vertical tab or form feed */#define _PUNCT 0x10 /* punctuation character */#define _CONTROL 0x20 /* control character */#define _BLANK 0x40 /* space char */#define _HEX 0x80 /* hexadecimal digit */#define _LEADBYTE 0x8000 /* multibyte leadbyte */#define _ALPHA (0x0100|_UPPER|_LOWER) /* alphabetic character *//* character classification function prototypes */#ifndef _CTYPE_DEFINED_CRTIMP int __cdecl _isctype(int, int);_CRTIMP int __cdecl isalpha(int);_CRTIMP int __cdecl isupper(int);_CRTIMP int __cdecl islower(int);_CRTIMP int __cdecl isdigit(int);_CRTIMP int __cdecl isxdigit(int);_CRTIMP int __cdecl isspace(int);_CRTIMP int __cdecl ispunct(int);_CRTIMP int __cdecl isalnum(int);_CRTIMP int __cdecl isprint(int);_CRTIMP int __cdecl isgraph(int);_CRTIMP int __cdecl iscntrl(int);_CRTIMP int __cdecl toupper(int);_CRTIMP int __cdecl tolower(int);_CRTIMP int __cdecl _tolower(int);_CRTIMP int __cdecl _toupper(int);_CRTIMP int __cdecl __isascii(int);_CRTIMP int __cdecl __toascii(int);_CRTIMP int __cdecl __iscsymf(int);_CRTIMP int __cdecl __iscsym(int);#define _CTYPE_DEFINED#endif#ifndef _MAC#ifndef _WCTYPE_DEFINED/* wide function prototypes, also declared in wchar.h *//* character classification function prototypes */_CRTIMP int __cdecl iswalpha(wint_t);_CRTIMP int __cdecl iswupper(wint_t);_CRTIMP int __cdecl iswlower(wint_t);_CRTIMP int __cdecl iswdigit(wint_t);_CRTIMP int __cdecl iswxdigit(wint_t);_CRTIMP int __cdecl iswspace(wint_t);_CRTIMP int __cdecl iswpunct(wint_t);_CRTIMP int __cdecl iswalnum(wint_t);_CRTIMP int __cdecl iswprint(wint_t);_CRTIMP int __cdecl iswgraph(wint_t);_CRTIMP int __cdecl iswcntrl(wint_t);_CRTIMP int __cdecl iswascii(wint_t);_CRTIMP int __cdecl isleadbyte(int);_CRTIMP wchar_t __cdecl towupper(wchar_t);_CRTIMP wchar_t __cdecl towlower(wchar_t);_CRTIMP int __cdecl iswctype(wint_t, wctype_t);/* --------- The following functions are OBSOLETE --------- */_CRTIMP int __cdecl is_wctype(wint_t, wctype_t);/* --------- The preceding functions are OBSOLETE --------- */#define _WCTYPE_DEFINED#endif#endif /* ndef _MAC *//* the character classification macro definitions */#ifndef _CTYPE_DISABLE_MACROS/* * Maximum number of bytes in multi-byte character in the current locale * (also defined in stdlib.h). */#ifndef MB_CUR_MAX#define MB_CUR_MAX __mb_cur_max_CRTIMP extern int __mb_cur_max;#endif /* MB_CUR_MAX */#ifdef _MAC#ifndef __cplusplus#define isalpha(_c) ( _pctype[_c] & (_UPPER|_LOWER) )#define isupper(_c) ( _pctype[_c] & _UPPER )#define islower(_c) ( _pctype[_c] & _LOWER )#define isdigit(_c) ( _pctype[_c] & _DIGIT )#define isxdigit(_c) ( _pctype[_c] & _HEX )#define isspace(_c) ( _pctype[_c] & _SPACE )#define ispunct(_c) ( _pctype[_c] & _PUNCT )#define isalnum(_c) ( _pctype[_c] & (_UPPER|_LOWER|_DIGIT) )#define isprint(_c) ( _pctype[_c] & (_BLANK|_PUNCT|_UPPER|_LOWER|_DIGIT) )#define isgraph(_c) ( _pctype[_c] & (_PUNCT|_UPPER|_LOWER|_DIGIT) )#define iscntrl(_c) ( _pctype[_c] & _CONTROL )#elif 0 /* Pending ANSI C++ integration */inline int isalpha(int _C) {return (_pctype[_C] & (_UPPER|_LOWER)); }inline int isupper(int _C) {return (_pctype[_C] & _UPPER); }inline int islower(int _C) {return (_pctype[_C] & _LOWER); }inline int isdigit(int _C) {return (_pctype[_C] & _DIGIT); }inline int isxdigit(int _C) {return (_pctype[_C] & _HEX); }inline int isspace(int _C) {return (_pctype[_C] & _SPACE); }inline int ispunct(int _C) {return (_pctype[_C] & _PUNCT); }inline int isalnum(int _C) {return (_pctype[_C] & (_UPPER|_LOWER|_DIGIT)); }inline int isprint(int _C) {return (_pctype[_C] & (_BLANK|_PUNCT|_UPPER|_LOWER|_DIGIT)); }inline int isgraph(int _C) {return (_pctype[_C] & (_PUNCT|_UPPER|_LOWER|_DIGIT)); }inline int iscntrl(int _C) {return (_pctype[_C] & _CONTROL); }#endif /* __cplusplus */#else /* ndef _MAC */#ifndef __cplusplus#define isalpha(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_ALPHA) : _pctype[_c] & _ALPHA)#define isupper(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_UPPER) : _pctype[_c] & _UPPER)#define islower(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_LOWER) : _pctype[_c] & _LOWER)#define isdigit(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_DIGIT) : _pctype[_c] & _DIGIT)#define isxdigit(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_HEX) : _pctype[_c] & _HEX)#define isspace(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_SPACE) : _pctype[_c] & _SPACE)#define ispunct(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_PUNCT) : _pctype[_c] & _PUNCT)#define isalnum(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_ALPHA|_DIGIT) : _pctype[_c] & (_ALPHA|_DIGIT))#define isprint(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_BLANK|_PUNCT|_ALPHA|_DIGIT) : _pctype[_c] & (_BLANK|_PUNCT|_ALPHA|_DIGIT))#define isgraph(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_PUNCT|_ALPHA|_DIGIT) : _pctype[_c] & (_PUNCT|_ALPHA|_DIGIT))#define iscntrl(_c) (MB_CUR_MAX > 1 ? _isctype(_c,_CONTROL) : _pctype[_c] & _CONTROL)#elif 0 /* Pending ANSI C++ integration */inline int isalpha(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_ALPHA) : _pctype[_C] & _ALPHA); }inline int isupper(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_UPPER) : _pctype[_C] & _UPPER); }inline int islower(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_LOWER) : _pctype[_C] & _LOWER); }inline int isdigit(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_DIGIT) : _pctype[_C] & _DIGIT); }inline int isxdigit(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_HEX) : _pctype[_C] & _HEX); }inline int isspace(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_SPACE) : _pctype[_C] & _SPACE); }inline int ispunct(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_PUNCT) : _pctype[_C] & _PUNCT); }inline int isalnum(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_ALPHA|_DIGIT) : _pctype[_C] & (_ALPHA|_DIGIT)); }inline int isprint(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_BLANK|_PUNCT|_ALPHA|_DIGIT) : _pctype[_C] & (_BLANK|_PUNCT|_ALPHA|_DIGIT)); }inline int isgraph(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_PUNCT|_ALPHA|_DIGIT) : _pctype[_C] & (_PUNCT|_ALPHA|_DIGIT)); }inline int iscntrl(int _C) {return (MB_CUR_MAX > 1 ? _isctype(_C,_CONTROL) : _pctype[_C] & _CONTROL); }#endif /* __cplusplus */#endif /* _MAC */#define _tolower(_c) ( (_c)-'A'+'a' )#define _toupper(_c) ( (_c)-'a'+'A' )#define __isascii(_c) ( (unsigned)(_c) < 0x80 )#define __toascii(_c) ( (_c) & 0x7f )#ifndef _WCTYPE_INLINE_DEFINED#ifndef __cplusplus#define iswalpha(_c) ( iswctype(_c,_ALPHA) )#define iswupper(_c) ( iswctype(_c,_UPPER) )#define iswlower(_c) ( iswctype(_c,_LOWER) )#define iswdigit(_c) ( iswctype(_c,_DIGIT) )#define iswxdigit(_c) ( iswctype(_c,_HEX) )#define iswspace(_c) ( iswctype(_c,_SPACE) )#define iswpunct(_c) ( iswctype(_c,_PUNCT) )#define iswalnum(_c) ( iswctype(_c,_ALPHA|_DIGIT) )#define iswprint(_c) ( iswctype(_c,_BLANK|_PUNCT|_ALPHA|_DIGIT) )#define iswgraph(_c) ( iswctype(_c,_PUNCT|_ALPHA|_DIGIT) )#define iswcntrl(_c) ( iswctype(_c,_CONTROL) )#define iswascii(_c) ( (unsigned)(_c) < 0x80 )#define isleadbyte(_c) (_pctype[(unsigned char)(_c)] & _LEADBYTE)#elif 0 /* __cplusplus */inline int iswalpha(wint_t _C) {return (iswctype(_C,_ALPHA)); }inline int iswupper(wint_t _C) {return (iswctype(_C,_UPPER)); }inline int iswlower(wint_t _C) {return (iswctype(_C,_LOWER)); }inline int iswdigit(wint_t _C) {return (iswctype(_C,_DIGIT)); }inline int iswxdigit(wint_t _C) {return (iswctype(_C,_HEX)); }inline int iswspace(wint_t _C) {return (iswctype(_C,_SPACE)); }inline int iswpunct(wint_t _C) {return (iswctype(_C,_PUNCT)); }inline int iswalnum(wint_t _C) {return (iswctype(_C,_ALPHA|_DIGIT)); }inline int iswprint(wint_t _C) {return (iswctype(_C,_BLANK|_PUNCT|_ALPHA|_DIGIT)); }inline int iswgraph(wint_t _C) {return (iswctype(_C,_PUNCT|_ALPHA|_DIGIT)); }inline int iswcntrl(wint_t _C) {return (iswctype(_C,_CONTROL)); }inline int iswascii(wint_t _C) {return ((unsigned)(_C) < 0x80); }inline int isleadbyte(int _C) {return (_pctype[(unsigned char)(_C)] & _LEADBYTE); }#endif /* __cplusplus */#define _WCTYPE_INLINE_DEFINED#endif /* _WCTYPE_INLINE_DEFINED *//* MS C version 2.0 extended ctype macros */#define __iscsymf(_c) (isalpha(_c) || ((_c) == '_'))#define __iscsym(_c) (isalnum(_c) || ((_c) == '_'))#endif /* _CTYPE_DISABLE_MACROS */#if !__STDC__/* Non-ANSI names for compatibility */#ifndef _CTYPE_DEFINED_CRTIMP int __cdecl isascii(int);_CRTIMP int __cdecl toascii(int);_CRTIMP int __cdecl iscsymf(int);_CRTIMP int __cdecl iscsym(int);#else#define isascii __isascii#define toascii __toascii#define iscsymf __iscsymf#define iscsym __iscsym#endif#endif /* __STDC__ */#ifdef __cplusplus}#endif#endif /* _INC_CTYPE */
其中的函数的解释如下
isalnum(c) 如果c是数字或者字母,则为trueisalpha(c) 如果c是字母,则为trueiscntrl(c) 如果c是控制字符,则为trueisdigit(c) 如果c是数字,则为trueisgraph(c) 如果c不是空格,但可打印,则为trueislower(c) 如果c是小写字母,则为trueisprint(c) 如果c是可以打印的字符,这位trueispunct(c) 如果c是标点符号,则为trueisspace(c) 如果c是空白字符,则为trueisupper(c) 如果c是大写字母,则为trueisxdigit(c) 如果c是十六进制数,则为truetolower(c) 如果c是大写字母与,则返回其小写字母形式,否则直接返回ctoupper(c) 如果c是小写字母,则返回其大写字母形式,否则直接返回c
大部分函数式测试一个给定的字符是否符合条件,并返回一个int值作为真值。如果测试失败则返回0,否则返回一个无意义的非0值,表示被测试的字符符合条件。
可打印的字符是指那些可以显示表示的字符。空白字符则是空格,制表符,垂直制表符,回车符,换行符和进纸符中的任一种;标点符号就是除了数字,字母或(可打印的)空白字符(如空格)以外的其他可打印字符。
下面的程序是将"Hello World!"字符串变为小写的Demo:
下面的程序是将"Hello World!"字符串变为小写的Demo:
#include <iostream>#include <string>#include <cctype>using namespace std;int main(){string s1="Hello World!";for(string::size_type i=0;i!=s1.size();i++){s1[i]=tolower(s1[i]);}cout<<s1<<endl;return 0;}
- c++_标准库类型string
- 标准库String类型
- 标准库string类型
- 标准库string类型
- 标准库string类型
- 标准库类型 string
- 标准库string类型
- 标准库类型string
- 标准库类型string
- 标准库 string 类型
- 标准库string类型
- 标准库类型string
- 标准库类型string
- 标准库类型string
- 标准库string类型
- 标准库string类型
- 标准库类型string
- 标准库 String 类型
- js实现让人物走路,点击人物图片实现砍杀
- ruby学习之文件和数据库(一)
- 基于kerberos的分布式计算平台安全
- java CA证件相关操作,Android,java
- android之Looper详解及api详细介绍,使用案例
- C++标准库string类型
- 01-设计模式之--单例设计模式
- springmvc入门demo说明
- RabbitMQ 工具
- solaris 下 oracle imppd 过程中出现的问题
- iOS系统Crash文件分析方法
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- 数据对齐
- java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.String