stdstring

How can I format a std::string using a collection of arguments?

大憨熊 提交于 2020-06-09 16:49:48
问题 Is it possible to format std::string passing a set of arguments? Currently I am formatting the string this way: string helloString = "Hello %s and %s"; vector<string> tokens; //initialized vector of strings const char* helloStringArr = helloString.c_str(); char output[1000]; sprintf_s(output, 1000, helloStringArr, tokens.at(0).c_str(), tokens.at(1).c_str()); But the size of the vector is determined at runtime. Is there any similar function to sprintf_s which takes a collection of arguments

How to check if a string contains a char?

跟風遠走 提交于 2020-05-08 06:09:28
问题 I have a text file that I want to read. I want to know if one of the lines contains [ so I tried : if(array[i] == "[") But this isn't working. How can I check if a string contains a certain character? 回答1: Look at the documentation string::find std::string s = "hell[o"; if (s.find('[') != std::string::npos) ; // found else ; // not found 回答2: If the array is char* array or char array[] , you can find a char through a while loop: while(i < nSize) if (array[i] == '[') Note: '[' is the correct

Is this “elision failure” language-mandated?

自闭症网瘾萝莉.ら 提交于 2020-04-13 03:53:08
问题 Consider the following code: #include <utility> #include <string> int bar() { std::pair<int, std::string> p { 123, "Hey... no small-string optimization for me please!" }; return p.first; } (simplified thanks to @Jarod42 :-) ...) I expect the function to be implemented as simply: bar(): mov eax, 123 ret but instead, the implementation calls operator new() , constructs an std::string with my literal, then calls operator delete() . At least - that's what gcc 9 and clang 9 do (GodBolt). Here's

Visual Studio debugger doesn't display std::string properly in debug

落爺英雄遲暮 提交于 2020-01-23 07:50:30
问题 Me and my team are having an issue with Visual Studio displaying gibberish instead of proper string contents when I pause my program. The string inside has correct data, it's just that the debugger of VS gets lost somehow. I marked the correct contents in green, incorrect in red. You can see that the std::string defined as const std::string testStdString = "contents of std::string"; Displays as "\bÄĎD\x19" in debug hover and watch window. But the C-string from .c_str() displays fine. Console

Visual Studio debugger doesn't display std::string properly in debug

拈花ヽ惹草 提交于 2020-01-23 07:50:20
问题 Me and my team are having an issue with Visual Studio displaying gibberish instead of proper string contents when I pause my program. The string inside has correct data, it's just that the debugger of VS gets lost somehow. I marked the correct contents in green, incorrect in red. You can see that the std::string defined as const std::string testStdString = "contents of std::string"; Displays as "\bÄĎD\x19" in debug hover and watch window. But the C-string from .c_str() displays fine. Console

Does “&s[0]” point to contiguous characters in a std::string?

依然范特西╮ 提交于 2020-01-18 03:24:04
问题 I'm doing some maintenance work and ran across something like the following: std::string s; s.resize( strLength ); // strLength is a size_t with the length of a C string in it. memcpy( &s[0], str, strLength ); I know using &s[0] would be safe if it was a std::vector, but is this a safe use of std::string? 回答1: A std::string's allocation is not guaranteed to be contiguous under the C++98/03 standard, but C++11 forces it to be. In practice, neither I nor Herb Sutter know of an implementation

When does std::string reallocate memory?

元气小坏坏 提交于 2020-01-14 12:57:10
问题 When using an std::string object and I want to add characters to it, would it preallocate some memory, or would it only allocate as much as I need? To be precise: std::string s; s.reserve(20); char c = 'a'; s = ""; for(int i = 0; i < 25; i++) s += c; In the above example I reserve an amount of memory. Now when I clear the string, will it cause the reserved memory to be discarded? In the loop would it fill up the reserved memory and then reallocate for the extra 5 characters each time? 回答1:

When does std::string reallocate memory?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-14 12:57:06
问题 When using an std::string object and I want to add characters to it, would it preallocate some memory, or would it only allocate as much as I need? To be precise: std::string s; s.reserve(20); char c = 'a'; s = ""; for(int i = 0; i < 25; i++) s += c; In the above example I reserve an amount of memory. Now when I clear the string, will it cause the reserved memory to be discarded? In the loop would it fill up the reserved memory and then reallocate for the extra 5 characters each time? 回答1:

C++ convert UnicodeString into String

孤人 提交于 2020-01-04 15:57:50
问题 Please help me somebody to convert unicodestring into string This is how i am getting unicodestring UnicodeString _str = OpenDialog1->FileName; Or if it possible to write into file unicode string with ifstream or something like that? Thanks 回答1: Depending on your needs, assign the UnicodeString to an AnsiString or a UTF8String , and then write that to your file instead of the original UnicodeString itself: UnicodeString _str = OpenDialog1->FileName; AnsiString _astr = _str; Or: UnicodeString

returning const char* to char* and then changing the data

a 夏天 提交于 2020-01-04 03:04:47
问题 I am confused about the following code: string _str = "SDFDFSD"; char* pStr = (char*)_str.data(); for (int i = 0; i < iSize; i++) pStr[i] = ::tolower(pStr[i]); here _str.data() returns const char* . But we are assigning it to a char* . My questions is, _str.data() is returning pointer to a constant data. How is it possible to store it in a pointer to data? The data was constant right? If we assign it to char pointer than we can change it like we are doing inside the for statement which should