string:clear的用法

本文介绍了一段代码的功能,该代码用于清除输入文本字符串的内容,使其变为一个空字符串。通过循环读取用户输入直到遇到句号结束,并在每次读取字符后检查是否为换行符,如果是则输出当前字符串并清空它。此过程展示了如何在C++中实现简单的文本输入与处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

clear string:Erases the contents of the string, which becomes an empty string (with a length of 0 characters).

// string::clear
#include <iostream>
#include <string>

int main ()
{
  char c;
  std::string str;
  std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";
  do {
    c = std::cin.get();
    str += c;
    if (c=='\n')
    {
       std::cout << str;
       str.clear();
    }
  } while (c!='.');
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值