在dot上拆分字符串并在C++中从中检索每个值

AKI*_*WEB 2 c++ split stdstring

我需要.在C++中拆分字符串..

下面是我的字符串 -

@event.hello.dc1

现在我需要拆就.在上面的字符串,并从中获取@event,然后传递@event到下面的方法-

bool upsert(const char* key);

以下是我从这里阅读后到目前为止的代码-

void splitString() {

    string sentence = "@event.hello.dc1";

    istringstream iss(sentence);
    copy(istream_iterator<string>(iss), istream_iterator<string>(), ostream_iterator<string>(cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)

但我无法理解如何@event通过拆分.使用上述方法来提取,因为上述方法仅适用于空白...以及如何通过拆分提取该字符串中的所有内容,.如下所述 -

split1 = @event
split2 = hello
split3 = dc1
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助..

Lih*_*ihO 9

你可以使用std::getline:

string sentence = "@event.hello.dc1";
istringstream iss(sentence);
std::vector<std::string> tokens;
std::string token;
while (std::getline(iss, token, '.')) {
    if (!token.empty())
        tokens.push_back(token);
}
Run Code Online (Sandbox Code Playgroud)

这导致:

tokens[0] == "@event"
tokens[1] == "hello"
tokens[2] == "dc1"
Run Code Online (Sandbox Code Playgroud)