Sign in to follow this  

C++11, how does std::vector take a static array initializer?

This topic is 823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

With C++11 you can initialize a vector as if it were a static array, eg.:

std::vector<int> numbers = { 0, 1, 2, 3 };

I have a class that I want to be able to declare and initialize with a static array like that.  I've been reading through the <vector> header file but I'm not even sure where they're doing it, much less how.  Anyone know how to do this?

Share this post


Link to post
Share on other sites

I'm fairly certain that the complier is doing this at compile time and that it has nothing to do with how vectors are implemented in code. Sorry. Brain fart..

Edited by MarkS

Share this post


Link to post
Share on other sites

Initializer lists are very buggy in VisualC++ 2013, so that's what I've done several times:

#include <vector>
#include <iostream>

struct S {
    S(const std::vector<int>& items) {
        for (const auto & item : items) {
            std::cout << item << "\n";
        }
    }
};

int main () {
    S s({ 1, 2, 10, 20 });
}

Share this post


Link to post
Share on other sites


Initializer lists are very buggy in VisualC++ 2013, so that's what I've done several times:

 

Do you have the most recent service pack for VS2013? I had some issues with initalizer list with VS2013 in its original state, but with the SP they disappeared.

Share this post


Link to post
Share on other sites

This topic is 823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this