strtok vs istringsteam on splitting the strings?

非 Y 不嫁゛ 提交于 2019-12-11 20:27:43

问题


I am trying to split my actual key on dot and then extract all the fields after splitting it on dot.

My key would look like something this -

t26.example.1136580077.colox

Below is the code I have which I was in the impression, it should work fine but after that I figured it out that it is for windows only and I am running my code on ubuntu and because of that reason I always get -

 error: âstrtok_sâ was not declared in this scope

Below is my code

if(key) {
    vector<string> res;
    char* p;
    char* totken = strtok_s(key, ".", &p);
    while(totken != NULL)
    {
        res.push_back(totken);
        totken = strtok_s(NULL, ".", &p);
    }

    string field1 = res[0]; // this should be t26
    string field2 = res[1]; // this should be example
    uint64_t field3 = atoi(res[2].c_str()); // this should be 1136580077
    string field4 = res[3]; // this should be colox

    cout<<field1<<" "<<field2<<" "<<field3<<" "<<field4<<endl;
}         

I am running Ubuntu 12.04 and g++ version is -

g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3

Is there any way to do the same thing using plain strtok as I don't want to use istringstream as strtok will be more efficient as compared to istringstream..

来源:https://stackoverflow.com/questions/20224898/strtok-vs-istringsteam-on-splitting-the-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!