当前位置: 代码迷 >> 综合 >> 将string字符串“10086,3433,1234”,以“,”分割开
  详细解决方案

将string字符串“10086,3433,1234”,以“,”分割开

热度:77   发布时间:2023-12-24 01:12:11.0

功能:将string字符串“10086,3433,1234”,以“,”分割开,存到vector容器中
代码片段:

		int posstar = 0;size_t pos;pos = 0;vector<int> v;size_t flagpso = strport.find(',', 0);//"10086,3433,1234" --- 多端口情况if (flagpso != string::npos){
    while ((pos = strport.find(',', pos)) != string::npos){
    cout << "position " << " : " << pos << endl;string v_str = strport.substr(posstar, pos);int v_port = atoi(v_str.c_str());v.push_back(v_port);pos++;posstar = pos;}if (posstar > 0 && posstar < strport.size()){
    string v_str = strport.substr(posstar, strport.size());int v_port = atoi(v_str.c_str());v.push_back(v_port);}}else{
    //没找到 //"10086" --- 单端口情况int v_port = atoi(strport.c_str());v.push_back(v_port);}
  相关解决方案