![](/img/trans.png)
[英]What is the correct way to “clear" a std::string_view?
我看到這篇文章: c++ - 為什么 std::string_view 沒有 assign() 和 clear() 方法? - 堆棧溢出,因此string_view不包含clear的 function。 但在我的例子中,我有一個string_view作為 class 成員變量,有時,我想將它重置 ...
[英]What is the correct way to “clear" a std::string_view?
我看到這篇文章: c++ - 為什么 std::string_view 沒有 assign() 和 clear() 方法? - 堆棧溢出,因此string_view不包含clear的 function。 但在我的例子中,我有一個string_view作為 class 成員變量,有時,我想將它重置 ...
[英]Why does std::string store content at the same address inside a loop?
我需要動態創建一個 char[] 。 我想使用 std::string 而不是直接處理 char[],因為我不需要使用 std::string 處理 memory 管理。 這是我的示例代碼: output 是: ,這不是我想要的。 在我的理解中,循環內部創建的字符串s在每次迭代中應該都是全新 ...
[英]How to accept an unknown number of lines in c++, each line has two strings
如何接受 c++ 中的未知行數? 每行有兩個字符串,中間用空格分隔。 我嘗試了This cplusplus forum中提到的解決方案,但沒有一個解決方案對我有用。 只有在每行末尾按下Enter時,其中一種解決方案才有效。 我不確定是否會在輸入行的末尾給出\n字符。 我有哪些選擇? 我目前的嘗試要求 ...
[英]std::string formatting like sprintf / printf and also allow for no arguments
我正在嘗試解決這樣一個問題: std::string formatting like sprintf: std::string formatting like sprintf 然而,我遇到的問題是我想讓它適用於只有常量字符串而沒有額外參數的情況:`string_format("just a str ...
[英]Does creating a std::string in a function and returning it create a memory leak?
調用此函數是否會造成內存泄漏? 我讀過“價值語義”,但我無法想象。 ...
[英]How to convert char* into std::u8string?
介紹如果我捕獲到異常,我想將錯誤消息(通過what()方法作為 C 風格字符串返回)轉換為std::u8string (UTF-8 字符串)。 例如: std::u8string(error.what()); 問題如何將char*轉換為std::u8string ? 附加信息我只捕獲來自標准庫、b ...
[英]How to get the variable values of __malloc_printerr__ without debug-info?
我有一個 C++ 程序,它使用 gcc4.4.5 編譯並在 redhat 6.1 上運行。 程序用-m64編譯,CPU為Intel-64。 它總是在幾天后(隨機)中止。 由於找不到redhat6.1 的debug-info 文件,我不得不反匯編function malloc_pinterr來嘗試找 ...
[英]Concept for constraining parameter pack to string-like types or types convertible to string
我正在拼湊一個 C++20 難題。 這是我想要做的: Function append_params會將 url 與其他查詢參數連接在一起。 為了以動態和可擴展的方式進行設計,我想寫一個這樣的概念它允許可以從中構造 std::string 的類型它允許使用 std::to_string() 將類型轉換 ...
[英]Disabling a constructor using std::enable_if
我的目標是創建我自己的std::basic_string類似物,但有一些附加條件。 我希望我的AnyString<CharType, Traits>可以從std::basic_string<CharType, AnyOtherTraits, AnyAlloc>但我想為某些 C ...
[英]How to initialize custom analogue for std::cout?
我正在嘗試實現自己的basic_string ,但在打印字符串時遇到了問題。 我不會使用std::char_traits和 std 的其他特征,因為我自己已經實現了。 如何創建可用於我的字符串的std::cout的直接模擬並為此使用std::basic_ostream ? (不會自己創建basi ...
[英]Why does std::basic_string have two separate template parameters _Elem (char type) and _Traits (char traits)?
問題是我不明白為什么這些應該分開。 為什么不使用一個 class,如 CharType,它包含 char 特征和 char 類型的邏輯。 我的意思是替換它: 接着就,隨即: 其中 ExtendedTraits 是 _Elem 和 _Traits 的預置組合,可能看起來像這樣: 我嘗試實施這兩種方法 ...
[英]Custom data type for boost property tree (inheriting string)
我知道這個字符串業務是個壞主意,但無論如何我都必須知道它: 所以我想使用屬性樹讀取 xml ,同時將它存儲在我自己的數據類型中:字符串的子項。 這是我嘗試過的,但沒有運氣: 如果你有提升,代碼是可運行的。 這些是編譯時拋出的錯誤: #1- /opt/homebrew/include/boost/ ...
[英]How to use std::string_view with reverse iterators?
我正在嘗試創建一個非常簡單的 function: 為了避免不必要的分配,我想我可以使用 string_view: 然而,最后一個 function 編譯失敗,因為編譯器找不到合適的構造函數(我嘗試了 g++ 12.2 和 clang++ 15.0)。 為什么在這種情況下沒有構造函數 while s ...
[英]I do not understand std::string from std::stream
盡管我害怕顯得很愚蠢,但我想問一下為什么這個程序:#include <iostream> #include <fstream> int main(int argc, char * argv[]) { std::ifstream fitData("./fitData. ...
[英]What is the new feature about constexpr std::string with gcc 12.1
情況1:#include <string> inline constexpr std::string test(std::string s) noexcept { return s + "xxx"; } int main() { auto s = test("abc" ...
[英]std::cin>> is digit or string
我必須確定輸入是數字還是字符串。 為此我收到error: no matching function for call to 'isdigit(std::__cxx11::string&)'有人可以提出我應該使用的方法嗎? ...
[英]Revers went wrong and returns the same number
我想我無法理解我的周期邏輯中的某些東西,你不能匹配我的錯誤嗎? 我們需要顛倒數字。 我的代碼: ...
[英]How to efficiently pass a string to a function if I will certainly copy it? C++20
我想知道如何正確處理 function 中的字符串輸入,如果我知道我必須在其中制作一個副本以將其放入容器中。 我正在考慮這樣做: 但是,如果我傳遞一個 char* 字符串,我將制作 2 個副本,首先是在調用 foo 時,然后在將其傳遞給容器時。 我的第二個想法是使用 string_view,正如人 ...
[英]Copy a part of an std::string in a char* pointer
假設我在 C++ 中有這個代碼片段 我需要復制str中的字符串data (第一個和最后一個字符除外)。 我似乎可行的解決方案是創建一個 substring 然后像這樣執行std::copy操作 但也許這有點矯枉過正,因為我創建了一個新字符串。 有沒有更簡單的方法來實現這個目標? 也許在std:cop ...
[英]VS2022: std::string.c_str() Dangling pointer warning
我從 VS2022 收到此代碼片段的懸空指針警告: 警告 getProgramUUID()返回一個std::string並定義為 基本上,這只是c_str()返回的指針的memcpy 。 知道這是一個真正的問題,還是這只是 VS 迂腐? 問候, 阿布舍克 ...