c++数组排序和字符数组

让用户输入一行字符(空格分隔的多个单词),然后输出每一个单词(每行一个单词)。

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int main() {
    // 读入一行字符
    string s;
    getline(cin, s);

    // 将字符串按空格分隔为单词
    char str[s.length() + 1];
    strcpy(str, s.c_str());
    char* pch = strtok(str, " "); // 使用 strtok 函数分隔单词
    while (pch != nullptr) {
        cout << pch << endl; // 输出单词并换行
        pch = strtok(nullptr, " "); // 继续分隔下一个单词
    }

    return 0;
}