[英]Java-like Arrays.asList() in c++
Does c++ support either in the STL or there exists an external library supporting Arrays.asList() ? c ++是否支持STL,或者是否存在支持Arrays.asList()的外部库?
Typical usage 典型用法
private ArrayList<String> lexeme = new ArrayList<String>(Arrays.asList(" ", ",", "(", ")", ";", "=", ".", "*", "-"));
I am using Visual Studio 11 (2012) and they have not included the c++11 feature Initializer lists
leaving me in a quandry as to initialize a vector of nine unique strings without 我正在使用Visual Studio 11(2012)并且他们没有包含c ++ 11特性
Initializer lists
让我处于一个quandry中,初始化了九个唯一字符串的向量而没有
std::vector<std::string>::push_back("a");
std::vector<std::string>::push_back("b");
std::vector<std::string>::push_back("c");
. . .
A common thing to do before C++11 was to first create an array, then initialize the vector with it, for example: 在C ++ 11之前要做的一件事是首先创建一个数组,然后用它初始化向量,例如:
char const * arr[] = { " ", ",", "(", ")", ";", "=", ".", "*", "-" };
std::vector<std::string> str_vec(arr, arr + sizeof(arr) / sizeof(*arr));
Of course, VS11 does support some of C++11, so you can do this instead, which is slightly more readable: 当然,VS11确实支持一些C ++ 11,所以你可以这样做,它更具可读性:
char const * arr[] = { " ", ",", "(", ")", ";", "=", ".", "*", "-" };
std::vector<std::string> str_vec(std::begin(arr), std::end(arr));
You could use: 你可以使用:
const char* arr[] = {"a", "bc", "def"};
std::vector<std::string> vec(std::begin(arr), std::end(arr));
If your compiler doesn't support std::begin()
and std::end()
, they are easy to do without: 如果你的编译器不支持
std::begin()
和std::end()
,那么很容易做到:
std::vector<std::string> vec(arr, arr + sizeof(arr) / sizeof(*arr));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.