且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字符串的元素存储到不同的向量/容器c ++中

更新时间:2023-11-07 16:59:04

一个简单的方法是使用 std :: stringstream 结合 getline 像这样:

A simple way of doing this is probably using a std::stringstream in combination with getline like this:

std::string str;
while (std::getline(fin, str)) // fin is the input stream
{
    std::istringstream iss(str);
    std::string token;
    while (std::getline(iss, token, ',')) // get the token as a std::string
    {
         // process it here, can use `stoi` to convert to int
    }
}