What is the easiest way to initialize a std::vecto

2018-12-31 08:11发布

I can create an array and initialize it like this:

int a[] = {10, 20, 30};

How do I create a std::vector and initialize it similarly elegant?

The best way I know is:

std::vector<int> ints;

ints.push_back(10);
ints.push_back(20);
ints.push_back(30);

Is there a better way?

25条回答
闭嘴吧你
2楼-- · 2018-12-31 08:39

For God's sake, use the modern C++[11,14,17,...] way:

std::vector<int> vec = {10,20,30};

The old way of looping over a variable-length array or using sizeof() is truly terrible on the eyes and completely unnecessary in terms of mental overhead. Yuck.

查看更多
笑指拈花
3楼-- · 2018-12-31 08:39

A more recent duplicate question has this answer by Viktor Sehr. For me, it is compact, visually appealing (looks like you are 'shoving' the values in), doesn't require c++11 or a third party module, and avoids using an extra (written) variable. Below is how I am using it with a few changes. I may switch to extending the function of vector and/or va_arg in the future intead.


// 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)
查看更多
明月照影归
4楼-- · 2018-12-31 08:40

If your compiler supports Variadic macros (which is true for most modern compilers), then you can use the following macro to turn vector initialization into a one-liner:

#define INIT_VECTOR(type, name, ...) \
static const type name##_a[] = __VA_ARGS__; \
vector<type> name(name##_a, name##_a + sizeof(name##_a) / sizeof(*name##_a))

With this macro, you can define an initialized vector with code like this:

INIT_VECTOR(int, my_vector, {1, 2, 3, 4});

This would create a new vector of ints named my_vector with the elements 1, 2, 3, 4.

查看更多
泛滥B
5楼-- · 2018-12-31 08:40

Related, you can use the following if you want to have a vector completely ready to go in a quick statement (e.g. immediately passing to another function):

#define VECTOR(first,...) \
   ([](){ \
   static const decltype(first) arr[] = { first,__VA_ARGS__ }; \
   std::vector<decltype(first)> ret(arr, arr + sizeof(arr) / sizeof(*arr)); \
   return ret;})()

example function

template<typename T>
void test(std::vector<T>& values)
{
    for(T value : values)
        std::cout<<value<<std::endl;
}

example use

test(VECTOR(1.2f,2,3,4,5,6));

though be careful about the decltype, make sure the first value is clearly what you want.

查看更多
人间绝色
6楼-- · 2018-12-31 08:43

Before C++ 11 :

Method 1=>

vector<int> v(arr, arr + sizeof(arr)/sizeof(arr[0]));
vector<int>v;

Method 2 =>

 v.push_back(SomeValue);

C++ 11 onward below is also possible

vector<int>v = {1, 3, 5, 7};
查看更多
浅入江南
7楼-- · 2018-12-31 08:44
typedef std::vector<int> arr;

arr a {10, 20, 30};       // This would be how you initialize while defining

To compile use:

clang++ -std=c++11 -stdlib=libc++  <filename.cpp>
查看更多
登录 后发表回答