本章讲解如下三种标准库类型:
string、
vector、
bitset。其中,string定义了大小可变的字符串;vector用于保存一组指定类型的对象;bitset提供了一种抽象方法来操作位的集合。
命名空间的using声明
注:为避免名字冲突,要避免使用using指示,而应当使用using声明。两者表示如下:
方式
| 示例 |
using指示:
|
using namespace std;
|
using声明:
|
using std::cin;
using std::cout;
using std::endl;
|
标准库string类型
1、几种初始化string类对象的方式如下:
方式
|
含义
|
string s1;
|
默认构造函数
|
string s2(s1);
|
将s2初始化为s1的一个副本
|
string s3("value");
|
将s3初始化为一个字符串字面值副本
|
string s4(n, 'c');
|
将s4初始化为字符'c'的n个副本
|
string s5((char *str), n);
|
将s5初始化为从str开始的n个字符
|
string s6("value", n); |
将s6初始化为从"value"第一个字符开始往后的n个字符组成的字符串副本
|
string s7(s1, n);
|
将s7初始化为从s1的第一个字符开始跳过n个字符后剩余的字符串副本
|
2、string对象的读写
读取方法 | 处理原则 |
string s;
cin >> s;
|
这两句代码是指从标准输入读取字符串并存储到s当中,读取的原则如下:
a. 忽略开头的所有空白字符(如:空格、换行符、制表符等);
b. 读取字符直到再次遇到空白字符为止(该空白字符仍然留在输入流中)。
|
string line;
getline(cin, line);
|
这两句代码是指从标准输入读取字符串并存储到s当中,读取的原则如下:
a. 不忽略开头的空白字符(如:空格、制表符等,不包含换行符);
b. 读取字符直到遇到换行符符为止(换行符从输入流中去掉,但并不存储在string对象中)。
|
样例一(cin读入字符串):
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string s;
cin >> s;
cout << s << endl;
return 0;
}
输出:
[chapter3]$ ./a.out
Hello World!
Hello
样例二(cin循环读取,并将单词逐个输出):
说明:cin >> word语句一般返回true,只有当读取到EOF时返回false。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string word;
while (cin >> word)
{
cout << word << endl;
}
cout << "end ok\n";
return 0;
}
输出:
[chapter3]$ ./a.out
a
a
b
b
c
c
<Ctrl + D>
end ok
样例三(使用getline函数读取整行文本):
说明:getline(cin, line)语句同cin >> word一样,一般返回true,只有当读取到EOF时返回false。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string line;
while (getline(cin, line))
{
cout << line << endl;
}
cout << "end ok" << endl;
return 0;
}
输出:
[chapter3]$ ./a.out
a b c
a b c
d e f
d e f
end ok
3、string对象的操作
操作
|
含义
|
s.empty()
|
判断s是否为空,是则返回true,否则返回false
|
s.size()
|
返回s中字符的个数;该函数的返回类型为
string::size_type,为一个无符号数,至于是unsigned int还是unsigned long由机器决定。
|
s[n]
|
返回s中位置为n的字符,位置从0开始计数。这里的下标n的类型最好也使用
string::size_type。
|
s1 + s2
s1 + "string"
|
将s1和s2连接成一个新的字符串,并返回新生成的字符串。
字符串进行连接时,+操作符的左右操作数必须至少有一个是string类型的(即:两个字符串字面值相加是非法的),并且+操作返回一个新的string对象。
|
s1 = s2
|
把s1的内容替换成s2的副本
|
s1 == s2
|
比较s1和s2的内容,相等则返回true,不等则返回false。这里相等的含义是:长度相同,并且含有相同的字符。
|
!=, >, >=, <, <=
|
保持惯有含义,参考 ==
|
样例一(string的empty和size操作):
注:判断字符串为空的两种方法:a. 使用s.empty()进行判断;b. 使用s.size()是否为0进行判断。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("Baidu is a good company\n");
cout << "The size of: " << str << "is " << str.size()
<< " characters, including the newline." << endl;
string str2;
if (str2.size() == 0)
{
cout << "Method of size: str2 is empty." << endl;
}
if (str2.empty())
{
cout << "Method of empty: str2 is empty." << endl;
}
return 0;
}
输出:
[chapter3]$ ./a.out
The size of: Baidu is a good company
is 24 characters, including the newline.
Method of size: str2 is empty.
Method of empty: str2 is empty.
样例二(读取string中的字符):
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("Baidu is a good company\n");
for (string::size_type i = 0; i < str.size(); i++)
{
cout << str[i] << endl;
}
return 0;
}
输出:
[chapter3]$ ./a.out
B
a
i
d
u
i
s
a
g
o
o
d
c
o
m
p
a
n
y
4、string对象中字符的处理
cctype头文件定义的函数
|
说明
|
isalnum(c)
|
如果c是字母或数字,则为true。
|
isalpha(c)
|
如果c是字母,则为true。
|
iscntrl(c)
|
如果c是控制字符,则为true。
|
isdigit(c)
|
如果c是数字,则为true。
|
isgraph(c)
|
如果c不是空格,但可以打印,则为true。
|
islower(c)
|
如果c是小写字母,则为true。
|
isprint(c)
|
如果c是可打印字符,则为true。
|
ispunct(c)
|
如果c是标点符号,则为true。
|
isspace(c)
|
如果c是空白字符,则为true。
|
isupper(c)
|
如果c是大写字母,则为true。
|
isxdigit(c)
|
如果c是十六进制数,则为true。
|
tolower(c)
|
如果c是大写字母,则返回其小写字母形式,否则直接返回c。
|
toupper(c)
|
如果c是小写字母,则返回其大写字母形式,否则直接返回c。
|