Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
Subscribe to see which companies asked this question
string simplifyPath(string path) { string str; vector<string> strs; stringstream ss(path); while (getline(ss, str, ‘/‘)) { if (str == "." || str == "") continue; else if (str == "..") { if (!strs.empty()) strs.pop_back(); } else strs.push_back(str); } string ret; for (auto s : strs) ret += "/" + s; return ret.empty() ? "/" : ret; }
原文:http://www.cnblogs.com/sdlwlxf/p/5128284.html