What is the easiest way to initialize a std::vector with hardcoded elements?

后端 未结 29 2649
终归单人心
终归单人心 2020-11-22 05:07

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 sim

相关标签:
29条回答
  • 2020-11-22 05:43

    If the array is:

    int arr[] = {1, 2, 3};
    int len = (sizeof(arr)/sizeof(arr[0])); // finding length of array
    vector < int > v;
    std:: v.assign(arr, arr+len); // assigning elements from array to vector 
    
    0 讨论(0)
  • 2020-11-22 05:44

    B. Stroustrup describes a nice way to chain operations in 16.2.10 Selfreference on page 464 in the C++11 edition of the Prog. Lang. where a function returns a reference, here modified to a vector. This way you can chain like v.pb(1).pb(2).pb(3); but may be too much work for such small gains.

    #include <iostream>
    #include <vector>
    
    template<typename T>
    class chain
    {
    private:
        std::vector<T> _v;
    public:
        chain& pb(T a) {
            _v.push_back(a);
            return *this;
        };
        std::vector<T> get() { return _v; };
    };
    
    using namespace std;
    
    int main(int argc, char const *argv[])
    {
        chain<int> v{};
    
        v.pb(1).pb(2).pb(3);
    
        for (auto& i : v.get()) {
            cout << i << endl;
        }
    
        return 0;
    }
    

    1
    2
    3

    0 讨论(0)
  • 2020-11-22 05:45

    If you can, 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.

    0 讨论(0)
  • 2020-11-22 05:45

    Just thought I'd toss in my $0.02. I tend to declare this:

    template< typename T, size_t N >
    std::vector<T> makeVector( const T (&data)[N] )
    {
        return std::vector<T>(data, data+N);
    }
    

    in a utility header somewhere and then all that's required is:

    const double values[] = { 2.0, 1.0, 42.0, -7 };
    std::vector<double> array = makeVector(values);
    

    But I can't wait for C++0x. I'm stuck because my code must also compile in Visual Studio. Boo.

    0 讨论(0)
  • 2020-11-22 05:46

    Starting with:

    int a[] = {10, 20, 30}; //i'm assuming a is just a placeholder
    

    If you don't have a C++11 compiler and you don't want to use boost:

    const int a[] = {10, 20, 30};
    const std::vector<int> ints(a,a+sizeof(a)/sizeof(int)); //make it const if you can
    

    If you don't have a C++11 compiler and can use boost:

    #include <boost/assign.hpp>
    const std::vector<int> ints = boost::assign::list_of(10)(20)(30);
    

    If you do have a C++11 compiler:

    const std::vector<int> ints = {10,20,30};
    
    0 讨论(0)
  • 2020-11-22 05:46

    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)
    
    0 讨论(0)
提交回复
热议问题