Difference between `vector v;` and `vector v = vector();`What is the difference between #include...

Broken patches on a road

Can a person refuse a presidential pardon?

Parsing a string of key-value pairs as a dictionary

How to tag distinct options/entities without giving any an implicit priority or suggested order?

Solving Fredholm Equation of the second kind

Isn't using the Extrusion Multiplier like cheating?

Why does a metal block make a shrill sound but not a wooden block upon hammering?

What makes the Forgotten Realms "forgotten"?

What's a good word to describe a public place that looks like it wouldn't be rough?

We are very unlucky in my court

What to do if authors don't respond to my serious concerns about their paper?

Is there any differences between "Gucken" and "Schauen"?

Word or phrase for showing great skill at something without formal training in it

Am I a Rude Number?

How to prevent cleaner from hanging my lock screen in Ubuntu 16.04

Every character has a name - does this lead to too many named characters?

Why does String.replaceAll() work differently in Java 8 from Java 9?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

What kind of hardware implements Fourier transform?

Pre-1980's science fiction short story: alien disguised as a woman shot by a gangster, has tentacles coming out of her breasts when remaking her body

Why doesn't "auto ch = unsigned char{'p'}" compile under C++ 17?

Grade 10 Analytic Geometry Question 23- Incredibly hard

Quenching swords in dragon blood; why?

Why zero tolerance on nudity in space?



Difference between `vector v;` and `vector v = vector();`


What is the difference between #include <filename> and #include “filename”?What are the differences between a pointer variable and a reference variable in C++?What is the difference between g++ and gcc?Do the parentheses after the type name make a difference with new?Difference between private, public, and protected inheritanceWhat is the difference between const int*, const int * const, and int const *?What is difference between instantiating an object using new vs. withoutRule-of-Three becomes Rule-of-Five with C++11?Easiest way to convert int to string in C++Difference between Constructor and ngOnInit













19















What is the difference between



std::vector<int> v;


and



std::vector<int> v = std::vector<int>();


Intuitively, I would never use the second version but I am not sure if there is any difference. It feels to me that the second line is simply a default constructor building a temporary object that is then moved by by a move assignment operator.



I am wondering whether the second line is not somehow equivalent to



std::vector<int>* p = new std::vector<int>();
std::vector<int> v = *p;


or



std::vector<int> v = *(new std::vector<int>()); 


hence causing the vector itself to be on the heap (dynamically allocated). If it is the case, then the first line would probably be preferred for most cases.



How do these lines of code differ?










share|improve this question

























  • Even when there are temporary objects, they won't be on the heap.

    – Daniel H
    yesterday
















19















What is the difference between



std::vector<int> v;


and



std::vector<int> v = std::vector<int>();


Intuitively, I would never use the second version but I am not sure if there is any difference. It feels to me that the second line is simply a default constructor building a temporary object that is then moved by by a move assignment operator.



I am wondering whether the second line is not somehow equivalent to



std::vector<int>* p = new std::vector<int>();
std::vector<int> v = *p;


or



std::vector<int> v = *(new std::vector<int>()); 


hence causing the vector itself to be on the heap (dynamically allocated). If it is the case, then the first line would probably be preferred for most cases.



How do these lines of code differ?










share|improve this question

























  • Even when there are temporary objects, they won't be on the heap.

    – Daniel H
    yesterday














19












19








19


2






What is the difference between



std::vector<int> v;


and



std::vector<int> v = std::vector<int>();


Intuitively, I would never use the second version but I am not sure if there is any difference. It feels to me that the second line is simply a default constructor building a temporary object that is then moved by by a move assignment operator.



I am wondering whether the second line is not somehow equivalent to



std::vector<int>* p = new std::vector<int>();
std::vector<int> v = *p;


or



std::vector<int> v = *(new std::vector<int>()); 


hence causing the vector itself to be on the heap (dynamically allocated). If it is the case, then the first line would probably be preferred for most cases.



How do these lines of code differ?










share|improve this question
















What is the difference between



std::vector<int> v;


and



std::vector<int> v = std::vector<int>();


Intuitively, I would never use the second version but I am not sure if there is any difference. It feels to me that the second line is simply a default constructor building a temporary object that is then moved by by a move assignment operator.



I am wondering whether the second line is not somehow equivalent to



std::vector<int>* p = new std::vector<int>();
std::vector<int> v = *p;


or



std::vector<int> v = *(new std::vector<int>()); 


hence causing the vector itself to be on the heap (dynamically allocated). If it is the case, then the first line would probably be preferred for most cases.



How do these lines of code differ?







c++ constructor initialization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 11 hours ago







Remi.b

















asked yesterday









Remi.bRemi.b

6,0111447100




6,0111447100













  • Even when there are temporary objects, they won't be on the heap.

    – Daniel H
    yesterday



















  • Even when there are temporary objects, they won't be on the heap.

    – Daniel H
    yesterday

















Even when there are temporary objects, they won't be on the heap.

– Daniel H
yesterday





Even when there are temporary objects, they won't be on the heap.

– Daniel H
yesterday












3 Answers
3






active

oldest

votes


















30














Starting from C++17 there's no difference whatsoever.



There's one niche use case where the std::vector = std::vector initialization syntax is quite useful (albeit not for default construction): when one wants to supply a "count, value" initializer for std::vector<int> member of a class directly in the class's definition:



struct S {
std::vector<int> v; // Want to supply `(5, 42)` initializer here. How?
};


In-class initializers support only = or {} syntax, meaning that we cannot just say



struct S {
std::vector<int> v(5, 42); // Error
};


If we use



struct S {
std::vector<int> v{ 5, 42 }; // or = { 5, 42 }
};


the compiler will interpret it as a list of values instead of "count, value" pair, which is not what we want.



So, one proper way to do it is



struct S {
std::vector<int> v = std::vector(5, 42);
};





share|improve this answer


























  • What changed in C++17 to mean there's now no difference? What was the difference in C++14?

    – Muzer
    15 hours ago











  • @Muzer see: stackoverflow.com/a/54937445/4342498

    – NathanOliver
    15 hours ago



















17














The 1st one is default initialization, the 2nd one is copy initialization; The effect is same here, i.e. initialize the object v via the default constructor of std::vector.



For std::vector<int> v = std::vector<int>();, in concept it will construct a temporary std::vector then use it to move-construct the object v (note there's no assignment here). According to the copy elision (since C++17 it's guaranteed), it'll just call the default constructor to initialize v directly.




Under the following circumstances, the compilers are required to omit
the copy and move construction of class objects, even if the copy/move
constructor and the destructor have observable side-effects. The
objects are constructed directly into the storage where they would
otherwise be copied/moved to. The copy/move constructors need not be
present or accessible, as the language rules ensure that no copy/move
operation takes place, even conceptually:





  • In the initialization of a variable, when the initializer expression
    is a prvalue of the same class type (ignoring cv-qualification) as the
    variable type:



    T x = T(T(f())); // only one call to default constructor of T, to initialize x





(Before C++17, the copy elision is an optimization.)




this is an optimization: even when it takes place and the copy/move (since C++11) constructor is not called, it still must be present and accessible (as if no optimization happened at all),




BTW: For both cases, no std::vector objects (including potential temporary) will be constructed with dynamic storage duration via new expression.






share|improve this answer

































    0














    You had asked:




    What is the difference between



    std::vector<int> v;


    and



    std::vector<int> v = std::vector<int>();


    ?




    As others have stated since C++17 there basically is no difference. Now as for preference of which one to use, I would suggest the following:




    • If you are creating an empty vector to be filled out later then the first version is the more desirable candidate.

    • If you are constructing a vector with either a default value as one of its members and or a specified count, then the later version would be the one to use.

    • Note that the following two are different constructors though:


      • std::vector<int> v = std::vector<int>();

      • Is not the same constructor as:


      • std::vector<int> v = std::vector<int>( 3, 10 );

      • The 2nd version of the constructor is an overloaded constructor.



    • Otherwise if you are constructing a vector from a set of numbers than you have the following options:


      • std::vector<int> v = { 1,2,3,4,5 };

      • operator=( std::initializer_list)

      • std::vector<int> v{ 1,2,3,4,5 };

      • list_initialization




    Here is the list of std::vector constructors.






    share|improve this answer





















    • 1





      "Now as for preference" I would say for preference auto v = std::vector<int>(); is better.

      – Slava
      yesterday











    • @Slava It depends on the situation. Sometimes you don't want to bloat your code with auto everywhere. Nothing wrong with using it in specific places, but not for everything. If you are using a ranged loop then by all means use auto to traverse through a container. However just to declare a variable; I prefer to see the container with its types so I know what the container details. Another example that is a bad case use for using auto is when using some 3rd party libraries such as Eigen. They have lazy evaluation on some things but not all and the auto feature can lead to UB.

      – Francis Cugler
      yesterday













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54937426%2fdifference-between-vectorint-v-and-vectorint-v-vectorint%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    30














    Starting from C++17 there's no difference whatsoever.



    There's one niche use case where the std::vector = std::vector initialization syntax is quite useful (albeit not for default construction): when one wants to supply a "count, value" initializer for std::vector<int> member of a class directly in the class's definition:



    struct S {
    std::vector<int> v; // Want to supply `(5, 42)` initializer here. How?
    };


    In-class initializers support only = or {} syntax, meaning that we cannot just say



    struct S {
    std::vector<int> v(5, 42); // Error
    };


    If we use



    struct S {
    std::vector<int> v{ 5, 42 }; // or = { 5, 42 }
    };


    the compiler will interpret it as a list of values instead of "count, value" pair, which is not what we want.



    So, one proper way to do it is



    struct S {
    std::vector<int> v = std::vector(5, 42);
    };





    share|improve this answer


























    • What changed in C++17 to mean there's now no difference? What was the difference in C++14?

      – Muzer
      15 hours ago











    • @Muzer see: stackoverflow.com/a/54937445/4342498

      – NathanOliver
      15 hours ago
















    30














    Starting from C++17 there's no difference whatsoever.



    There's one niche use case where the std::vector = std::vector initialization syntax is quite useful (albeit not for default construction): when one wants to supply a "count, value" initializer for std::vector<int> member of a class directly in the class's definition:



    struct S {
    std::vector<int> v; // Want to supply `(5, 42)` initializer here. How?
    };


    In-class initializers support only = or {} syntax, meaning that we cannot just say



    struct S {
    std::vector<int> v(5, 42); // Error
    };


    If we use



    struct S {
    std::vector<int> v{ 5, 42 }; // or = { 5, 42 }
    };


    the compiler will interpret it as a list of values instead of "count, value" pair, which is not what we want.



    So, one proper way to do it is



    struct S {
    std::vector<int> v = std::vector(5, 42);
    };





    share|improve this answer


























    • What changed in C++17 to mean there's now no difference? What was the difference in C++14?

      – Muzer
      15 hours ago











    • @Muzer see: stackoverflow.com/a/54937445/4342498

      – NathanOliver
      15 hours ago














    30












    30








    30







    Starting from C++17 there's no difference whatsoever.



    There's one niche use case where the std::vector = std::vector initialization syntax is quite useful (albeit not for default construction): when one wants to supply a "count, value" initializer for std::vector<int> member of a class directly in the class's definition:



    struct S {
    std::vector<int> v; // Want to supply `(5, 42)` initializer here. How?
    };


    In-class initializers support only = or {} syntax, meaning that we cannot just say



    struct S {
    std::vector<int> v(5, 42); // Error
    };


    If we use



    struct S {
    std::vector<int> v{ 5, 42 }; // or = { 5, 42 }
    };


    the compiler will interpret it as a list of values instead of "count, value" pair, which is not what we want.



    So, one proper way to do it is



    struct S {
    std::vector<int> v = std::vector(5, 42);
    };





    share|improve this answer















    Starting from C++17 there's no difference whatsoever.



    There's one niche use case where the std::vector = std::vector initialization syntax is quite useful (albeit not for default construction): when one wants to supply a "count, value" initializer for std::vector<int> member of a class directly in the class's definition:



    struct S {
    std::vector<int> v; // Want to supply `(5, 42)` initializer here. How?
    };


    In-class initializers support only = or {} syntax, meaning that we cannot just say



    struct S {
    std::vector<int> v(5, 42); // Error
    };


    If we use



    struct S {
    std::vector<int> v{ 5, 42 }; // or = { 5, 42 }
    };


    the compiler will interpret it as a list of values instead of "count, value" pair, which is not what we want.



    So, one proper way to do it is



    struct S {
    std::vector<int> v = std::vector(5, 42);
    };






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 21 hours ago

























    answered yesterday









    AnTAnT

    260k33421663




    260k33421663













    • What changed in C++17 to mean there's now no difference? What was the difference in C++14?

      – Muzer
      15 hours ago











    • @Muzer see: stackoverflow.com/a/54937445/4342498

      – NathanOliver
      15 hours ago



















    • What changed in C++17 to mean there's now no difference? What was the difference in C++14?

      – Muzer
      15 hours ago











    • @Muzer see: stackoverflow.com/a/54937445/4342498

      – NathanOliver
      15 hours ago

















    What changed in C++17 to mean there's now no difference? What was the difference in C++14?

    – Muzer
    15 hours ago





    What changed in C++17 to mean there's now no difference? What was the difference in C++14?

    – Muzer
    15 hours ago













    @Muzer see: stackoverflow.com/a/54937445/4342498

    – NathanOliver
    15 hours ago





    @Muzer see: stackoverflow.com/a/54937445/4342498

    – NathanOliver
    15 hours ago













    17














    The 1st one is default initialization, the 2nd one is copy initialization; The effect is same here, i.e. initialize the object v via the default constructor of std::vector.



    For std::vector<int> v = std::vector<int>();, in concept it will construct a temporary std::vector then use it to move-construct the object v (note there's no assignment here). According to the copy elision (since C++17 it's guaranteed), it'll just call the default constructor to initialize v directly.




    Under the following circumstances, the compilers are required to omit
    the copy and move construction of class objects, even if the copy/move
    constructor and the destructor have observable side-effects. The
    objects are constructed directly into the storage where they would
    otherwise be copied/moved to. The copy/move constructors need not be
    present or accessible, as the language rules ensure that no copy/move
    operation takes place, even conceptually:





    • In the initialization of a variable, when the initializer expression
      is a prvalue of the same class type (ignoring cv-qualification) as the
      variable type:



      T x = T(T(f())); // only one call to default constructor of T, to initialize x





    (Before C++17, the copy elision is an optimization.)




    this is an optimization: even when it takes place and the copy/move (since C++11) constructor is not called, it still must be present and accessible (as if no optimization happened at all),




    BTW: For both cases, no std::vector objects (including potential temporary) will be constructed with dynamic storage duration via new expression.






    share|improve this answer






























      17














      The 1st one is default initialization, the 2nd one is copy initialization; The effect is same here, i.e. initialize the object v via the default constructor of std::vector.



      For std::vector<int> v = std::vector<int>();, in concept it will construct a temporary std::vector then use it to move-construct the object v (note there's no assignment here). According to the copy elision (since C++17 it's guaranteed), it'll just call the default constructor to initialize v directly.




      Under the following circumstances, the compilers are required to omit
      the copy and move construction of class objects, even if the copy/move
      constructor and the destructor have observable side-effects. The
      objects are constructed directly into the storage where they would
      otherwise be copied/moved to. The copy/move constructors need not be
      present or accessible, as the language rules ensure that no copy/move
      operation takes place, even conceptually:





      • In the initialization of a variable, when the initializer expression
        is a prvalue of the same class type (ignoring cv-qualification) as the
        variable type:



        T x = T(T(f())); // only one call to default constructor of T, to initialize x





      (Before C++17, the copy elision is an optimization.)




      this is an optimization: even when it takes place and the copy/move (since C++11) constructor is not called, it still must be present and accessible (as if no optimization happened at all),




      BTW: For both cases, no std::vector objects (including potential temporary) will be constructed with dynamic storage duration via new expression.






      share|improve this answer




























        17












        17








        17







        The 1st one is default initialization, the 2nd one is copy initialization; The effect is same here, i.e. initialize the object v via the default constructor of std::vector.



        For std::vector<int> v = std::vector<int>();, in concept it will construct a temporary std::vector then use it to move-construct the object v (note there's no assignment here). According to the copy elision (since C++17 it's guaranteed), it'll just call the default constructor to initialize v directly.




        Under the following circumstances, the compilers are required to omit
        the copy and move construction of class objects, even if the copy/move
        constructor and the destructor have observable side-effects. The
        objects are constructed directly into the storage where they would
        otherwise be copied/moved to. The copy/move constructors need not be
        present or accessible, as the language rules ensure that no copy/move
        operation takes place, even conceptually:





        • In the initialization of a variable, when the initializer expression
          is a prvalue of the same class type (ignoring cv-qualification) as the
          variable type:



          T x = T(T(f())); // only one call to default constructor of T, to initialize x





        (Before C++17, the copy elision is an optimization.)




        this is an optimization: even when it takes place and the copy/move (since C++11) constructor is not called, it still must be present and accessible (as if no optimization happened at all),




        BTW: For both cases, no std::vector objects (including potential temporary) will be constructed with dynamic storage duration via new expression.






        share|improve this answer















        The 1st one is default initialization, the 2nd one is copy initialization; The effect is same here, i.e. initialize the object v via the default constructor of std::vector.



        For std::vector<int> v = std::vector<int>();, in concept it will construct a temporary std::vector then use it to move-construct the object v (note there's no assignment here). According to the copy elision (since C++17 it's guaranteed), it'll just call the default constructor to initialize v directly.




        Under the following circumstances, the compilers are required to omit
        the copy and move construction of class objects, even if the copy/move
        constructor and the destructor have observable side-effects. The
        objects are constructed directly into the storage where they would
        otherwise be copied/moved to. The copy/move constructors need not be
        present or accessible, as the language rules ensure that no copy/move
        operation takes place, even conceptually:





        • In the initialization of a variable, when the initializer expression
          is a prvalue of the same class type (ignoring cv-qualification) as the
          variable type:



          T x = T(T(f())); // only one call to default constructor of T, to initialize x





        (Before C++17, the copy elision is an optimization.)




        this is an optimization: even when it takes place and the copy/move (since C++11) constructor is not called, it still must be present and accessible (as if no optimization happened at all),




        BTW: For both cases, no std::vector objects (including potential temporary) will be constructed with dynamic storage duration via new expression.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 15 hours ago

























        answered yesterday









        songyuanyaosongyuanyao

        92.2k11177242




        92.2k11177242























            0














            You had asked:




            What is the difference between



            std::vector<int> v;


            and



            std::vector<int> v = std::vector<int>();


            ?




            As others have stated since C++17 there basically is no difference. Now as for preference of which one to use, I would suggest the following:




            • If you are creating an empty vector to be filled out later then the first version is the more desirable candidate.

            • If you are constructing a vector with either a default value as one of its members and or a specified count, then the later version would be the one to use.

            • Note that the following two are different constructors though:


              • std::vector<int> v = std::vector<int>();

              • Is not the same constructor as:


              • std::vector<int> v = std::vector<int>( 3, 10 );

              • The 2nd version of the constructor is an overloaded constructor.



            • Otherwise if you are constructing a vector from a set of numbers than you have the following options:


              • std::vector<int> v = { 1,2,3,4,5 };

              • operator=( std::initializer_list)

              • std::vector<int> v{ 1,2,3,4,5 };

              • list_initialization




            Here is the list of std::vector constructors.






            share|improve this answer





















            • 1





              "Now as for preference" I would say for preference auto v = std::vector<int>(); is better.

              – Slava
              yesterday











            • @Slava It depends on the situation. Sometimes you don't want to bloat your code with auto everywhere. Nothing wrong with using it in specific places, but not for everything. If you are using a ranged loop then by all means use auto to traverse through a container. However just to declare a variable; I prefer to see the container with its types so I know what the container details. Another example that is a bad case use for using auto is when using some 3rd party libraries such as Eigen. They have lazy evaluation on some things but not all and the auto feature can lead to UB.

              – Francis Cugler
              yesterday


















            0














            You had asked:




            What is the difference between



            std::vector<int> v;


            and



            std::vector<int> v = std::vector<int>();


            ?




            As others have stated since C++17 there basically is no difference. Now as for preference of which one to use, I would suggest the following:




            • If you are creating an empty vector to be filled out later then the first version is the more desirable candidate.

            • If you are constructing a vector with either a default value as one of its members and or a specified count, then the later version would be the one to use.

            • Note that the following two are different constructors though:


              • std::vector<int> v = std::vector<int>();

              • Is not the same constructor as:


              • std::vector<int> v = std::vector<int>( 3, 10 );

              • The 2nd version of the constructor is an overloaded constructor.



            • Otherwise if you are constructing a vector from a set of numbers than you have the following options:


              • std::vector<int> v = { 1,2,3,4,5 };

              • operator=( std::initializer_list)

              • std::vector<int> v{ 1,2,3,4,5 };

              • list_initialization




            Here is the list of std::vector constructors.






            share|improve this answer





















            • 1





              "Now as for preference" I would say for preference auto v = std::vector<int>(); is better.

              – Slava
              yesterday











            • @Slava It depends on the situation. Sometimes you don't want to bloat your code with auto everywhere. Nothing wrong with using it in specific places, but not for everything. If you are using a ranged loop then by all means use auto to traverse through a container. However just to declare a variable; I prefer to see the container with its types so I know what the container details. Another example that is a bad case use for using auto is when using some 3rd party libraries such as Eigen. They have lazy evaluation on some things but not all and the auto feature can lead to UB.

              – Francis Cugler
              yesterday
















            0












            0








            0







            You had asked:




            What is the difference between



            std::vector<int> v;


            and



            std::vector<int> v = std::vector<int>();


            ?




            As others have stated since C++17 there basically is no difference. Now as for preference of which one to use, I would suggest the following:




            • If you are creating an empty vector to be filled out later then the first version is the more desirable candidate.

            • If you are constructing a vector with either a default value as one of its members and or a specified count, then the later version would be the one to use.

            • Note that the following two are different constructors though:


              • std::vector<int> v = std::vector<int>();

              • Is not the same constructor as:


              • std::vector<int> v = std::vector<int>( 3, 10 );

              • The 2nd version of the constructor is an overloaded constructor.



            • Otherwise if you are constructing a vector from a set of numbers than you have the following options:


              • std::vector<int> v = { 1,2,3,4,5 };

              • operator=( std::initializer_list)

              • std::vector<int> v{ 1,2,3,4,5 };

              • list_initialization




            Here is the list of std::vector constructors.






            share|improve this answer















            You had asked:




            What is the difference between



            std::vector<int> v;


            and



            std::vector<int> v = std::vector<int>();


            ?




            As others have stated since C++17 there basically is no difference. Now as for preference of which one to use, I would suggest the following:




            • If you are creating an empty vector to be filled out later then the first version is the more desirable candidate.

            • If you are constructing a vector with either a default value as one of its members and or a specified count, then the later version would be the one to use.

            • Note that the following two are different constructors though:


              • std::vector<int> v = std::vector<int>();

              • Is not the same constructor as:


              • std::vector<int> v = std::vector<int>( 3, 10 );

              • The 2nd version of the constructor is an overloaded constructor.



            • Otherwise if you are constructing a vector from a set of numbers than you have the following options:


              • std::vector<int> v = { 1,2,3,4,5 };

              • operator=( std::initializer_list)

              • std::vector<int> v{ 1,2,3,4,5 };

              • list_initialization




            Here is the list of std::vector constructors.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 14 hours ago









            Hong Ooi

            42.7k1094136




            42.7k1094136










            answered yesterday









            Francis CuglerFrancis Cugler

            4,79611227




            4,79611227








            • 1





              "Now as for preference" I would say for preference auto v = std::vector<int>(); is better.

              – Slava
              yesterday











            • @Slava It depends on the situation. Sometimes you don't want to bloat your code with auto everywhere. Nothing wrong with using it in specific places, but not for everything. If you are using a ranged loop then by all means use auto to traverse through a container. However just to declare a variable; I prefer to see the container with its types so I know what the container details. Another example that is a bad case use for using auto is when using some 3rd party libraries such as Eigen. They have lazy evaluation on some things but not all and the auto feature can lead to UB.

              – Francis Cugler
              yesterday
















            • 1





              "Now as for preference" I would say for preference auto v = std::vector<int>(); is better.

              – Slava
              yesterday











            • @Slava It depends on the situation. Sometimes you don't want to bloat your code with auto everywhere. Nothing wrong with using it in specific places, but not for everything. If you are using a ranged loop then by all means use auto to traverse through a container. However just to declare a variable; I prefer to see the container with its types so I know what the container details. Another example that is a bad case use for using auto is when using some 3rd party libraries such as Eigen. They have lazy evaluation on some things but not all and the auto feature can lead to UB.

              – Francis Cugler
              yesterday










            1




            1





            "Now as for preference" I would say for preference auto v = std::vector<int>(); is better.

            – Slava
            yesterday





            "Now as for preference" I would say for preference auto v = std::vector<int>(); is better.

            – Slava
            yesterday













            @Slava It depends on the situation. Sometimes you don't want to bloat your code with auto everywhere. Nothing wrong with using it in specific places, but not for everything. If you are using a ranged loop then by all means use auto to traverse through a container. However just to declare a variable; I prefer to see the container with its types so I know what the container details. Another example that is a bad case use for using auto is when using some 3rd party libraries such as Eigen. They have lazy evaluation on some things but not all and the auto feature can lead to UB.

            – Francis Cugler
            yesterday







            @Slava It depends on the situation. Sometimes you don't want to bloat your code with auto everywhere. Nothing wrong with using it in specific places, but not for everything. If you are using a ranged loop then by all means use auto to traverse through a container. However just to declare a variable; I prefer to see the container with its types so I know what the container details. Another example that is a bad case use for using auto is when using some 3rd party libraries such as Eigen. They have lazy evaluation on some things but not all and the auto feature can lead to UB.

            – Francis Cugler
            yesterday




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54937426%2fdifference-between-vectorint-v-and-vectorint-v-vectorint%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

            How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

            Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...