#include<iostream> #include<string> #include<vector> using namespace std; //相对路径转绝对路径,vector实现 //输入路径:/home/abs/../temp/new/../ ,输出路径为:/home/temp vector<string> pathChange(string input){ vector<string> inputpath,outputpath; string tmp; for(int i=0;i<input.size();i++){ //将input字符串按‘/’分开存储到inputpath if(input[i] != '/'){ //向量中。 注意:第一个为空字符串! tmp+=input[i]; } else{ inputpath.push_back(tmp); tmp.clear(); } } while(!inputpath.empty()){ //判断vector最后一个数据是否为“..”,若 if(inputpath.back() != ".."){ //是,则连续2次删除尾数据,若否,则将尾数 tmp = inputpath.back(); //据存到outputpath中,并删除inputpath中的 outputpath.push_back(tmp); //尾数据。 inputpath.pop_back(); } else{ inputpath.pop_back(); inputpath.pop_back(); } } while(!outputpath.empty()){ //将outputpath中的数据从尾到头转 inputpath.push_back(outputpath.back()); //存到inputpath中的从头到尾中 outputpath.pop_back(); } return inputpath; } int main(){ string input = "/home/abs/../temp/new/../",output; vector<string> outputpath; cout<<"输入相对路径:"<<input<<endl; outputpath = pathChange(input); cout<<"输出绝对路径:"; for(unsigned i=1;i<outputpath.size();i++) cout<<"/"<<outputpath.at(i); //由上述函数可以outputpath首数据 cout<<endl; //为空! return 0; }
原文链接: https://www.cnblogs.com/cthon/p/9291555.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/277383
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!