在 Java 中,我可以通过调用来构造单个元素的集合:
Collection<String> c = Collections.singleton("foo");
在 C++ 中(在 Boost 或其他中)是否有类似的单线std::vector
或构造?std::set
在 Java 中,我可以通过调用来构造单个元素的集合:
Collection<String> c = Collections.singleton("foo");
在 C++ 中(在 Boost 或其他中)是否有类似的单线std::vector
或构造?std::set
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
.