Jump to content
  • Advertisement
Sign in to follow this  
Funkymunky

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

This topic is 1033 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
Advertisement

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
You should also consider upgrading to 2015, as the CE version is free. It greatly improves C++11/14 compliance over previous versions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!