stdstring

What's the difference between std::string::c_str and std::string::data? [duplicate]

元气小坏坏 提交于 2019-12-03 12:27:54
问题 This question already has answers here : string c_str() vs. data() (6 answers) Closed 5 years ago . Why would I ever want to call std::string::data() over std::string::c_str()? Surely there is some method to the standard's madness here... 回答1: c_str() guarantees NUL termination. data() does not. 回答2: c_str() return a pointer to the data with a NUL byte appended so you can use the return value as a "C string". data() returns a pointer to the data without any modifications. Use c_str() if the

C++ Program Always Crashes While doing a std::string assign

若如初见. 提交于 2019-12-03 12:13:09
问题 I have been trying to debug a crash in my application that crashes (i.e. asserts a * glibc detected * free(): invalid pointer: 0x000000000070f0c0 ***) while I'm trying to do a simple assign to a string. Note that I'm compiling on a linux system with gcc 4.2.4 with an optimization level set to -O2. With -O0 the application no longer crashes. E.g. std::string abc; abc = "testString"; but if I changed the code as follows it no longer crashes std::string abc("testString"); So again I scratched my

Setting an std::string variable value from gdb?

↘锁芯ラ 提交于 2019-12-03 11:03:35
Is it possible... when the debugger is stopped at a breakpoint, to modify the value of a std::string variable without resorting to hacks like tweaking the memory image of the current buffer? e.g. something like "set var mystring="hello world" ? Try this (tested and works for me): call mystring.assign("hello world") The key is that instead of modifying memory directly, you call the object's functions to change its state. It so happens that std::basic_string has a member function called assign which does the job. 来源: https://stackoverflow.com/questions/2502314/setting-an-stdstring-variable-value

Difference between string += s1 and string = string + s1 [closed]

纵然是瞬间 提交于 2019-12-03 10:39:09
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 months ago . One of my programs is exceeding the time limit when I am using fans = fans + s[i] , while when I am using fans += s[i] it is being accepted... Why does this happen? To Explain more , fans is a string and s is also a string so while iterating over string s i want only some characters of s so i am creating a new

Initialize std::string from a possibly NULL char pointer

ぐ巨炮叔叔 提交于 2019-12-03 09:47:39
Initializing std::string from a NULL char pointer is undefined behaviour, I believe. So, here are alternative versions of a constructor, where mStdString is a member variable of type std::string : void MyClass::MyClass(const char *cstr) : mStdString( cstr ? cstr : "") {} void MyClass::MyClass(const char *cstr) : mStdString(cstr ? std::string(cstr) : std::string()) {} void MyClass::MyClass(const char *cstr) { if (cstr) mStdString = cstr; // else keep default-constructed mStdString } Edit, constructor declaration inside class MyClass : MyClass(const char *cstr = NULL); Which of these, or

How to cleanse (overwrite with random bytes) std::string internal buffer?

梦想与她 提交于 2019-12-03 09:34:11
Consider a scenario, where std::string is used to store a secret . Once it is consumed and is no longer needed, it would be good to cleanse it, i.e overwrite the memory that contained it, thus hiding the secret . std::string provides a function const char* data() returning a pointer to (since C++11) continous memory. Now, since the memory is continous and the variable will be destroyed right after the cleanse due to scope end, would it be safe to: char* modifiable = const_cast<char*>(secretString.data()); OpenSSL_cleanse(modifiable, secretString.size()); According to standard quoted here: $5.2

Why does std::string(“\\x00”) report length of 0?

泪湿孤枕 提交于 2019-12-03 05:27:15
I have a function which needs to encode strings, which needs to be able to accept 0x00 as a valid 'byte'. My program needs to check the length of the string, however if I pass in "\x00" to std::string the length() method returns 0. How can I get the actual length even if the string is a single null character? You're passing in an empty string. Use std::string(1, '\0') instead. Or std::string{ '\0' } (thanks, @zett42) std::string is perfectly capable of storing nulls. However, you have to be wary, as const char* is not, and you very briefly construct a const char* , from which you create the

getting cout output to a std::string

别等时光非礼了梦想. 提交于 2019-12-03 04:17:22
I have the following cout statement. I use char arrays because I have to pass to vsnprintf to convert variable argument list and store in Msg . Is there any way we can get cout output to C++ std::string ? char Msg[100]; char appname1[100]; char appname2[100]; char appname3[100]; // I have some logic in function which some string is assigned to Msg. std::cout << Msg << " "<< appname1 <<":"<< appname2 << ":" << appname3 << " " << "!" << getpid() <<" " << "~" << pthread_self() << endl; Björn Pollex You can replace cout by a stringstream . std::stringstream buffer; buffer << "Text" << std::endl;

How to efficiently get a `string_view` for a substring of `std::string`

☆樱花仙子☆ 提交于 2019-12-03 04:02:21
问题 Using http://en.cppreference.com/w/cpp/string/basic_string_view as a reference, I see no way to do this more elegantly: std::string s = "hello world!"; std::string_view v = s; v = v.substr(6, 5); // "world" Worse, the naive approach is a pitfall and leaves v a dangling reference to a temporary: std::string s = "hello world!"; std::string_view v(s.substr(6, 5)); // OOPS! I seem to remember something like there might be an addition to the standard library to return a substring as a view: auto v

C++: how to convert ASCII or ANSI to UTF8 and stores in std::string

怎甘沉沦 提交于 2019-12-03 03:59:50
My company use some code like this: std::string(CT2CA(some_CString)).c_str() which I believe it converts a Unicode string (whose type is CString)into ANSI encoding, and this string is for a email's subject. However, header of the email (which includes the subject) indicates that the mail client should decode it as a unicode (this is how the original code does). Thus, some German chars like "ä ö ü" will not be properly displayed as the title. Is there anyway that I can put this header back to UTF8 and store into a std::string or const char*? I know there are a lot of smarter ways to do this,