stdstring

convert standard C++ string to String^

南笙酒味 提交于 2019-11-28 14:26:00
I want to convert to std::string to System::String^ in Visual C++ environment. I know that we can convert System::String to std::string by the MarshalString Function as below: void MarshalString ( String ^ s, string& os ) { using namespace Runtime::InteropServices; const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer(); os = chars; Marshal::FreeHGlobal(IntPtr((void*)chars)); } I can't find the way to convert std::string to System::String but I found that System::String has constructor with argument as below : System::String(Char* value, Int32 startIndex, Int32 length)

How do I construct a std::string from a DWORD?

百般思念 提交于 2019-11-28 13:17:37
I have following code: Tools::Logger.Log(string(GetLastError()), Error); GetLastError() returns a DWORD a numeric value, but the constructor of std::string doesn't accept a DWORD . What can I do? You want to read up on ostringstream: #include <sstream> #include <string> int main() { std::ostringstream stream; int i = 5; stream << i; std::string str = stream.str(); } Johannes Schaub - litb You want to convert the number to a string : std::ostringstream os; os << GetLastError(); Log(os.str(), Error); Or boost::lexical_cast : Log(boost::lexical_cast<std::string>(GetLastError()), Error); Since C+

How do I convert wchar_t* to std::string?

喜欢而已 提交于 2019-11-28 09:37:34
I changed my class to use std::string (based on the answer I got here but a function I have returns wchar_t *. How do I convert it to std::string? I tried this: std::string test = args.OptionArg(); but it says error C2440: 'initializing' : cannot convert from 'wchar_t *' to 'std::basic_string<_Elem,_Traits,_Ax>' You could just use wstring and keep everything in Unicode wstring ws( args.OptionArg() ); string test( ws.begin(), ws.end() ); You can convert a wide char string to an ASCII string using the following function: #include <locale> #include <sstream> #include <string> std::string ToNarrow

Encode/Decode std::string to UTF-16

感情迁移 提交于 2019-11-28 09:21:57
I have to handle a file format (both read from and write to it) in which strings are encoded in UTF-16 (2 bytes per character). Since characters out of the ASCII table are rarely used in the application domain, all of the strings in my C++ model classes are stored in instances of std::string (UTF-8 encoded). I'm looking for a library (searched in STL and Boost with no luck) or a set of C/C++ functions to handle this std::string <-> UTF-16 conversion when loading from or saving to file format (actually modeled as a bytestream) including the generation/recognition of surrogate pairs and all that

Value and size of an uninitialized std::string variable in c++

荒凉一梦 提交于 2019-11-28 08:57:48
If a string is defined like this std::string name; What will be the value of the uninitialized string "name" and what size it would be? Because it is not initialized, it is the default constructor that is called. Then : empty string constructor (default constructor) : Constructs an empty string, with a length of zero characters. Take a look : http://www.cplusplus.com/reference/string/string/string/ EDIT : As stated in C++11, §21.4.2/1 : Effects: Constructs an object of class basic_string . The postconditions of this function are indicated in Table 63. -> Table 63 +------------------------------

Default capacity of std::string?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 07:28:01
问题 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; 回答1: 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. 回答2: It is implementation dependent. Some string

Convert a number to a string with specified length in C++

浪子不回头ぞ 提交于 2019-11-28 04:51:53
I have some numbers of different length (like 1, 999, 76492, so on) and I want to convert them all to strings with a common length (for example, if the length is 6, then those strings will be: '000001', '000999', '076492'). In other words, I need to add correct amount of leading zeros to the number. int n = 999; string str = some_function(n,6); //str = '000999' Is there a function like this in C++? xtofl or using the stringstreams: #include <sstream> #include <iomanip> std::stringstream ss; ss << std::setw(10) << std::setfill('0') << i; std::string s = ss.str(); I compiled the information I

How to convert std::string to NSString?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 03:37:50
Hi I am trying to convert a standard std::string into an NSString but I'm not having much luck. I can convert successfully from an NSString to a std::string with the following code NSString *realm = @"Hollywood"; std::string REALM = [realm cStringUsingEncoding:[NSString defaultCStringEncoding]]; However I get a compile time error when I try the following NSString *errorMessage = [NSString stringWithCString:REALM encoding:[NSString defaultCStringEncoding]]; The error I get is Cannot convert 'std::string' to 'const char*' in argument passing Am I missing something here? Thanks in advance. Get c

What is the point of STL Character Traits?

偶尔善良 提交于 2019-11-28 02:41:16
I notice that in my copy of the SGI STL reference, there is a page about Character Traits but I can't see how these are used? Do they replace the string.h functions? They don't seem to be used by std::string , e.g. the length() method on std::string doesn't make use of the Character Traits length() method. Why do Character Traits exist and are they ever used in practice? Character traits are an extremely important component of the streams and strings libraries because they allow the stream/string classes to separate out the logic of what characters are being stored from the logic of what

Can a std::string contain embedded nulls?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 22:40:15
For regular C strings, a null character '\0' signifies the end of data. What about std::string , can I have a string with embedded null characters? Yes you can have embedded nulls in your std::string . Example: std::string s; s.push_back('\0'); s.push_back('a'); assert(s.length() == 2); Note: std::string 's c_str() member will always append a null character to the returned char buffer; However, std::string 's data() member may or may not append a null character to the returned char buffer. Be careful of operator+= One thing to look out for is to not use operator+= with a char* on the RHS. It