stdstring

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

泪湿孤枕 提交于 2019-11-26 08:48:48
问题 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

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

别来无恙 提交于 2019-11-26 08:09:33
问题 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

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

*爱你&永不变心* 提交于 2019-11-26 06:57:49
问题 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? 回答1: 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

Is std::string refcounted in GCC 4.x / C++11?

拈花ヽ惹草 提交于 2019-11-26 06:44:25
问题 Is std::string reference-counted when using gcc 4 with -std=c++0x or -std=c++11 ? 回答1: Looking at libstdc++ documentation I find (see the link for more info): A string looks like this: [_Rep] _M_length [basic_string<char>] _M_capacity _M_dataplus _M_refcount _M_p ----------------> unnamed array of char_type So, yes it is ref counted. Also, from the discussion here: Yes, std::string will be made non-reference counting at some point, but as a non-reference-counted string is valid in C++98 as

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

依然范特西╮ 提交于 2019-11-26 06:03:36
问题 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

How do you construct a std::string with an embedded null?

可紊 提交于 2019-11-26 03:33:30
问题 If I want to construct a std::string with a line like: std::string my_string(\"a\\0b\"); Where i want to have three characters in the resulting string (a, null, b), I only get one. What is the proper syntax? 回答1: Since C++14 we have been able to create literal std::string #include <iostream> #include <string> int main() { using namespace std::string_literals; std::string s = "pl-\0-op"s; // <- Notice the "s" at the end // This is a std::string literal not // a C-String literal. std::cout << s

char* vs std::string in c++ [closed]

℡╲_俬逩灬. 提交于 2019-11-26 02:18:31
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . When should I use std::string and when should I use char* to manage arrays of char s in C++? It seems you should use char* if

Alternative to itoa() for converting integer to string C++? [duplicate]

耗尽温柔 提交于 2019-11-26 01:29:19
问题 This question already has answers here : Easiest way to convert int to string in C++ (27 answers) Closed 4 years ago . I was wondering if there was an alternative to itoa() for converting an integer to a string because when I run it in visual Studio I get warnings, and when I try to build my program under Linux, I get a compilation error. 回答1: In C++11 you can use std::to_string: #include <string> std::string s = std::to_string(5); If you're working with prior to C++11, you could use C++

Legality of COW std::string implementation in C++11

青春壹個敷衍的年華 提交于 2019-11-25 23:30:32
问题 It had been my understanding that copy-on-write is not a viable way to implement a conforming std::string in C++11, but when it came up in discussion recently I found myself unable to directly support that statement. Am I correct that C++11 does not admit COW based implementations of std::string ? If so, is this restriction explicitly stated somewhere in the new standard (where)? Or is this restriction implied, in the sense that it is the combined effect of the new requirements on std::string

How to replace all occurrences of a character in string?

时光毁灭记忆、已成空白 提交于 2019-11-25 22:48:18
问题 What is the effective way to replace all occurrences of a character with another character in std::string ? 回答1: std::string doesn't contain such function but you could use stand-alone replace function from algorithm header. #include <algorithm> #include <string> void some_func() { std::string s = "example string"; std::replace( s.begin(), s.end(), 'x', 'y'); // replace all 'x' to 'y' } 回答2: I thought I'd toss in the boost solution as well: #include <boost/algorithm/string/replace.hpp> // in