c++学习笔记-string-1

#include<iostream>
#include<string>

using namespace std;//c++标准库重要的组成部分

int main()
{
    char name[10];//c语言
    string s("maple leaf");//c++

    cout << s << endl;

    //四种定义和初始化的方法

    string s1;
    string s2("HELLO");
    string s3(s2);
    string s4(10, 'a');

    string s5 = "hello";//C++不推荐这种方法

    //cin和cout操作字符串
    //string s6,s7;

    //cin >> s6 >> s7;
    //cout << s6<<s7<< endl;//cin碰见空格自动停止,读取字符直到遇见空格为止
    //如何完整的一行都读取进去
    //用getline
    /*string name1;
    getline(cin, name1);
    cout << name1 << endl;*/

    //例题1,编写程序从标准输入每次读一行文本,然后改写程序,每次读入一个单词
    /*string line;
    while (getline(cin, line))
           cout << line << endl;*/
    string word;
    while (cin >> word)
        cout << word << endl;
    //例2,解释string类型的输入操作符和getline函数分别如何处理空白字符,如上




    system("pause");
    return 0;



}

猜你喜欢

转载自blog.csdn.net/weixin_42655231/article/details/82501678