I store list of my students in a text file. Every Student's primary data is stored in one line and list of his classes as second line, where classes are separated by ','. It goes like Mathematics,Linear Algebra,Physical Education Adv, Optics,. If i read this to one string, how can i divide it, so temp1 will get Mathematics, temp2 Linear Algera and so on...?

有帮助吗?

解决方案

Use strtok function - Use this page for reference http://www.cplusplus.com/reference/cstring/strtok/

Though it might look difficult to use at first, but it's quite efficient

其他提示

In case you need a robust ready-to-go function that works with std::string and std::vector:

using namespace std;
vector<string> splitString(const string &str, const string &delim)
{
    size_t start = 0, delimPos = 0;
    vector<string> result;
    do
    {
        delimPos = str.find(delim, start);
        result.push_back(string(str, start, delimPos-start));
        start = delimPos + delim.length();
    } while(delimPos != string::npos);
    return result;
}

I actually pulled this out of my snipped library ;)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top