stdstring

How to copy/set a volatile std::string?

◇◆丶佛笑我妖孽 提交于 2019-12-04 14:13:30
How can I copy a volatile std::string ? There is no copy constructor for volatile, nor does something like c_str allow volatile access. operator= also doesn't seem to allow setting a volatile. It seems like std::string is simply unusable as a volatile object. Is this intended, or am I missing some way to use it? NOTE: I have easy workarounds, I just came upon the issue while trying to use string in some low-level code. As you noted, none of the member functions on std::string are marked volatile , so you can't perform any operations on a volatile std::string . I think the only option would be

C++ How to calculate the number time a string occurs in a data

走远了吗. 提交于 2019-12-04 12:52:02
I want to measure the following two things: How many times a comma appears in a std::std, e.g. if str ="1,2,3,4,1,2," then str.Count(',') returns me 6 in case of above string The second thing is also similar to the first on but instead of single char i want to calculate the number of occurances of a string e.g str.FindAllOccurancesOF("1,2,") returns me 2 Is there any builtin functions in c++ for calculating this or i need to write custom code for this? Using one of the std::string::find methods, you can step through the reference string, counting each time you find the sub-string. No need for

std::cout doen't like std::endl and string in conditional-if

末鹿安然 提交于 2019-12-04 05:53:27
问题 main.cpp: In function ‘void PrintVector(std::vector<std::__cxx11::basic_string<char> >&, bool)’: main.cpp:16:41: error: overloaded function with no contextual type information std::cout << ((newline)? (std::endl) : ""); ^~ Why std::cout doen't like std::endl and string in conditional-if? 回答1: std::endl is a stream manipulator. It's a function. It does not have a common type with "" . So they cannot be the two types of a conditional expression. Since the common type is the type of the whole

Specific behaviour of std::string on visual studio?

最后都变了- 提交于 2019-12-04 05:53:17
I've got a project in which I need to read/write large files. I've decided to use ifstream::read() to put those files into memory in one single pass, into an std::string. (that seems to be the fastest way to do it in c++ : http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html and http://insanecoding.blogspot.com/2011/11/reading-in-entire-file-at-once-in-c.html ) When switching between files, I then need to "reset" the std::string used as the previous memory buffer (ie, erase the char[] buffer to free memory) I tried : std::string::clear() std::string::assign("") std::string:

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

旧街凉风 提交于 2019-12-04 03:19:38
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 = {"Hello", "World"}; ^ And running the ./a.out I get a Segmentation fault (core dumped) I think it is

convert a char* to std::string

半腔热情 提交于 2019-12-03 17:53:42
问题 I need to use an std::string to store data retrieved by fgets() . To do this I need to convert the char* return value from fgets() into an std::string to store in an array. How can this be done? 回答1: std::string has a constructor for this: const char *s = "Hello, World!"; std::string str(s); Just make sure that your char * isn't NULL , or else the behavior is undefined. 回答2: If you already know size of the char*, use this instead char* data = ...; int size = ...; std::string myString(data,

constexpr with string operations workaround?

﹥>﹥吖頭↗ 提交于 2019-12-03 17:41:28
This previously answered question explains why the code I have posted below does not work. I have a follow-up question: is there a workaround that is conceptually equivalent, i.e., achieves compile-time string concatenation, but is implemented in a way that is actually supported by C++11? Using std::string is completely non-essential. constexpr std::string foo() { return std::string("foo"); } constexpr std::string bar() { return std::string("bar"); } constexpr std::string foobar() { return foo() + bar(); } Compile-time "string" concatenation : #include <iostream> #include <string> template

getting cout output to a std::string

大兔子大兔子 提交于 2019-12-03 14:30:04
问题 I have the following cout statement. I use char arrays because I have to pass to vsnprintf to convert variable argument list and store in Msg . Is there any way we can get cout output to C++ std::string ? char Msg[100]; char appname1[100]; char appname2[100]; char appname3[100]; // I have some logic in function which some string is assigned to Msg. std::cout << Msg << " "<< appname1 <<":"<< appname2 << ":" << appname3 << " " << "!" << getpid() <<" " << "~" << pthread_self() << endl; 回答1: You

C++: how to convert ASCII or ANSI to UTF8 and stores in std::string

风流意气都作罢 提交于 2019-12-03 13:59:10
问题 My company use some code like this: std::string(CT2CA(some_CString)).c_str() which I believe it converts a Unicode string (whose type is CString)into ANSI encoding, and this string is for a email's subject. However, header of the email (which includes the subject) indicates that the mail client should decode it as a unicode (this is how the original code does). Thus, some German chars like "ä ö ü" will not be properly displayed as the title. Is there anyway that I can put this header back to

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

独自空忆成欢 提交于 2019-12-03 13:26:58
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 to improve this a bit by using a reference/pointer into the original text. From what I have read it is a