标准库string类型简述

若想使用标准库的string类需要使用如下声明:

#include <string>

Using std::string;

Using std::wstring;

 

那么就可以使用这两个类了;以string为例子介绍其对外接口:

String类的构造函数

 

String  s1; //

String  s2(s1);

String  s3(“hollo world!”);

String  s4(n, ‘c’);

string 对象的操作

 

s.empty()

如果 为空串,则返回 true,否则返回 false

s.size()

返回 中字符的个数

s[n]

返回 中位置为 的字符,位置从 开始计数

s1 + s2

把 s1 s2 连接成一个新字符串,返回新生成的字符串

s1 = s2

把 s1 内容替换为 s2 的副本

v1 == v2

比较 v1 与 v2的内容,相等则返回 true,否则返回 false

!=, <, <=, >, and >=

保持这些操作符惯有的含义

 

string 对象中字符的处理

 

isalnum(c)

如果 是字母或数字,则为 True

isalpha(c)

如果 是字母,则为 true

iscntrl(c)

如果 是控制字符,则为 true 

isdigit(c)

如果 是数字,则为 true

isgraph(c)

如果 不是空格,但可打印,则为 true

islower(c)

如果 是小写字母,则为 true

isprint(c)

如果 是可打印的字符,则为 true

ispunct(c)

如果 是标点符号,则 true

isspace(c)

如果 是空白字符,则为 true

isupper(c)

如果 是大写字母,则 true

isxdigit(c)

如果是 十六进制数,则为 true

tolower(c)

如果 大写字母,返回其小写字母形式,否则直接返回 c

toupper(c)

如果 是小写字母,则返回其大写字母形式,否则直接返回 c

 

 

还有一个获取字符串的函数:

istream& getline ( istream& , string& );

Getline函数返回时会丢弃换行符,所以换行符不会存储到string对象中。

参考文章:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html

原文地址:https://www.cnblogs.com/fly-fish/p/3602520.html