3

在 Java 中,我可以通过调用来构造单个元素的集合:

Collection<String> c = Collections.singleton("foo");

在 C++ 中(在 Boost 或其他中)是否有类似的单线std::vector或构造?std::set

4

1 回答 1

5

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.

于 2013-09-11T08:39:18.713 回答