簡體   English   中英

當然我正在做一些非常錯誤的提升:replace_all。 請進行理智檢查?

[英]Sure I'm doing something very wrong with boost:replace_all. Sanity check please?

我想要一種有效的方法來執行搜索並替換字符串(實際上它是一個着色器字符串),所以我做了一些研究並提出了boost :: replace_all。 我的函數傳遞一個字符串和一個或多個向量。 每對是一對弦; 搜索並用字符串替換。 我想迭代這組替換,修改進程中的字符串。 這是我想出的:

void 
Shader::Read(ShaderTypes type, std::string const & shader, std::vector<std::pair<std::string, std::string>> const & replace)
{
    // Perform search and replace on input string.

    std::string newShader = shader;

    std::for_each(replace.begin(), replace.end(), [newShader](std::pair<std::string, std::string> const & pair) {
        boost::replace_all(newShader, pair.first, pair.second);
    });




    // Create and compile shader.

    Read(type, newShader);
}

現在這不會編譯。 我從提升中得到了一些錯誤。 我認為這與對象是const有關,但我不是100%肯定。 當我嘗試手動創建一個std ::對並用它調用replace_all時,它有效。 不喜歡它從lambda到達的形式。

有人可以幫我嗎?

您應該通過引用捕獲newShader

std::for_each(replace.begin(), replace.end(), [&newShader](...

我很確定newShader應該通過引用傳遞給lambda。 而且您不必提供其名稱,您可以使用[&]自動捕獲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM