2013-04-08 53 views
1

C++是否支持STL或支持Arrays.asList()的外部庫?類似Java的Arrays.asList()在C++中

典型用法

private ArrayList<String> lexeme = new ArrayList<String>(Arrays.asList(" ", ",", "(", ")", ";", "=", ".", "*", "-")); 

我使用Visual Studio 11(2012),他們已經不包括C++ 11功能Initializer lists使我有奎德里作爲初始化九個唯一的字符串向量沒有

std::vector<std::string>::push_back("a"); 
std::vector<std::string>::push_back("b"); 
std::vector<std::string>::push_back("c"); 
       . . . 
+0

我相信這是接近你的問題和解決問題:HTTP:// stackoverflo w.com/questions/7050485/implementing-a-stdarray-like-container-with-a-c11-initializer-list?rq=1 – quetzalcoatl 2013-04-08 18:11:19

回答

3

之前C++ 11是首先建立一個數組來做,然後用它初始化向量,例如常見的事:

char const * arr[] = { " ", ",", "(", ")", ";", "=", ".", "*", "-" }; 
std::vector<std::string> str_vec(arr, arr + sizeof(arr)/sizeof(*arr)); 

當然,VS11不支持一些C++ 11,這樣你就可以做到這一點,而不是,這是稍微更具可讀性:

char const * arr[] = { " ", ",", "(", ")", ";", "=", ".", "*", "-" }; 
std::vector<std::string> str_vec(std::begin(arr), std::end(arr)); 
1

你可以使用:

const char* arr[] = {"a", "bc", "def"}; 
std::vector<std::string> vec(std::begin(arr), std::end(arr)); 

如果你的編譯不支持std::begin()std::end(),他們很容易做到無:

std::vector<std::string> vec(arr, arr + sizeof(arr)/sizeof(*arr));