




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第關(guān)于c++11與c風(fēng)格路徑拼接的速度對(duì)比目錄這里用c++11的stingstream實(shí)現(xiàn)一個(gè)用c重新實(shí)現(xiàn)一遍c++11的std庫(kù)中沒(méi)有提供路徑拼接的功能
比如我需要將d:\\temp\\robin和..\\config.ini路徑拼接,
這里用c++11的stingstream實(shí)現(xiàn)一個(gè)
stringreplace_str(stringstr,conststringto_replaced,conststringnewchars)
for(string::size_typepos(0);pos!=string::npos;pos+=newchars.length())
{
pos=str.find(to_replaced,pos);
if(pos!=string::npos)
str.replace(pos,to_replaced.length(),newchars);
else
break;
}
return
str;
//windows
std::stringcombinePath(constchar*dir,constchar*name)
{
//printf("%s
+
%s\n",dir,name);
if(dir==nullptr||name==nullptr)
return"";
stringtemp=dir;
replace_str(temp,"/","\\");
std::stringstreamoss(temp);
std::dequestd::stringvecDir;
std::dequestd::stringvecName;
stringpart;
while(std::getline(oss,part,'\\'))
{
if(part.length()==0)
continue;
vecDir.emplace_back(part);
}
temp=name;
replace_str(temp,"/","\\");
oss.clear();
oss.str(temp);
while(std::getline(oss,part,'\\'))
{
if(part.length()==0)
continue;
vecName.emplace_back(part);
}
intindex=0;
while(indexvecName.size())
{
if(vecName[0]==".")
{
vecName.pop_front();
}
//elseif(vecName[0].find("..")!=vecName[0].npos)
elseif(vecName[0]=="..")
{
vecName.pop_front();
if(vecDir.size()1)
vecDir.pop_back();
}
else
{
vecDir.emplace_back(vecName[0]);
vecName.pop_front();
}
}
oss.clear();
oss.str("");
for(inti=0;ivecDir.size();i++)
{
ossvecDir[i];
if(vecDir.size()==1||i(vecDir.size()-1))
{
oss"\\";
}
}
returnoss.str();
}
測(cè)試方法:
voidtest1()
coutcombinePath("d:\\temp\\robin\\","..\\demo\\config.ini")endl;
coutendl;
coutcombinePath("d:","..\\demo\\config.ini")endl;
coutendl;
coutcombinePath("d:\\temp\\robin","../demo/config.ini")endl;
coutendl;
coutcombinePath("d:\\temp\\robin","./config.ini")endl;
coutendl;
coutcombinePath("d:\\temp\\robin","config.ini")endl;
coutendl;
coutcombinePath("d:\\temp\\robin\\","\\config.ini")endl;
coutendl;
}
測(cè)試發(fā)現(xiàn),release使用O2優(yōu)化,能平均在2~4微秒左右,還是不夠快啊,
用c重新實(shí)現(xiàn)一遍
//字符串范圍內(nèi),逆向查找
constchar*
strrfind(constchar*begin,constchar*end,constcharc)
constchar*buf=end;
while(buf=begin)
{
if(*buf==c)
returnbuf;
buf--;
}
returnnullptr;
size_t
combinePathC(char**buffer,constchar*dir,constchar*name)
intn1=strlen(dir);
intn2=strlen(name);
size_tlen=
n1+n2+2;
char*buf=newchar[len];
*buffer=buf;
memcpy(buf,dir,n1);
size_tlen1=n1;
//末尾保證有一個(gè)"\"
if(buf[n1-1]=='\\')
{
len1=n1;
}
else
{
buf[n1]='\\';
len1=n1+1;
}
buf[len1]='\0';
//len1++示當(dāng)前拼接的長(zhǎng)度
size_tindex=0;
char*rPart=(char*)name;
size_tlen2=n2;
while(indexn2)
//使用index向后滑動(dòng),直到末尾
{
//滑動(dòng)后當(dāng)前位置
rPart=(char*)name+index;
char*tmp=(char*)strchr(rPart,'\\');
if(tmp==nullptr)
//endhere
{
//拼接剩下的
len2=n2-index;
memcpy(buf+len1,rPart,len2);
len1+=len2;
buf[len1]='\0';
len1++;
break;
}
//當(dāng)前找到的長(zhǎng)度
len2=tmp-rPart;
if(len2==0)
//遇到"\config.ini",去掉1個(gè)字符
{
index+=1;
}
elseif(len2==1rPart[0]=='.')
//遇到".\config.ini",去掉2個(gè)字符
{
index+=2;
}
elseif(len2=2rPart[0]=='.')
//遇到"..\config.ini","..x\config.ini"去掉3個(gè)字符,末尾去掉一個(gè)目錄
{
index+=len2+1;
constchar*back=strrfind(buf,buf+len1-2,'\\');
//從末尾的"\"前一個(gè)字符開(kāi)始找
if(back==nullptr)
{
//dir當(dāng)前是這樣的情況,"d:\”
}
elseif((back-dir)==2)
//dir當(dāng)前是這樣的情況,"d:\temp\”
{
len1=3;
buf[3]='\0';
}
else
//dir當(dāng)前是這樣的情況,"d:\temp\test1\”
{
len1=back-buf+1;
buf[len1]='\0';
}
}
else
//
遇到首字符不為點(diǎn)"x\config.ini",
{
index+=len2+1;
memcpy(buf+len1,name+index,len2+1);
len1+=len2+1;
}
}
returnlen1;
}
測(cè)試一下:
char*buf=nullptr;
size_tlen;
len=combinePathC(buf,"d:\\temp\\robin\\","config.ini");
coutbufendl;
len=combinePathC(buf,"d:\\temp\\robin","config.ini");
coutbufendl;*/
len=combinePathC(buf,"d:\\temp\\robin",
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 班會(huì)課件APP制作
- 《貝塞爾函數(shù)及其應(yīng)用》課件
- 一年級(jí)學(xué)生安全教育課件
- 禽類(lèi)屠宰行業(yè)職業(yè)技能提升與培訓(xùn)考核試卷
- 新能源技術(shù)與化妝品產(chǎn)業(yè)發(fā)展考核試卷
- 幼兒園暴風(fēng)雪安全教育
- 糖果企業(yè)市場(chǎng)營(yíng)銷(xiāo)渠道建設(shè)考核試卷
- 環(huán)境工程專(zhuān)題課件
- 航海英語(yǔ)閱讀與寫(xiě)作能力測(cè)試考核試卷
- 《數(shù)據(jù)庫(kù)操作基礎(chǔ)第11講》課件
- 2025-2030中國(guó)戰(zhàn)斗機(jī)行業(yè)市場(chǎng)發(fā)展趨勢(shì)與前景展望戰(zhàn)略研究報(bào)告
- 大學(xué)英語(yǔ)四級(jí)考試2024年12月真題(第一套)Part I Writing
- 吡侖帕奈產(chǎn)品簡(jiǎn)介
- 高處作業(yè)力學(xué)基礎(chǔ)知識(shí)
- 洗煤廠(chǎng)應(yīng)急救援預(yù)案
- 幼兒園科學(xué)發(fā)現(xiàn)室環(huán)境布置設(shè)計(jì)方案
- 《企業(yè)的績(jī)效管理問(wèn)題與優(yōu)化策略的分析案例-以舍得酒業(yè)公司為例9100字》
- 超星爾雅學(xué)習(xí)通《移動(dòng)互聯(lián)網(wǎng)時(shí)代的信息安全與防護(hù)(南京師范大學(xué))》2025章節(jié)測(cè)試附答案
- (部編版)語(yǔ)文四年級(jí)上冊(cè)課外閱讀“天天練”100篇,附參考答案
- DB31∕701-2020 有色金屬鑄件單位產(chǎn)品能源消耗限額
- 統(tǒng)編版語(yǔ)文六年級(jí)下冊(cè)古詩(shī)詞誦讀10《清平樂(lè)》
評(píng)論
0/150
提交評(píng)論