#include <string> #include <sstream> #include <iostream> int main() { std::wstring keyname = L"HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Firefox"; std::wstring root, key; std::wistringstream iss(keyname); if (std::getline(iss, root, L'\') && std::getline(iss, key)) { std::wcout<<root<<" "<<key; } }
输出:
HKEY_LOCAL_MACHINE SOFTWAREMozillaFirefox
另一种方法:
#include <string> std::wstring keyname = ...; // "HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Firefox" std::wstring::size_type pos = keyname.find(L'\'); if (pos != std::wstring::npos) { std::wstring root = keyname.substr(0, pos); std::wstring key = keyname.substr(pos+1); // use root and key as needed... }