最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于c++11與c風格路徑拼接的速度對比

 更新時間:2022年07月22日 08:38:46   作者:飛鳥真人  
這篇文章主要介紹了關(guān)于c++11與c風格路徑拼接的速度對比分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

c++11的std庫中沒有提供路徑拼接的功能

比如我需要將  "d:\\temp\\robin"  和   "..\\config.ini" 路徑拼接,

這里用c++11的stingstream實現(xiàn)一個

string& replace_str(string& str, const string& to_replaced, const string& newchars)
{
?? ?for (string::size_type pos(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::string combinePath(const char *dir, const char* name)
{?? ?
?? ?//printf("%s ?+ ?%s\n", dir, name);
?? ?if (dir == nullptr || name == nullptr)
?? ??? ?return "";
?? ?string temp = dir;
?? ?replace_str(temp, "/", "\\");
?? ?std::stringstream oss(temp);
?? ?std::deque<std::string> vecDir;
?? ?std::deque<std::string> vecName;
?
?? ?string part;
?? ?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);
?? ?}
?
?? ?int index = 0;
?? ?while (index < vecName.size())
?? ?{
?? ??? ?if (vecName[0] == ".")
?? ??? ?{
?? ??? ??? ?vecName.pop_front();
?? ??? ?}
?? ??? ?//else if (vecName[0].find("..") != vecName[0].npos)
?? ??? ?else if (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 (int i=0; i<vecDir.size(); i++)
?? ?{
?? ??? ?oss << vecDir[i];
?? ??? ?if (vecDir.size() == 1 || i < (vecDir.size() - 1))
?? ??? ?{
?? ??? ??? ?oss << "\\";
?? ??? ?}
?? ?}?
?? ?return oss.str();
}

測試方法:

void test1()
{
?? ?cout << combinePath("d:\\temp\\robin\\", "..\\demo\\config.ini") << endl;
?? ?cout << endl;
?
?? ?cout << combinePath("d:", "..\\demo\\config.ini") << endl;
?? ?cout << endl;
?
?? ?cout << combinePath("d:\\temp\\robin", "../demo/config.ini") << endl;
?? ?cout << endl;
?
?? ?cout << combinePath("d:\\temp\\robin", "./config.ini") << endl;
?? ?cout << endl;
?
?? ?cout << combinePath("d:\\temp\\robin", "config.ini") << endl;
?? ?cout << endl;
?
?? ?cout << combinePath("d:\\temp\\robin\\", "\\config.ini") << endl;
?? ?cout << endl;
}

測試發(fā)現(xiàn),release使用O2優(yōu)化,能平均在2~4微秒左右,還是不夠快啊,

用c重新實現(xiàn)一遍

// 字符串范圍內(nèi),逆向查找
const char * ?strrfind(const char *begin, const char *end, const char c)
{
?? ?const char * buf = end;
?? ?while (buf >= begin)
?? ?{
?? ??? ?if (*buf == c)
?? ??? ??? ?return buf;
?? ??? ?buf--;
?? ?}
?? ?return nullptr;
}
size_t ?combinePathC(char ** buffer, const char * dir, const char *name)
{
?? ?int n1 = strlen(dir);
?? ?int n2 = strlen(name);
?? ?size_t len = ?n1 + n2 + 2;
?? ?char *buf = new char[len];
?? ?*buffer = buf;
?
?? ?memcpy(buf, dir, n1);
?? ?size_t len1 = n1;
?
?? ?// 末尾保證有一個"\"
?? ?if (buf[n1-1] == '\\')
?? ?{
?? ??? ?len1 = n1;
?? ?}
?? ?else
?? ?{
?? ??? ?buf[n1] = '\\';
?? ??? ?len1 = n1+1;?? ??? ?
?? ?}
?? ?buf[len1] = '\0';
?
?? ?// len1++示當前拼接的長度
?? ?size_t index = 0;
?? ?char * rPart = (char *)name;
?? ?size_t len2 = n2;
?? ?while (index < n2) ? // 使用index向后滑動,直到末尾
?? ?{
?? ??? ?// 滑動后當前位置
?? ??? ?rPart = (char *)name + index;
?? ??? ?char * tmp = (char *)strchr(rPart, '\\');
?? ??? ?if (tmp == nullptr) ? // end here
?? ??? ?{
?? ??? ??? ?// 拼接剩下的
?? ??? ??? ?len2 = n2 - index;
?? ??? ??? ?memcpy(buf + len1, rPart, len2);
?? ??? ??? ?len1 += len2;
?? ??? ??? ?buf[len1] = '\0';
?? ??? ??? ?len1++;
?
?? ??? ??? ?break;
?? ??? ?}
?
?? ??? ?// 當前找到的長度
?? ??? ?len2 = tmp - rPart;
?? ??? ?if (len2 == 0) ? ? // 遇到 "\config.ini",去掉1個字符
?? ??? ?{
?? ??? ??? ?index += 1;
?? ??? ?}
?? ??? ?else if (len2 == 1 && rPart[0] == '.') ? // 遇到 ".\config.ini",去掉2個字符
?? ??? ?{
?? ??? ??? ?index += 2;
?? ??? ?}
?? ??? ?else if (len2 >= 2 && rPart[0] == '.') ?// 遇到 "..\config.ini", "..x\config.ini"去掉3個字符,末尾去掉一個目錄
?? ??? ?{
?? ??? ??? ?index += len2 + 1;
?? ??? ??? ?const char * back = strrfind(buf, buf + len1 - 2 , '\\'); ? // 從末尾的"\"前一個字符開始找
?? ??? ??? ?if (back == nullptr)
?? ??? ??? ?{
?? ??? ??? ??? ?// dir 當前是這樣的情況,"d:\”
?? ??? ??? ?}
?? ??? ??? ?else if ((back - dir) == 2) ?// dir 當前是這樣的情況,"d:\temp\”
?? ??? ??? ?{
?? ??? ??? ??? ?len1 = 3;
?? ??? ??? ??? ?buf[3] = '\0';
?? ??? ??? ?}
?? ??? ??? ?else ? ? ? ? ? ? ? ? ? ? ? ?// dir 當前是這樣的情況,"d:\temp\test1\”
?? ??? ??? ?{
?? ??? ??? ??? ?len1 = back - buf + 1;
?? ??? ??? ??? ?buf[len1] = '\0';
?? ??? ??? ?}
?? ??? ?}
?? ??? ?else ? ?// ?遇到首字符不為點 "x\config.ini", ?
?? ??? ?{
?? ??? ??? ?index += len2 + 1;
?? ??? ??? ?memcpy(buf + len1, name + index, len2 + 1);
?? ??? ??? ?len1 += len2 + 1;
?? ??? ?}
?? ?}?
?? ?return len1;
}

測試一下:

? ? char * buf = nullptr;
?? ?size_t len;?
?? ?
?? ?len = combinePathC(&buf, "d:\\temp\\robin\\", "config.ini");
?? ?cout << buf << endl;
?
?? ?len = combinePathC(&buf, "d:\\temp\\robin", "config.ini");
?? ?cout << buf << endl;*/
?
?? ?len = combinePathC(&buf, "d:\\temp\\robin", ".\\config.ini");
?? ?cout << buf << endl;
?
?? ?len = combinePathC(&buf, "d:\\temp\\robin", "..\\config.ini");
?? ?cout << buf << endl;
?
?? ?len = combinePathC(&buf, "d:\\temp\\robin", "...\\config.ini");
?? ?cout << buf << endl;

測試發(fā)現(xiàn),平均在0.2微秒左右;

打完收功! 以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++實現(xiàn)趣味掃雷游戲

    C++實現(xiàn)趣味掃雷游戲

    這篇文章主要為大家詳細介紹了C++實現(xiàn)趣味掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • C++中的Reactor原理與實現(xiàn)

    C++中的Reactor原理與實現(xiàn)

    reactor設計模式是event-driven?architecture的一種實現(xiàn)方式,處理多個客戶端并發(fā)的向服務端請求服務的場景,每種服務在服務端可能由多個方法組成,這篇文章主要介紹了Reactor原理與實現(xiàn),需要的朋友可以參考下
    2022-07-07
  • 詳談C語言指針

    詳談C語言指針

    這篇文章主要介紹了C語言的指針,介紹了其相關(guān)概念,然后分享了幾種用法,具有一定參考價值。需要的朋友可以了解下
    2021-10-10
  • C語言中獲取和改變目錄的相關(guān)函數(shù)總結(jié)

    C語言中獲取和改變目錄的相關(guān)函數(shù)總結(jié)

    這篇文章主要介紹了C語言中獲取和改變目錄的相關(guān)函數(shù)總結(jié),包括getcwd()函數(shù)和chdir()函數(shù)以及chroot()函數(shù)的使用方法,需要的朋友可以參考下
    2015-09-09
  • C++迷宮的實現(xiàn)代碼

    C++迷宮的實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了C++實現(xiàn)迷宮游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Qt實現(xiàn)驗證碼相關(guān)功能的代碼示例

    Qt實現(xiàn)驗證碼相關(guān)功能的代碼示例

    驗證碼的原理基于人類視覺和計算機視覺的差異性,通過給用戶顯示一些難以被機器識別的圖形或文字,讓用戶進行人機交互,確認自己的身份,這樣可以有效保護網(wǎng)站安全,所以本給大家介紹了Qt實現(xiàn)驗證碼相關(guān)功能的代碼示例,感興趣的朋友可以參考下
    2024-01-01
  • 深入解析C++中的指針數(shù)組與指向指針的指針

    深入解析C++中的指針數(shù)組與指向指針的指針

    以下是對C++中的指針數(shù)組與指向指針的指針進行了詳細的介紹,需要的朋友可以過來參考下
    2013-09-09
  • C++標準模板庫STL深入講解

    C++標準模板庫STL深入講解

    STL提供了一組表示容器、迭代器、函數(shù)對象和算法的模板。容器是一個與數(shù)組類似的單元,可以存儲若干個值。STL容器是同質(zhì)的,即存儲的值的類型相同:算法是完成特定任務(如對數(shù)組進行排序或在鏈表中查找特定值)的處方
    2022-12-12
  • C++ 中的INT_MAX,INT_MIN數(shù)值大小操作

    C++ 中的INT_MAX,INT_MIN數(shù)值大小操作

    這篇文章主要介紹了C++ 中的INT_MAX,INT_MIN數(shù)值大小操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • C++獲取本地時間常見方法匯總

    C++獲取本地時間常見方法匯總

    這篇文章主要介紹了C++獲取本地時間的常見方法,幫助大家更好的理解和學習C++,感興趣的朋友可以了解下
    2020-08-08

最新評論

买车| 饶河县| 团风县| 陆川县| 达孜县| 丹阳市| 沅陵县| 玛多县| 襄城县| 遵义县| 且末县| 宿迁市| 藁城市| 石家庄市| 四川省| 灌阳县| 吴堡县| 麦盖提县| 固阳县| 万全县| 南江县| 汝城县| 余江县| 龙口市| 绍兴市| 泸水县| 凭祥市| 高安市| 金坛市| 清镇市| 池州市| 藁城市| 安阳市| 河东区| 苍梧县| 沿河| 贺州市| 霍林郭勒市| 齐齐哈尔市| 白银市| 岑溪市|