The easiest way is to use find_last_of
member function of std::string
string s1("../somepath/somemorepath/somefile.ext");
string s2("..\somepath\somemorepath\somefile.ext");
cout << s1.substr(0, s1.find_last_of("\/")) << endl;
cout << s2.substr(0, s2.find_last_of("\/")) << endl;
This solution works with both forward and back slashes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…