#pragma once #pragma comment(lib,"rpcrt4.lib") #include <Windows.h> #include <rpcdce.h> #include <iostream> #include <string> #include <vector> using namespace std; void stringDemo() { string str = "Hello world"; string str2 = str.substr(12); cout << str << endl << str2 << endl; } string getUuid() { RPC_CSTR rpcStr; string uuidValue; UUID newUUID; UuidCreate(&newUUID); UuidToStringA(&newUUID, &rpcStr); uuidValue = (char*)rpcStr; RpcStringFreeA(&rpcStr); return uuidValue; } void splitString(string str, string delimeter) { size_t pos = 0; string tempStr; while ((pos = str.find(delimeter)) != string::npos) { tempStr = str.substr(0, pos); cout << tempStr << endl; str.erase(0, pos + delimeter.length()); } cout << str << endl; } void splitUuid() { string uuidStr = getUuid(); cout << uuidStr << endl; string delimeter = "-"; splitString(uuidStr, delimeter); } void splitStringToVector(vector<string> &vec, string sourceStr, string delimeter) { size_t pos = 0; string tempStr; while ((pos = sourceStr.find(delimeter)) != string::npos) { tempStr = sourceStr.substr(0, pos); vec.push_back(tempStr); sourceStr.erase(0, pos + delimeter.length()); } vec.push_back(sourceStr); } void stringSplitDemo() { string sourceStr = getUuid(); cout << sourceStr << endl; string delimeter = "-"; vector<string> vec; splitStringToVector(std::ref(vec), sourceStr, delimeter); vector<string>::iterator itr = vec.begin(); cout << "Split:" << endl; while (itr != vec.end()) { cout << *itr << endl; itr++; } } int main() { try { stringSplitDemo(); } catch (const std::exception&ex) { cout << ex.what() << endl; } }
标签:via,string,pos,vec,delimeter,sourceStr,include,split From: https://www.cnblogs.com/Fred1987/p/16787640.html