输入可变长字符串

版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 4.0

Example

从标准输入设备读入字符串,并把字符串存放在字符数组中(输入的字符串长度不定)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int ()
{
size_t arry_size = 10;
char *p_str = new char[arry_size];
int count = 0;
char c;//存放字符

cout << "Enter some chars:" << endl;
while (cin.get(c))//每次读取一个字符赋给c
{
if (count + 1 >= arry_size)//超出预设长度
{
arry_size += 10;//长度增加10
char *p_temp = new char[arry_size];//申请新空间
strncpy(p_temp, p_str, count);//将就空间的内容拷贝到新空间
delete[] p_str;//销毁以前的p_str(p_temp)
p_str = p_temp;//将新空间p_temp赋给p_str
}

p_str[count] = c;
count++;
}
p_str[count] = '