我可以创建一个数组并像这样初始化它:
int a[] = {10, 20, 30};
我如何创建一个std::vector和初始化它同样优雅?
我知道的最好的方法是:
std::vector<int> ints;
ints.push_back(10);
ints.push_back(20);
ints.push_back(30);
有没有更好的办法?
我可以创建一个数组并像这样初始化它:
int a[] = {10, 20, 30};
我如何创建一个std::vector和初始化它同样优雅?
我知道的最好的方法是:
std::vector<int> ints;
ints.push_back(10);
ints.push_back(20);
ints.push_back(30);
有没有更好的办法?
当前回答
开始:
int a[] = {10, 20, 30}; //I'm assuming 'a' is just a placeholder
如果你没有c++ 11编译器,也不想使用Boost:
const int a[] = {10, 20, 30};
const std::vector<int> ints(a, a+sizeof(a)/sizeof(int)); //Make it const if you can
如果你没有c++ 11编译器,可以使用Boost:
#include <boost/assign.hpp>
const std::vector<int> ints = boost::assign::list_of(10)(20)(30);
如果你有c++ 11编译器:
const std::vector<int> ints = {10,20,30};
其他回答
在C++ 11之前:
方法1
vector<int> v(arr, arr + sizeof(arr)/sizeof(arr[0]));
方法2
vector<int>v;
v.push_back(SomeValue);
下面的c++ 11也是可能的
vector<int>v = {1, 3, 5, 7};
我们也可以这样做
vector<int>v {1, 3, 5, 7}; // Notice .. no "=" sign
对于c++ 17以后,我们可以省略类型
vector v = {1, 3, 5, 7};
维克多·塞尔(Viktor Sehr)给出了一个最近的重复问题的答案。对我来说,它很紧凑,在视觉上很吸引人(看起来就像你在“塞”值),不需要c++ 11或第三方模块,并且避免使用额外的(编写的)变量。下面是我如何使用它与一些变化。将来我可能会转而扩展vector和/或va_arg的函数。
// Based on answer by "Viktor Sehr" on Stack Overflow
// https://stackoverflow.com/a/8907356
//
template <typename T>
class mkvec {
public:
typedef mkvec<T> my_type;
my_type& operator<< (const T& val) {
data_.push_back(val);
return *this;
}
my_type& operator<< (const std::vector<T>& inVector) {
this->data_.reserve(this->data_.size() + inVector.size());
this->data_.insert(this->data_.end(), inVector.begin(), inVector.end());
return *this;
}
operator std::vector<T>() const {
return data_;
}
private:
std::vector<T> data_;
};
std::vector<int32_t> vec1;
std::vector<int32_t> vec2;
vec1 = mkvec<int32_t>() << 5 << 8 << 19 << 79;
// vec1 = (5, 8, 19, 79)
vec2 = mkvec<int32_t>() << 1 << 2 << 3 << vec1 << 10 << 11 << 12;
// vec2 = (1, 2, 3, 5, 8, 19, 79, 10, 11, 12)
如果你想要一个与Boost::assign相同的顺序,而不需要创建对Boost的依赖关系,那么下面的代码至少大致类似:
template<class T>
class make_vector {
std::vector<T> data;
public:
make_vector(T const &val) {
data.push_back(val);
}
make_vector<T> &operator,(T const &t) {
data.push_back(t);
return *this;
}
operator std::vector<T>() { return data; }
};
template<class T>
make_vector<T> makeVect(T const &t) {
return make_vector<T>(t);
}
虽然我希望使用它的语法更简洁,但它仍然不是特别糟糕:
std::vector<int> x = (makeVect(1), 2, 3, 4);
我倾向于宣称
template< typename T, size_t N >
std::vector<T> makeVector( const T (&data)[N] )
{
return std::vector<T>(data, data+N);
}
在一个实用程序头的某处,然后所需要的是:
const double values[] = { 2.0, 1.0, 42.0, -7 };
std::vector<double> array = makeVector(values);
开始:
int a[] = {10, 20, 30}; //I'm assuming 'a' is just a placeholder
如果你没有c++ 11编译器,也不想使用Boost:
const int a[] = {10, 20, 30};
const std::vector<int> ints(a, a+sizeof(a)/sizeof(int)); //Make it const if you can
如果你没有c++ 11编译器,可以使用Boost:
#include <boost/assign.hpp>
const std::vector<int> ints = boost::assign::list_of(10)(20)(30);
如果你有c++ 11编译器:
const std::vector<int> ints = {10,20,30};