
版权声明:自由转载-非商用-非衍生-保持署名 | 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)) { if (count + 1 >= arry_size) { arry_size += 10; char *p_temp = new char[arry_size]; strncpy(p_temp, p_str, count); delete[] p_str; p_str = p_temp; }
p_str[count] = c; count++; } p_str[count] = '
|
近期评论