stdstring

Is there a way to get std:string's buffer

非 Y 不嫁゛ 提交于 2019-11-30 01:15:05
问题 Is there a way to get the "raw" buffer o a std::string? I'm thinking of something similar to CString::GetBuffer() . For example, with CString I would do: CString myPath; ::GetCurrentDirectory(MAX_PATH+1, myPath.GetBuffer(MAX_PATH)); myPath.ReleaseBuffer(); So, does std::string have something similar? 回答1: Use std::vector<char> if you want a real buffer. #include <vector> #include <string> int main(){ std::vector<char> buff(MAX_PATH+1); ::GetCurrentDirectory(MAX_PATH+1, &buff[0]); std::string

C++ addition overload ambiguity

社会主义新天地 提交于 2019-11-29 16:04:17
I am coming up against a vexing conundrum in my code base. I can't quite tell why my code generates this error, but (for example) std::string does not. class String { public: String(const char*str); friend String operator+ ( const String& lval, const char *rval ); friend String operator+ ( const char *lval, const String& rval ); String operator+ ( const String& rval ); }; The implementation of these is easy enough to imagine on your own. My driver program contains the following: String result, lval("left side "), rval("of string"); char lv[] = "right side ", rv[] = "of string"; result = lv +

Default capacity of std::string?

早过忘川 提交于 2019-11-29 13:57:23
When I create a std::string using the default constructor, is ANY memory allocated on the heap? I'm hoping the answer does not depend on the implementation and is standardized. Consider the following: std::string myString; Unfortunately, the answer is no according to N3290. Table 63 Page 643 says: data() a non-null pointer that is copyable and can have 0 added to it size() 0 capacity() an unspecified value The table is identical for C++03. It is implementation dependent. Some string implementations use a small amount of automatically allocated storage for small strings, and then dynamically

how to put std::string into boost::lockfree::queue (or alternative)?

坚强是说给别人听的谎言 提交于 2019-11-29 13:38:26
I'm trying to put std::string s into boost::lockfree::queue s so that my threads can update each other with new data. When I try to use boost::lockfree::queue<std::string> updated_data; , g++ says : In instantiation of 'class boost::lockfree::queue >': error: static assertion failed: (boost::has_trivial_destructor::value) error: static assertion failed: (boost::has_trivial_assign::value) I've been shown generally what these errors mean , but I have no hope of ever fixing this myself, as I'm almost brand new to c++. Is there an alternative way to pass text data between threads with lockfree ?

Is it bad to depend on index 0 of an empty std::string?

狂风中的少年 提交于 2019-11-29 10:35:47
问题 std::string my_string = ""; char test = my_string[0]; I've noticed that this doesn't crash, and every time I've tested it, test is 0. Can I depend on it always being 0? or is it arbitrary? Is this bad programming? Edit: From some comments, I gather that there is some misunderstanding about the usefulness of this. The purpose of this is NOT to check to see if the string is empty. It is to not need to check whether the string is empty. The situation is that there is a string that may or may not

C++/CLI String Conversions

南楼画角 提交于 2019-11-29 08:43:05
I found this really nice piece of code that converts a string to a System:String^ as in: System::String^ rtn = gcnew String(move.c_str()); // 'move' here is the string I'm passing rtn back to a C# program. Anyways, inside the function where this code exists, I'm passing in a System::String^ . I also found some code to convert a System:String^ to a string using the following code: pin_ptr<const wchar_t> wch = PtrToStringChars(cmd); // 'cmd' here is the System:String size_t convertedChars = 0; size_t sizeInBytes = ((cmd->Length + 1) * 2); errno_t err = 0; char *ch = (char *)malloc(sizeInBytes);

Append int to std::string

倖福魔咒の 提交于 2019-11-29 05:24:46
I tried two different ways to append an int to a std::string , and to my surprise, I got different results: #include <string> int main() { std::string s; s += 2; // compiles correctly s = s + 2; // compiler error return 0; } Why does it compile and work correctly when I use the += operator, but fail when I use the + operator? I don't think the question is like How to concatenate a std::string and an int? In that question,no answer uses += operator.And the difference between += and + operator of std::string is the key to solve my doubt. Frankly,the question is a good example for explaining why

Are end+1 iterators for std::string allowed?

Deadly 提交于 2019-11-28 23:09:08
Is it valid to create an iterator to end(str)+1 for std::string ? And if it isn't, why isn't it? This question is restricted to C++11 and later, because while pre-C++11 the data was already stored in a continuous block in any but rare POC toy-implementations, the data didn't have to be stored that way. And I think that might make all the difference. The significant difference between std::string and any other standard container I speculate on is that it always contains one element more than its size , the zero-terminator, to fulfill the requirements of .c_str() . 21.4.7.1 basic_string

Storing unicode UTF-8 string in std::string

这一生的挚爱 提交于 2019-11-28 21:33:33
In response to discussion in Cross-platform strings (and Unicode) in C++ How to deal with Unicode strings in C/C++ in a cross-platform friendly way? I'm trying to assign a UTF-8 string to a std::string variable in Visual Studio 2010 environment std::string msg = "महसुस"; However, when I view the string view debugger, I only see "?????" I have the file saved as Unicode (UTF-8 with Signature) and i'm using character set "use unicode character set" "महसुस" is a nepali language and it contains 5 characters and will occupy 15 bytes. But visual studio debugger shows msg size as 5 My question is: How

C++ strange behavior with string's c_str() function

一曲冷凌霜 提交于 2019-11-28 14:33:40
I am moving my project from Visual Studio 06 to 2010. While doing so, I have observed this behavior in my code. I have a Get string function that look like this: string GetTheStr() { return strSomeStdString; } Then there is another function that call above get function like this: const char* ptrStr = (char *)GetTheStr().c_str(); the value of string pointed by ptrStr is "" above code was working fine in visual studio 06 but not on visual studio 2010. Then I tried few experiments: std::string str = GetTheStr(); // -> value inside str displayed correctly const char* PtrCStr = str.c_str(); // ->