初始化一个字符串的向量数组 [英] Initialize a vector array of strings

查看:276
本文介绍了初始化一个字符串的向量数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以初始化一个字符串的向量数组。

Would it be possible to initialize a vector array of strings.

例如:

static std :: vector< std :: string> v; //声明为类成员

我使用 static 并用字符串填充它。

I used static just to initialize and fill it with strings. Or should i just fill it in constructor if it can't be initialized like we do regular arrays.

推荐答案

排序:

class some_class {
    static std::vector<std::string> v; // declaration
};

const char *vinit[] = {"one", "two", "three"};

std::vector<std::string> some_class::v(vinit, end(vinit)); // definition

end 不必写 vinit + 3 ,如果长度后来更改,请保持最新。定义为:

end is just so I don't have to write vinit+3 and keep it up to date if the length changes later. Define it as:

template<typename T, size_t N>
T * end(T (&ra)[N]) {
    return ra + N;
}

这篇关于初始化一个字符串的向量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆