当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++ string提取某一段被分割的子串

C++ string提取某一段被分割的子串

2018年03月02日  | 移动技术网IT编程  | 我要评论

红邸,孟锦云近况,今日钢铁

直接放上题目leetcode71

题目主要是如何提取/和/之间的子串,强行做事可以的,但是很麻烦,所以下面直接放出代码:

class Solution {
public:
    string simplifyPath(string path) {
        vectorst;
        string tmp, res = "";
        stringstream ss(path);
        while(getline(ss, tmp, '/')){
            if(tmp == "" || tmp == ".") continue;
            if(tmp == ".." && !st.empty()) st.pop_back();
            else if(tmp != "..") st.push_back(tmp);
        }

        for(string s : st) res += "/" + s;
        return res == "" ? "/" : res;
    }
};

这里是用了stringstream这个类,然后用getline这个方法提取,使得题目变得简单。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网