打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
4.2 字符串

strings.cpp: storing string in an array

#include <iostream>
#include <cstring>

int main()
{
  using namespace std;
  
  const int Size = 15;
  char name1[Size];
  char name2[Size] = "C++owboy";
  
  cout << "Howdy! I'm " << name2;
  cout << "! What's your name?\n";
  cin >> name1;
  cout << "Well, " << name1 << ", your name has ";
  cout << strlen(name1) << " letters and is stored\n";
  cout << "in an array of " << sizeof(name1) << " bytes.\n";
  cout << "Your initial is " << name1[0] << ".\n";
  
  name2[3] = '\0';
  cout << "Here are the first 3 characters of my name: ";
  cout << name2 << endl;
  
  return 0;
}

1. 编译输出:

Howdy! I'm C++owboy! What's your name?
Zhangsan
Well, Zhangsan, your name has 8 letters and is stored
in an array of 15 bytes.
Your initial is Z.
Here are the first 3 characters of my name: C++

2. 代码详解:

  • 字符串是存储在内存的连续字节中的一系列字符。可存储于char数组中。

    字符串提供了一种存储文本信息的便捷方式,如提供给用户的信息。

    C-风格字符串以空字符(null character)结尾,空字符被写作\0,其ASCII码为0,用来标记字符串的结尾。

    字符串常量(string constant)或字符串字面值(string literal):使用一个双引号括起的字符串。

    用引号括起的字符串隐式地包括结尾的空字符,因此不用显式地包括它。

    字符串常量(使用双引号)不能与字符常量(使用单引号)互换。'S'是83的另一种写法,char a = 'S';"S"表示字符S和\0组成的字符串,实际上表示的是字符串所在的内存地址。

  • 任何两个由空白(空格、制表符和换行符)分隔的字符串常量都将自动拼接成一个。

    注意:拼接时不会在被连接的字符串之间添加空格,第二个字符串的第一个字符将紧跟在第一个字符串的最后一个字符(不考虑\0)后面。第一个字符串中的\0字符将被第二个字符串中的第一个字符取代。

  • 字符串存储到数组中的两种方式:①将数组初始化为字符串常量;②将键盘或文件输入读入到数组中。

  • strlen():确定字符串的长度。标准头文件cstring提供了该函数。

    sizeof运算符指出整个数组的长度:15字节。

    strlen()函数返回的是存储在数组中的字符串的长度,而不是数组本身的长度。另外,strlen()只计算可见的字符,而不把空字符计算在内。

    数组的长度 >= strlen() +1

  • 程序将name2[3]设置为空字符,这使得字符串在第3个字符后即结束,虽然数组中还有其他的字符。


instr1.cpp: reading more than one string

#include <iostream>

int main()
{
  using namespace std;
  
  const int ArSize = 20;
  char name[ArSize];
  char dessert[ArSize];
  
  cout << "Enter your name:\n";
  cin >> name;
  cout << "Enter your favorite dessert:\n";
  cin >> dessert;
  cout << "I have some delicious " << dessert
       << " for you, " << name << ".\n";
  
  return 0;
}

1. 编译输出:

Enter your name:
Lebron James
Enter your favorite dessert:
I have some delicious James for you, Lebron.

2. 代码详解:

  • cin使用空白(空格、制表符和换行符)来确定字符串的结束位置,这意味着cin在获取字符数组输入时只读取一个单词。读取该单词后,cin将该字符串放到数组中,并自动在结尾添加空字符。

  • 在该程序中,cin 将 Lebron 作为第一个字符,并将它放到 name 数组中。把 James 留在输入队列中。当cin 在输入队列中搜索用户喜欢的 dessert 时,它发现了 James,因此 cin 读取 James,并将它放到dessert数组中。


instr2.cpp: reading more than one word with getline()

#include <iostream>

int main()
{
  using namespace std;
  
  const int ArSize = 20;
  char name[ArSize];
  char dessert[ArSize];
  
  cout << "Enter your name:\n";
  cin.getline(name, ArSize);
  cout << "Enter your favorite dessert:\n";
  cin.getline(dessert, ArSize);
  cout << "I have some delicious " << dessert
       << " for you, " << name << ".\n";
  
  return 0;
}

1. 编译输出:

Enter your name:
Dirk Hammernose
Enter your favorite dessert:
Redish Torte
I have some delicious Redish Torte for you, Dirk Hammernose.

2. 代码详解:

  • 成员函数getline():读取整行,它使用通过回车键输入的换行符来确定输入结尾。

    调用格式:cin.getline(a, b)。第一个参数存储输入行的数组名称,第二个参数读取字符数。如果这个参数为20,则函数最多读取19个字符,余下的空间用于存储自动在结尾处添加的空字符。

  • getline()通过换行符来确定行尾,但不保存换行符。在存储字符串时,用空字符来替换换行符


instr3.cpp: reading more than one word with get() & get()

#include <iostream>

int main()
{
  using namespace std;
  
  const int ArSize = 20;
  char name[ArSize];
  char dessert[ArSize];
  
  cout << "Enter your name: \n";
  cin.get(name, ArSize).get();
  cout << "Enter your favorite dessert: \n";
  cin.get(dessert, ArSize).get();
  cout << "I have some delicious " << dessert
       << " for you, " << name << ".\n";
  
  return 0;
}

1. 编译输出:

Enter your name:
Mai Parfait
Enter your favorite dessert:
Chocolate Mousse
I have some delicious Chocolate Mousse for you, Mai Parfait.

2. 代码详解:

  • get()的工作方式与getline()类似。但get()不再读取并丢弃换行符,而是将其留在输入队列中

    第一次调用get(),换行符将留在输入队列中,因此第二次调用时看到的第一个字符便是换行符。因此get()认为已经达到行尾,而没有发现任何可读取的内容。

  • 使用不带任何参数的cin.get()调用可读取下一个字符,即使是换行符,为读取下一行输入做好准备。

    cin.get(name, ArSize); cin.get() 和cin(name, ArSize).get()效果相同

  • getline()使用起来简单一些,但get()使用检查错误更简单些。


numstr.cpp: following number input with line input

#include <iostream>

int main()
{
  using namespace std;
  
  cout << "What year was your house built?\n";
  int year;
  (cin >> year).get();
  cout << "What is its street address?\n";
  char address[80];
  cin.getline(address, 80);
  cout << "Year built: " << year << endl;
  cout << "Address: " << address << endl;
  cout << "Done!\n";
  
  return 0;
}

1. 编译输出:

What year was your house built?
1996
What is its street address?
43821 Unsigned Short Street
Year built: 1996
Address: 43821 Unsigned Short Street
Done!

2. 代码详解:

  • 本例中,在读取地址之前要先读取并丢弃换行符。(cin >> year).get( ) 即可实现。

  • (cin >> year).get()对于数字来说可以使用,并丢弃换行符。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
3.2 字符序列 (Character Sequences)
学习C++
cin深入分析(上) - cin输入操作处理(转)_斯莱克威
C++复合类型
string中getline,cin的方法getline(),get总结
C++之标准设备IO操作流
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服