c++primer读书笔记——string

string的声明和引用:

#include<string>

using std::string;

 

string 的定义和初始化:

 

string s1;                      调用默认构造函数,s1被初始化成空字符串

string s2(s1);                将s2初始化成字符串s1的拷贝

string s3("hello");         用字符串常量初始化s3

string s4(n, 'c');           s4被初始化成N个字符'c'组成的字符串

 

提示:由于历史的原因为了和C兼容。字符串常量和标准库定义的string是不一样的类型。这个事实用以引起混乱但是在使用两者的时候一定要记住:二者不是同一类型。

 

#include<iostream>
#include<string>
using std::getline;
using std::cout;
using std::cin;
using std::string;
using std::endl;

int main(void)
{
 string s1;
 string s2(s1);
 string s3("hello world!");
 string s4(10,'a');

 cout << "The first string s1:" << s1 << endl;
 cout << "The second string s2:" << s2 << endl;
 cout << "The third string s3:" << s3 << endl;
 cout << "The fourth string s4:" << s4 << endl;

 while(cin >> s1)
 {
  cout << s1 << endl;
  break;
 }
 while(getline(cin,s2))
 {
  cout << s2 << endl;
 }

 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值