C - 将文本文件读为一串单词,然后将字符串分为向量

C++ - Read a text file in as a string of words, then split the string into a vector

本文关键字:单词 一串 字符串 向量 然后 文本 文件      更新时间:2023-10-16

假设" grade.txt"的内容为:

    David 99 94 95
    Mike 91 84
    John 100 85
    Lexa 76 88 90 74 85 76
    Paul 83 95
    Ryan 71 72 85
    Tommy 95 96
    Joseph 100 83 85 86
    Denise 100 90 76
    Ava 80 90

文件的第一行用每个学生的名称表示。之后的上线是每个学生成绩的随机集合。

我的问题涉及我如何以单词字符串的形式读取文件,然后将字符串拆分为向量,以便我可以将每个元素组织到班级学生的正确位置中。如果有一种更简单的方法来实现这一目标,我也将不胜感激。谢谢您提供的任何帮助。

这是我的班级学生:

    class Students {
    public:
        Students();
        ~Students();
    private:
        string name;
        int grades;
    };

您可以尝试boost :: split。例如:

//split strFileBuffer with space and get the string vector.
string strFileBuffer;
vector<string> strs;
boost::split(strs, strFileBuffer,boost::is_any_of(" "));