C++笔记 3.2 标准库类型string

    技术2024-07-16  69

    primer C++笔记

    标准库类型string

    直接初始化和拷贝初始化

    string s; cin >> s; cout << s << endl; // hello 输出:hello 自动忽略开头的空白(即空格符、换行符、制表符) // hello world! 输出:hello string s1, s2; cin >> s1 >> s2; cout << s1 << s2 << endl; // hello world! 输出:helloworld string word; while (cin >> word) //反复读取,直至文件末尾 cout << word << endl; //逐个输出单词,每个单词后面紧跟一个换行 // a b c 0 输出:a\n b\n c\n 0\n //a 换行 也能输出结果 string line; //每次读入一整行,直至到达文件末尾 while (getline(cin, line)) cout << line << endl;

    处理string对象中的字符

    string::size_type类型

    Processed: 0.014, SQL: 9