In Java, I can construct a collection of single element by calling:
Collection<String> c = Collections.singleton("foo");
Is there a similar one-liner for std::vector or std::set construction in C++ (in Boost or whatever)?
No, but there is also no need for it. In C++11 you can leverage the compiler's magic support for std::initializer_list<T> (and the new vector constructor that accepts one) by simply writing
vector<string> vec { "foo" };
The same goes for std::set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With