我想用std::string
的數組初始化一個向量。用數組初始化std :: string的向量
我有以下解決方案,但想知道是否有更優雅的方式來做到這一點?
std::string str[] = { "one", "two", "three", "four" };
vector<std::string> vec;
vec = vector<std::string>(str, str + (sizeof (str)/sizeof (std::string)));
我可以,當然,如下使這更可讀通過定義大小:
int size = (sizeof (str)/sizeof (std::string));
,並用替換矢量初始化:
vec = vector<std::string>(str, str + size);
但是,這仍然感覺小「不雅」。
見http://stackoverflow.com/questions/231491/how-to-initialize-const-stdvectort-like-ac-array –