本文对C++数组与字符串输入的问题进行了探讨,需要的朋友可以参考下
对于字符串问题,原来理解的不够深刻,现在讨论一些关于字符串输入的问题
650) this.width=650;” src=”https://ss.gaodaima.com/article/3c/e9/18/3ce918253c59f199b31505e9c3ad1643.jpg-600″/>
1.strlen() 返回的是数组中的字符串的长度,而不是数组本身的长度。
2.strlen()只计算可见的字符,而不把空字符计算在内。
那么更有意思的在后面:
char name[16] = "abcdefg"; //输出结果是多少? cout << name << endl; name[3] = '\0'; //输出结果又是多少? cout << name << endl;
大家猜猜 ?
# include # include # define SIZE 15 using namespace std; int main(void) { char name_cin[SIZE]; char name[SIZE] = "C++owboy"; //initialized array cout << "Hello I'm " << name; cout <> name_cin; cout << "Well " << name_cin << ", your name has "; cout << strlen(name_cin) << " letters and is stored " << endl; cout << "in an array of " << sizeof(name_cin) << "bytes." << endl; cout << "your initial is " << name_cin[0] << "." << endl; name[3] = '\0'; cout << "Here are the first 3 characters of my name : "; cout << name << endl; return 0; }
大家猜猜结果呢?
650) this.width=650;” src=”https://ss.gaodaima.com/article/10/64/f2/1064f28a9ae3ca2feb67bfa9c147b050.jpg-600″/>
name字符串被截断了…
释义:
数组可以用索引来访问数组的各个字符,例如name[0]找到数组的第一个字符,name[3] = ‘\0’; 设置为空字符,使得这个字符串在第三个字符后面即结束,即使数组中还有其他字符。
650) this.width=650;” src=”https://ss.gaodaima.com/article/38/ae/21/38ae21c24a470ea828c3b9f066279cb1.jpg-600″/>
不过cin有个缺陷,就是以空白符为结束标志,如果遇到空格和回车就把这个字符串输入完了,这样就需要用能输入一行字符串的方法来解决,但是先看看这个问题:
# include using namespace std; int main(void) { const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter your name : " <> name; //输入名字 cout << "Ent<a style="color:transparent">来源gao($daima.com搞@代@#码网</a>er your favorite dessert: " <> dessert; //输入甜点的名字 cout << "I have some delicious " << dessert; cout << " for you, " << name << "." << endl; return 0; }
650) this.width=650;” src=”https://ss.gaodaima.com/article/13/93/37/139337ec144ef5bc177500e72045d942.jpg-600″/>
释义:
cin使用空白(空格、制表符、换行符)来定字符串的边界,cin在获取字符数组输入时只读取第一个单词,读取单词后,cin将该字符串放到数组中,并自动在结尾添加空字符’\0′
cin把Meng作为第一个字符串,并放到数组中,把Liang放到输入队列中第二次输入时,发现输入队列Liang,因为cin读取Liang,并将它放到dessert数组中
这时如果能输入一行数据,这个问题不就解决了吗?
getline()、get()可以实现…
# include using namespace std; int main(void) { const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter you name : " << endl; cin.getline(name,ArSize); cout << "Enter you favorite dessert : " << endl; cin.getline(dessert,ArSize); cout << "I have some delicious " << dessert; cout << " for you," << name << endl; return 0; }
650) this.width=650;” src=”https://ss.gaodaima.com/article/14/98/9f/14989fa1444db9bf98ebbb47348e4705.jpg-600″/>
释义:
cin是将一个单词作为输入,而有些时候我们需要将一行作为输入,如 I love C++
iostream中类提供了一些面向行的类成员函数,如getline()和get(),这两个都是读取一行的输入,直到换行符结束,区别是getline()将丢弃换行符
get()将换行符保留在输入序列中
面向行的输入:getline(char* cha,int num)
getline()读取整行,通过换行符来确定结尾,调用可以使用 cin.getline(char* cha,int num),成员函数的方式使用,第一个参数是用来存储输入行的数组的名称,第二个参数是要读取的字符数,如果这个字符数的参数为30,则最多读入29个字符,余下的用于存储自动在结尾处添加的空字符。
get()存储字符串的时候,用空字符结尾。
如果遇到这种情况咋办?
# include using namespace std; int main(void) { cout << "What year was your house built? " <> year; //char ch; //cin.get(ch); 接收换行符 (cin >> year).get(); cout << "What is its street address ? " << endl; char address[80]; cin.getline(address, 80); cout << "Year built : " << year << endl; cout << "Address : " << address << endl; cout << "Done ! " << endl; return 0; }
650) this.width=650;” src=”https://ss.gaodaima.com/article/75/04/02/75040241f1285c7e45971186c05801cf.jpg-600″/>
地址还没有输入,就结束了…
去掉上面的注意,加一个字符,接收换行符就可以了…
注:C++程序常使用指针而不是数组来处理字符串
以上就是本文的全部内容,希望对大家的学习有所帮助。
以上就是探讨数组与字符串输入的问题(C++版)的详细内容,更多请关注gaodaima搞代码网其它相关文章!