stdstring

MFC: std::string vs CString?

做~自己de王妃 提交于 2019-12-04 22:31:31
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 of priorities (now). Also, I don't like the c semantics of creating string buffers and passing them

Why must a string be constructed at run-time? [duplicate]

情到浓时终转凉″ 提交于 2019-12-04 22:22:51
This question already has an answer here: Is it possible to use std::string in a constexpr? 4 answers Can C-Strings or std::string s be created as constexpr or must they be created at run-time? With gcc 4.9.2 I can do this: constexpr const char foo[] = "blee"; (Sadly the November 2013 Customer Technology Preview does not allow Visual Studio to support this: https://stackoverflow.com/a/29255013/2642059 ) But even with gcc 4.9.2 I cannot do this: constexpr const std::string foo = "blee"; I get the error: error: the type 'const string {aka const std::basic_string<char>}' of constexpr variable

In C++11 what is the most performant way to return a reference/pointer to a position in a std::string?

。_饼干妹妹 提交于 2019-12-04 20:48:48
问题 I'm building a text parser that uses std::string as the core storage for strings. I know this is not optimal and that parsers inside compilers use optimzed approaches for this. In my project I don't mind losing some performance in exchange for more clarity and easier maintenance. At the beginning I read a huge text into memory and then I scan each character to build a ordered set of tokens, its a simple lexer. Currently I'm using std::string to represent the text of a token but I would like

How fast is dynamic_cast<>

三世轮回 提交于 2019-12-04 19:22:47
问题 ... approximately compared to a typical std::string::operator==() ? I give some more details below, I'm not sure if they are of any relevance. Answer with complexity or approximation is good enough. Thanks! Details: I will use it inside a for loop over a list to find some specific instances. I estimate my average level of inheritance to 3.5 classes. The one I'm looking for has a parent class, a grandparent and above that two "interfaces", i.e. to abstract classes with a couple of virtual void

Using strings in switch statements - where do we stand with C++17?

笑着哭i 提交于 2019-12-04 17:09:36
问题 Every one of us has (probably) had the childhood dream of writing: switch(my_std_string) { case "foo": do_stuff(); break; case "bar": do_other_stuff(); break; default: just_give_up(); } but this is not possible, as is explained in the answers to this question from the olden days (2009): Why switch statement cannot be applied on strings? Since then we've seen the advent of C++11, which lets us go as far as: switch (my_hash::hash(my_std_string)) { case "foo"_hash: do_stuff(); break; case "bar"

Setting an std::string variable value from gdb?

╄→гoц情女王★ 提交于 2019-12-04 16:14:51
问题 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" ? 回答1: 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

Macro for static std::string object from literal

戏子无情 提交于 2019-12-04 15:59:23
问题 Suppose I need to call a function foo that takes a const std::string reference from a great number of places in my code: int foo(const std::string&); .. foo("bar"); .. foo("baz"); Calling a function with a string literal like this will create temporary std::string objects, copying the literal each time. Unless I'm mistaken, compilers won't optimize this by creating a static std::string object per literal that can be reused for subsequent calls. I know that g++ has advanced string pool

Initialize std::string from a possibly NULL char pointer

走远了吗. 提交于 2019-12-04 15:37:47
问题 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,

How to replace all occurrences of one character with two characters using std::string?

依然范特西╮ 提交于 2019-12-04 15:31:09
问题 Is there a nice simple way to replace all occurrences of "/" in a std::string with "\/" to escape all the slashes in a std::string ? 回答1: Probably the simplest way to get this done is with boost string algorithms library. boost::replace_all(myString, "/", "\\/"); std::string result = boost::replace_all_copy(myString, "/", "\\/"); 回答2: The answer is no... there is no "easy" way if you mean an one-liner already provided by the standard library. However it's not hard to implement that function.

C++20 with u8, char8_t and std::string

我与影子孤独终老i 提交于 2019-12-04 15:27:52
问题 C++11 brought us the u8 prefix for UTF-8 literals and I thought that was pretty cool a few years ago and peppered my code with things like this: std::string myString = u8"●"; This is all fine and good, but the issue comes up in C++20 it doesn't seem to compile anymore because u8 creates a char8_t* and this is incompatible with std::string which just uses char. Should I be creating a new utf8string? What's the consistent and correct way to do this kind of thing in a C++20 world where we have