[英]Why doesn't string class take in two words separated with space?
我想使用字符串 object 獲取人名。但是在我的代碼中,如果我將兩部分名稱用空格分隔,則只顯示第一部分。 我的理解是.c_str()返回一個指向存儲字符串的指針,終端為null。為什么空間有問題。 我是 C++ 的新手,使用的是 Code::Blocks 13.12。 這是我在我編寫的另一個程序 ...
[英]Why doesn't string class take in two words separated with space?
我想使用字符串 object 獲取人名。但是在我的代碼中,如果我將兩部分名稱用空格分隔,則只顯示第一部分。 我的理解是.c_str()返回一個指向存儲字符串的指針,終端為null。為什么空間有問題。 我是 C++ 的新手,使用的是 Code::Blocks 13.12。 這是我在我編寫的另一個程序 ...
[英]Accidentally linking c_str() strings
我正在嘗試將一個字符串分配給一個結構體值,該值有效,但該值以某種方式鏈接到變量: 輸出是: 有人可以解釋為什么會這樣嗎? ...
[英]Curious behaviour of c_str() and strings when passed to class
在使用 c-strings 和 std::string 時,我遇到了一個奇怪的行為(我相信它只是對我很好奇,並且存在一個完全有效的 c++ 答案)。 通常,當我將字符串傳遞給類的構造函數時,我會執行以下操作: class Foo { public: Foo(const std::string&a ...
[英]Getting char * or const char * data from a string breaks for 16 character strings or longer
我有一個 function string_to_char()試圖給我一個字符串的形式,我可以將它傳遞到我正在使用的庫中,它需要char * (但我認為適用於const char * ,所以我一直在嘗試兩者) . 我為測試string_to_char()的實現而編寫的代碼如下: 這是我得到的 outp ...
[英]c_str() is terminating my encrypted message is there a way to bypass this?
我正在使用 RSA 加密消息。 如果我嘗試在本地加密/解密消息,一切正常,但是,當將字符串轉換為void constant*以便像這樣使用socket::send方法時, 發生以下情況, 我的原始字符串tempMessage “_��JE<D�p�-d�R�U���`}�+�;����|[ ...
[英]Is it safe to use std::string::c_str() to modify the underlying std::string?
我遇到了一些代碼,其中包含以下示例的幾個實例: 在這個簡化的示例中,使用const_cast將返回const char*的std::string::c_str()賦值給char*指針。 這似乎按預期工作,並相應地修改了str 。 但是我本地庫中std::string::c_str()的描述如 ...
[英]Is there a dangling pointer problem in this code?
這段代碼對我來說工作正常,但在其他每個地方我都看到了這段代碼 我想知道哪個是正確的,為什么? ...
[英]The lifetime of pointer that point to c_str function in std::string
首先列出的代碼如下。 運行它,結果如下。 (Linux gcc) cs = 123456 cs = 123456 所以,我不知道為什么在s被銷毀后cs指針是有效的。 換句話說,指向 std::string 中 c_str function 的指針的生命周期。 ...
[英]Boost.Program_Options: Why does options_description_easy_init::operator() not have an overload for std::string?
考慮這個 MCVE: 我正在嘗試編寫一個方便的函數,以在將類似選項插入options_description對象時減少 C&P 代碼的數量(原始代碼使用通知程序,為了簡單起見,這些通知程序已被刪除,但添加了更多樣板文件)。 令我驚訝的是,沒有接受std::string options_d ...
[英]Strchr and strrchr returning same result
出於某種原因,我是否嘗試strchr或strrchr ,我得到返回的值相同。 我不知道為什么。 這是給我帶來麻煩的代碼段: 輸入是Jane Austen, 6 ,我試圖將它分成兩個字符串:一個在逗號之前,一個在逗號之后。 但是,我使用strrchr(input, ','); 或strchr(i ...
[英]Can you safely get a pointer to a string from its c_str() const char*?
我有一個 const char 指針,我知道它肯定來自一個字符串。 例如: 在我的情況下,我知道 myCstring 來自一個字符串,但我不再有權訪問該字符串(我從 function 調用中收到了 const char*,我無法修改函數的參數列表)。 鑒於我知道 myCstring 指向現有字符串 ...
[英]c_str() is only reading half of my string, why? How can I fix this? Is it a byte issue?
我正在編寫一個客戶端程序和服務器程序。 在服務器程序上,為了將結果寫回到客戶端,我必須將字符串轉換為const char *,以將其放入const void *變量中才能使用write()函數。 當我檢查時,字符串本身會輸出正確的結果,但是當我在字符串上使用c_str()函數時,它只會一直輸 ...
[英]C++: Why does this string input fail while the other does not
我從朋友那里得到這個問題 我的第一個觀察結果是,當傳遞給input的單詞數大於3個單詞時, riddle()函數不會產生垃圾。 我仍在嘗試查看為什么在第一種情況下而不是第二種情況下失敗。 無論如何,分享這很有趣。 ...
[英]format float in C++ mfc
我用VS2010在windows 7下搭建了一個MFC程序,我想對生成的報表中的浮點數據進行格式化。 我曾嘗試使用 %f,但報告中顯示了“%f”。 預期成績: 通過, 1.2, 3.4, 5.67 實際結果: 通過,%f,%f,%f ...
[英]Is c_str() or reinterpret_cast better for working with binary files?
我需要在程序中處理二進制文件,並且我已經看到使用了 reinterpret_cast 以及 c_str()。 這是使用 c_str() 的代碼片段: 這是使用 reinterpret_cast 的代碼片段: 當我寫入使用 reinterpret_cast 的二進制文件時,我得到了胡言亂語. ...
[英]c_str() vs. data() when it comes to return type
在C ++ 11之后,我想到了c_str()和data() 等效 。 C ++ 17為后者引入了一個重載,返回一個非常量指針( 引用 ,我不確定它是否完全在C ++ 17中更新): c_str()只返回一個常量指針: 為什么在C ++ 17中區分這兩種方法,特別是當C ++ ...
[英]After recompiling our FORTRAN-code and using it in C++ our system() or c_str() command don`t work properly
嗨,我有個大問題:幾年前,我們在C ++ / Qt 4.8.4 / Qt Creator 2.8.1中創建了一個程序,該程序在執行時會運行另一個可執行文件(用FORTRAN編寫和編譯)。 一切正常。 我們用新版本的Visual Studio重新編譯了Fortran代碼,現在突然不起作用了 ...
[英]Char pointer doesn't initialize as expected
MyClass.h MyClass.cpp Main.cpp的 我期望程序輸出Text_5 ,而是輸出: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ...
[英]Converting strstream to sstream conflict about c_str()
我有這個用strstream編寫的代碼塊。 我將其轉換為sstream ,如下所示。 我不確定,但是我認為printStream->str()返回一個字符串對象,該對象帶有printStream指向的流緩沖區中的內容的副本(臨時),然后您在其上調用c_str()並獲取const cha ...
[英]Difference in c_str function specification between C++03 and C++11
在std::string的c_str()的C ++ 引用中 ,出現以下內容: 返回值 指向底層字符存儲的指針。 data()[i] == operator[](i) for every i in [0, size()) (直到C ++ 11) data() + i == & ...