stdstring

Does std::atomic<std::string> work appropriately?

会有一股神秘感。 提交于 2019-11-27 02:29:32
问题 I am reading through Anthony Williams' "C++ Concurrency in Action" and in Chapter 5, which talks about the new multithreading-aware memory model and atomic operations, and he states: In order to use std::atomic<UDT> for some user-defined UDT , this type must have a trivial copy assignment operator. As I understand it, this means that we can use std::atomic<UDT> if the following returns true: std::is_trivially_copyable<UDT>::value By this logic, we shouldn't be able to use std::string as a

Is std::string size() a O(1) operation?

若如初见. 提交于 2019-11-27 01:57:34
Is std::string size() a O(1) operation? The implementation of STL I'm using is the one built into VC++ Michael Burr If you're asking if MSVC's implementation of string::size() has constant complexity, then the answer is yes. But Don Wakefield mentioned Table 65 in 23.1 of the C++ Standard where it says that the complexity of size() should follow what's said in 'Note A'. Note A says: Those entries marked ‘‘(Note A)’’ should have constant complexity. However, that does not mean that those entries shall have constant complexity. Standards use very specific terminology, and "should" means that it

std::string::c_str() and temporaries

廉价感情. 提交于 2019-11-27 01:00:27
Is the following C++ code well-formed: void consumer(char const* p) { std::printf("%s", p); } std::string random_string_generator() { // returns a random std::string object } consumer(random_string_generator().c_str()); The problem I have with it is, that after creating the temporary std::string object and taking the c_str() pointer, nothing prevents the std::string object from getting destroyed (or maybe I'm wrong?). Can you please point me to the standard, if the code is OK despite everything. It does work, when I test with g++. The pointer returned by std::string::c_str() points to memory

How to convert CString and ::std::string ::std::wstring to each other?

a 夏天 提交于 2019-11-26 23:42:38
CString is quite handy, while std::string is more compatible with STL container. I am using hash_map . However, hash_map does not support CString as key, so I want to convert CString into std::string . Writing a CString hash function seems to take a lot of time. CString -----> std::string How can I do this? std::string -----> CString: inline CString toCString(std::string const& str) { return CString(str.c_str()); } Am I right? EDIT: Here are more questions: How can I convert wstring , CString to each other? //wstring -> CString, std::wstring src; CString result(src.c_str()); //CString->wstring

How do you append an int to a string in C++? [duplicate]

一个人想着一个人 提交于 2019-11-26 23:37:57
This question already has an answer here: How to concatenate a std::string and an int? 22 answers int i = 4; string text = "Player "; cout << (text + i); I'd like it to print Player 4 . The above is obviously wrong but it shows what I'm trying to do here. Is there an easy way to do this or do I have to start adding new includes? headmyshoulder With C++11, you can write: #include <string> // to use std::string, std::to_string() and "+" operator acting on strings int i = 4; std::string text = "Player "; text += std::to_string(i); Well, if you use cout you can just write the integer directly to

Legal to overwrite std::string's null terminator?

两盒软妹~` 提交于 2019-11-26 22:17:04
问题 In C++11, we know that std::string is guaranteed to be both contiguous and null-terminated (or more pedantically, terminated by charT() , which in the case of char is the null character 0). There is this C API I need to use that fills in a string by pointer. It writes the whole string + null terminator. In C++03, I was always forced to use a vector<char> , because I couldn't assume that string was contiguous or null-terminated. But in C++11 (assuming a properly conforming basic_string class,

Why don't the std::fstream classes take a std::string?

不想你离开。 提交于 2019-11-26 22:05:50
This isn't a design question, really, though it may seem like it. (Well, okay, it's kind of a design question). What I'm wondering is why the C++ std::fstream classes don't take a std::string in their constructor or open methods. Everyone loves code examples so: #include <iostream> #include <fstream> #include <string> int main() { std::string filename = "testfile"; std::ifstream fin; fin.open(filename.c_str()); // Works just fine. fin.close(); //fin.open(filename); // Error: no such method. //fin.close(); } This gets me all the time when working with files. Surely the C++ library would use std

C++: how to get fprintf results as a std::string w/o sprintf

橙三吉。 提交于 2019-11-26 19:42:56
问题 I am working with an open-source UNIX tool that is implemented in C++, and I need to change some code to get it to do what I want. I would like to make the smallest possible change in hopes of getting my patch accepted upstream. Solutions that are implementable in standard C++ and do not create more external dependencies are preferred. Here is my problem. I have a C++ class -- let's call it "A" -- that currently uses fprintf() to print its heavily formatted data structures to a file pointer.

How to get the number of characters in a std::string?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 18:39:49
How should I get the number of characters in a string in C++? Eclipse If you're using a std::string , call length() : std::string str = "hello"; std::cout << str << ":" << str.length(); // Outputs "hello:5" If you're using a c-string, call strlen() . const char *str = "hello"; std::cout << str << ":" << strlen(str); // Outputs "hello:5" Or, if you happen to like using Pascal-style strings (or f***** strings as Joel Spolsky likes to call them when they have a trailing NULL), just dereference the first character. const char *str = "\005hello"; std::cout << str + 1 << ":" << *str; // Outputs

How to implode a vector of strings into a string (the elegant way)

橙三吉。 提交于 2019-11-26 17:32:56
I'm looking for the most elegant way to implode a vector of strings into a string. Below is the solution I'm using now: static std::string& implode(const std::vector<std::string>& elems, char delim, std::string& s) { for (std::vector<std::string>::const_iterator ii = elems.begin(); ii != elems.end(); ++ii) { s += (*ii); if ( ii + 1 != elems.end() ) { s += delim; } } return s; } static std::string implode(const std::vector<std::string>& elems, char delim) { std::string s; return implode(elems, delim, s); } Is there any others out there? Andre Holzner Use boost::algorithm::join(..) : #include