2

With an array you can do this:

int a[] = { 1,2,3,4,5,6 }

How do you do this with a vector?

john smith
  • 619
  • 1
  • 9
  • 15
  • 2
    You currently cannot use initializer lists with vectors, but that is a feature in C++0x, which to my knowledge has yet to be implemented in compilers. – Thomas Russell Aug 20 '11 at 12:31
  • possible duplicate of [how-to initialize 'const std::vector' like a c array](http://stackoverflow.com/questions/231491/how-to-initialize-const-stdvectort-like-a-c-array) – Ferruccio Aug 20 '11 at 13:09
  • @Shaktal: It's certainly implemented in my (fairly old) version of GCC, as long as you enable it with `--std=c++0x`. But you're correct, it's not valid in C++03. – Mike Seymour Aug 20 '11 at 13:37
  • @Mike Ahh, I wasn't aware that it was implemented in GCC, although I do know that it's possibly the most up to date compiler out there (probably due to it's open-source nature). Thanks for clearing that up! :) – Thomas Russell Aug 20 '11 at 13:50

4 Answers4

6

You could write this, though its not an initialization, but looks very appealing:

std::vector<int> v;
v << 1,2,3,4,5,6,7,8,9,10; //all pushed into the vector!

And to support this syntax, you need two utility operators defined as:

template<typename T>
std::vector<T>& operator << (std::vector<T>& v, const T & item)
{
    v.push_back(item);  return v;
}
template<typename T>
std::vector<T>& operator,(std::vector<T>& v, const T & item)
{
    v.push_back(item);  return v;
}

And you're done!

Test code:

int main() {
       std::vector<int> v;
       v << 1,2,3,4,5,6,7,8,9,10;

       for ( size_t i = 0 ; i < v.size() ; ++i )
             std::cout << v[i] << " ";
}

Output:

1 2 3 4 5 6 7 8 9 10 

Online demo : http://www.ideone.com/1hyR3

Using the utility operators, you actually can avoid temporary variables such as the one in @Als's solution, and you can write this without boost!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • looks very nice, still it doesn't work with size_t or double (error starts with `error: no match for ‘operator<<’ in ‘v << 1’` on g++ (Debian 4.7.2-5) 4.7.2) – hardmooth Oct 07 '14 at 13:28
  • @hardmooth: Use two different template parameters if you want to allow mismatched (but convertible) types. – Nawaz Oct 07 '14 at 14:07
  • 1
    yes, you're right. templating it to `template std::vector& operator << (std::vector& v, const T & item)` does the trick. Thanks! – hardmooth Oct 10 '14 at 12:47
5

If you are using Boost, You can simply use Boost.Assign

std::vector<int> v = boost::assign::list_of(1)(2)(3)(4)(5)(6);

Or

If you are not using Boost, you can do it in two steps:

static const int a[] = {1,2,3,4,5,6};
vector<int> v (a, a + sizeof(a) / sizeof(a[0]) );
Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

If you don't want to use boost, you can use this:

std::vector<int> myvec;
int myints[] = {1776, 7, 4};
myvec.assign(myints, myints + 3);

Not exactly the same, but it comes very close. I always use this.

0

As of C++ 11 you can create a vector object with list-initialization as follows:

std::vector<int> v1 = {1, 2, 3, 4};
std::vector<int> v2 {1, 2, 3, 4};

And in C++ 17 you can omit the type to allow C++ to infer the type based on the datatype passed in the list (provided you don't mix the types):

// C++ 17
std::vector v3 = {1, 2, 3, 4};
std::vector v4 {1, 2, 3, 4};
Mike Bonnell
  • 16,181
  • 3
  • 61
  • 77