温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

关于数组与字符串输入的问题-C++版

发布时间:2020-08-10 20:57:02 来源:网络 阅读:2031 作者:liam2199 栏目:移动开发

对于字符串问题,原来理解的不够深刻,现在讨论一些关于字符串输入的问题

关于数组与字符串输入的问题-C++版

   1.strlen() 返回的是数组中的字符串的长度,而不是数组本身的长度。

   2.strlen()只计算可见的字符,而不把空字符计算在内。


  那么更有意思的在后面:

char name[16] = "abcdefg";
//输出结果是多少?
cout << name << endl;
name[3] = '\0';
//输出结果又是多少?
cout << name << endl;

   大家猜猜 ?


# include <iostream>
# include <cstring>
# 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 << "! What is your name ? ";
    cin >> 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;
}


   大家猜猜结果呢?


关于数组与字符串输入的问题-C++版

   name字符串被截断了...


   释义:  

   数组可以用索引来访问数组的各个字符,例如name[0]找到数组的第一个字符,name[3] = '\0';  设置为空字符,使得这个字符串在第三个字符后面即结束,即使数组中还有其他字符。


关于数组与字符串输入的问题-C++版

   不过cin有个缺陷,就是以空白符为结束标志,如果遇到空格和回车就把这个字符串输入完了,这样就需要用能输入一行字符串的方法来解决,但是先看看这个问题:


# include <iostream>
using namespace std;
int main(void)
{
    const int ArSize = 20;
    char name[ArSize];
    char dessert[ArSize];
    cout << "Enter your name : " << endl;
    cin >> name;  //输入名字
    cout << "Enter your favorite dessert: " << endl;
    cin >> dessert; //输入甜点的名字
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << "." << endl;
    return 0;
}

关于数组与字符串输入的问题-C++版

   释义:


cin使用空白(空格、制表符、换行符)来定字符串的边界,cin在获取字符数组输入时只读取第一个单词,读取单词后,cin将该字符串放到数组中,并自动在结尾添加空字符'\0'

cin把Meng作为第一个字符串,并放到数组中,把Liang放到输入队列中第二次输入时,发现输入队列Liang,因为cin读取Liang,并将它放到dessert数组中


  这时如果能输入一行数据,这个问题不就解决了吗?

   getline()、get()可以实现...

   

# include <iostream>
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;
}


关于数组与字符串输入的问题-C++版

   释义:

   

   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 <iostream>
using namespace std;
int main(void)
{
    cout << "What year was your house built? " << endl;
    int year;
    cin >> 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;
}

   关于数组与字符串输入的问题-C++版

   地址还没有输入,就结束了...

   去掉上面的注意,加一个字符,接收换行符就可以了...



   注:C++程序常使用指针而不是数组来处理字符串

   


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI