stdstring

How to elegantly initialize vector<char *> with string literal?

我的未来我决定 提交于 2019-12-12 08:46:56
问题 The problem comes from an exercise on C++ Primer 5th Edition: Write a program to assign the elements from a list of char* pointers to C-style character strings to a vector of strings. ----------------Oringinal Question------------ First I try the following somewhat direct way: vector<char *> vec = {"Hello", "World"}; vec[0][0] = 'h'; But compiling the code I get a warning: temp.cpp:11:43: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] vector<char *> vec = {

MFC: std::string vs CString?

你离开我真会死。 提交于 2019-12-12 08:18:25
问题 Using C++ with MFC. Coming from a C# background I typically just use string for all, well, strings. I use them for class members, method parameters, and method return values. Now in C++ I've got std::string, CString, char *, LPCTSTR, and more. As I design my data members, method parameters, and method return values which type(s) should I be using? Ease of use is important and CString seems to offer that but my instinct is toward portable standards although portability is pretty low on my list

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

余生长醉 提交于 2019-12-12 07:39:58
问题 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*>

Confusing “std::out_of_range” Error

こ雲淡風輕ζ 提交于 2019-12-12 06:57:59
问题 I've been debugging this error for the last 2 hours, and knowing myself I won't be able to sleep if I don't ask for help before I go to bed. I'm writing a model loader for my game, and just for now I'm using a pretty flimsy method to split strings. However, it works on nearly identical lines, then randomly doesn't. I'm using string.substr(), and I believe the error means its trying to start at a location that doesn't exist in the string. The call stack says its happening on this line: v1 = v1

Weird behavior while converting a std::string to a LPCSTR

我的未来我决定 提交于 2019-12-12 04:19:47
问题 I was playing with some strings when I stumbled upon a weird behavior while converting a std::string to a LPCSTR . I have written a small test application to demonstrate : #include <string> #include <Windows.h> #include <iostream> using namespace std; int main () { string stringTest = (string("some text") + " in addition with this other text").c_str(); LPCSTR lpstrTest= stringTest.c_str(); cout << lpcstrTest << '\n'; cout << (string("some text") + " in addition with this other text").c_str()

std::string::find always returns string::npos even

雨燕双飞 提交于 2019-12-12 04:13:28
问题 std::string::find is always returning string::npos even if it's supposed to find something. In this case I'm trying to find a { followed by a new line. But no matter what string I put there, it won't find it. pos=0; while(pos!=string::npos) { ko=input.find("{\n"); //here is the problem!!! if(ko!=string::npos && input[ko]=='\n') { input.erase(ko, 3); kc=input.find("}", ko); for(pos=input.find("\",", ko); pos<kc; pos=input.find("\",\n", pos)) { input.erase(pos, 4); input.insert(pos, " | "); }

Confusing std::string::c_str() behavior in VS2010

社会主义新天地 提交于 2019-12-12 03:36:50
问题 I'm sure I've done something wrong, but for the life of me I can't figure out what! Please consider the following code: cerr<<el.getText()<<endl; cerr<<el.getText().c_str()<<endl; cerr<<"---"<<endl; const char *value = el.getText().c_str(); cerr<<"\""<<value<<"\""<<endl; field.cdata = el.getText().c_str(); cerr<<"\""<<field.cdata<<"\""<<endl; el is an XML element and getText returns a std::string. As expected, el.getText() and el.getText().c_str() print the same value. However, value is set

Optimizing flex string literal parsing

孤街浪徒 提交于 2019-12-12 03:34:03
问题 I am starting writing a lexical analyzer for my programming language. String literals in this language start with a " and end when an unescaped " is encountered. Everything inside (including newlines) is preserved, except escape sequences (the usual \n s, \t s, \" s etc plus a way of escaping a character by using its ASCII code, e.g. \097 or \97 ). This is the code I have written so far: %{ #include <iostream> #define YY_DECL extern "C" int yylex() std::string buffstr; %} %x SSTATE %% \" {

Keep temporary std::string and return c_str() to prevent memory leaks

故事扮演 提交于 2019-12-12 02:20:09
问题 I found myself using this type of code below to prevent memory leaks, is there anything wrong with it in terms of performance, safety, style or ...? The idea is that if i need to return an edited string (in terms of a c-string not std::string), i use a temporary std::string as a helper and set it to what I want my return to be and keep that temporary alive. Next time i call that function it re-sets the temporary to the new value that I want. And since the way i use the returned c-string, I

Get unichar * from a C++ std::string& to create a nonnull NSString in Objective-C++

亡梦爱人 提交于 2019-12-12 01:45:56
问题 I have some Objective-C++ that wraps an existing C++ library. The C++ library occasionally gives me std::string& parameters that I want to convert into nonnull NSString s. Almost all of the NSString initializers return nullable NSString s except: -initWithCharactersNoCopy:length:freeWhenDone: and -initWithCharacters:length: . However, both of those require unichar * , but std::string.c_str() returns char * . How can I get a unichar * from a C++ std::string so that I can create an NSString *