stdstring

Can std::string be used without #include <string>? [duplicate]

你。 提交于 2019-12-19 13:11:32
问题 This question already has answers here : Why does omission of “#include <string>” only sometimes cause compilation failures? (6 answers) Closed 4 years ago . Here is my code: #include <iostream> int main(int argc, char const *argv[]) { std::string s = "hello"; std::cout << s.size() << std::endl; return 0; } To my surprise, I can compile and run it with clang++ , though I even don't add #include <string> . So, is it necessary to add #include <string> in order to use std::string ? 回答1: Your

C++ compile error constructing object with rvalue std::string

情到浓时终转凉″ 提交于 2019-12-18 18:54:29
问题 I'm faced with a compile error that I don't even know how to describe! It completely baffles me. The situation : Code tries to create an object on the stack with an rvalue std::string that is initialized with a char*. The code : #include <iostream> #include <string> class Foo { public: Foo(double d) : mD(d) { } Foo(const std::string& str) { try { mD = std::stod(str); } catch (...) { throw; } } Foo(const Foo& other) : mD(other.mD) { } virtual ~Foo() {} protected: double mD; }; class Bar {

C++ addition overload ambiguity

隐身守侯 提交于 2019-12-18 09:17:12
问题 I am coming up against a vexing conundrum in my code base. I can't quite tell why my code generates this error, but (for example) std::string does not. class String { public: String(const char*str); friend String operator+ ( const String& lval, const char *rval ); friend String operator+ ( const char *lval, const String& rval ); String operator+ ( const String& rval ); }; The implementation of these is easy enough to imagine on your own. My driver program contains the following: String result

Improper use of c_str

社会主义新天地 提交于 2019-12-18 08:59:05
问题 I have a method defined as below: const std::string returnStringMethod() { std::string myString; // populate myString return myString; } Now, in the caller, I was doing something like this: const char * ptr = returnStringMethod().c_str(); As I can see this is returning some truncated string which I did not expect. However, the folllowing works fine: std::string str = returnStringMethod(); const char * ptr = str.c_str(); Can someone please help me understand whats happening here? . PS: We

how to put std::string into boost::lockfree::queue (or alternative)?

岁酱吖の 提交于 2019-12-18 07:37:11
问题 I'm trying to put std::string s into boost::lockfree::queue s so that my threads can update each other with new data. When I try to use boost::lockfree::queue<std::string> updated_data; , g++ says : In instantiation of 'class boost::lockfree::queue >': error: static assertion failed: (boost::has_trivial_destructor::value) error: static assertion failed: (boost::has_trivial_assign::value) I've been shown generally what these errors mean, but I have no hope of ever fixing this myself, as I'm

C++/CLI String Conversions

女生的网名这么多〃 提交于 2019-12-18 05:18:51
问题 I found this really nice piece of code that converts a string to a System:String^ as in: System::String^ rtn = gcnew String(move.c_str()); // 'move' here is the string I'm passing rtn back to a C# program. Anyways, inside the function where this code exists, I'm passing in a System::String^ . I also found some code to convert a System:String^ to a string using the following code: pin_ptr<const wchar_t> wch = PtrToStringChars(cmd); // 'cmd' here is the System:String size_t convertedChars = 0;

How to construct a std::string from a std::vector<string>?

对着背影说爱祢 提交于 2019-12-17 22:10:53
问题 I'd like to build a std::string from a std::vector<std::string> . I could use std::stringsteam , but imagine there is a shorter way: std::string string_from_vector(const std::vector<std::string> &pieces) { std::stringstream ss; for(std::vector<std::string>::const_iterator itr = pieces.begin(); itr != pieces.end(); ++itr) { ss << *itr; } return ss.str(); } How else might I do this? 回答1: C++03 std::string s; for (std::vector<std::string>::const_iterator i = v.begin(); i != v.end(); ++i) s += *i

What is the point of STL Character Traits?

牧云@^-^@ 提交于 2019-12-17 17:23:48
问题 I notice that in my copy of the SGI STL reference, there is a page about Character Traits but I can't see how these are used? Do they replace the string.h functions? They don't seem to be used by std::string , e.g. the length() method on std::string doesn't make use of the Character Traits length() method. Why do Character Traits exist and are they ever used in practice? 回答1: Character traits are an extremely important component of the streams and strings libraries because they allow the

Can a std::string contain embedded nulls?

最后都变了- 提交于 2019-12-17 16:30:14
问题 For regular C strings, a null character '\0' signifies the end of data. What about std::string , can I have a string with embedded null characters? 回答1: Yes you can have embedded nulls in your std::string . Example: std::string s; s.push_back('\0'); s.push_back('a'); assert(s.length() == 2); Note: std::string 's c_str() member will always append a null character to the returned char buffer; However, std::string 's data() member may or may not append a null character to the returned char

Padding stl strings in C++

。_饼干妹妹 提交于 2019-12-17 16:26:07
问题 I'm using std::string and need to left pad them to a given width. What is the recommended way to do this in C++? Sample input: 123 pad to 10 characters. Sample output: 123 (7 spaces in front of 123) 回答1: std::setw (setwidth) manipulator std::cout << std::setw (10) << 77 << std::endl; or std::cout << std::setw (10) << "hi!" << std::endl; outputs padded 77 and "hi!". if you need result as string use instance of std::stringstream instead std::cout object. ps: responsible header file <iomanip>