
please wait
StrItems()
and its job is to return the string-representation of a sequential container (array, vector, queue, deque, etc.) std::to_string
to do the type conversion but to_string has a small number of overloads for primitive types.
|
|
|
|
|
|
I've used std::to_string to do the type conversion but to_string has a small number of overloads for primitive types. |
using std::to_string;
|
|
|
|
Ganado wrote: |
---|
In C++, I usually see more of a focus on streams than you might in other languages. Usually, the primary functionality desired is to be able to send data to a stream, whether it's cout (stdout), a file stream, or a string stream. A stringstream, for example, can then easily be converted into a string. But if you're just going to be printing the value, converting it to a string first is just wasted computation -- just directly send the object/container to the output stream. |
string ToString()
. Subclasses (i.e., user-defined classes) inherit the method and developers override them to define how their classes are represented as strings. In contexts where the object is used like a string (e.g., System.Console.Printline(MyObject)
), the overloaded MyObject.ToString()
gets called. Python classes work similarly with __repr__ and __str__ methods, [2].
|
|
|
|
to_string(obj)
. If the type of obj
matches one defined by std::to_string()
, the std::'s to_string() gets called. Otherwise, it looks for a function with a matching signature across all other compilation units (which it will find, presumably in the header/src file containing your Foo
class. dutch wrote: |
---|
I don't feel the indent or newline are particularly useful. However, allowing the user to specify the separator would be useful. |
Making Tea .Boil Water ..Pour water into electric kettle ...Make sure kettle is unplugged ...Pour water into kettle reservoir ..Plug kettle in .Find tea bag ..Search cabinet 1 ..Search cabinet 2 |
dutch wrote: |
---|
using std::to_string; if (c.empty()) return ""; std::string s; auto it = c.begin(); s = to_string(*it); |
|
|
ElusiveTau wrote: |
---|
My guess is that execution tries to run to_string(obj). If the type of obj matches one defined by std::to_string(), the std::'s to_string() gets called. Otherwise, it looks for a function with a matching signature across all other compilation units (which it will find, presumably in the header/src file containing your Foo class. |
|
|