when trying to insert the data into a vector for data type structure its returning wrong size












0















I am trying to insert data into a vector which has the data type as structure. But when i am doing that its returning me a size one greater than what it should be.



struct data{
int cID;
int arrival;
int service;
};

vector<data> myvect;

int main()
{
data d1;

myvect.push_back(data());

for(int i = 0; i < 3 ; i++){
int i1 = i + 1;
int i2 = i + 2;
int i3 = i + 3;

i1 >> d1.cID;
i2 >> d1.arrival;
i3 >> d1.service;

myvect.push_back(d1);
}
cout << myvect.size();

return 0;
}









share|improve this question


















  • 4





    greater than what it should be What should it be according to you? According to me, it should be 4.

    – DeiDei
    Nov 16 '18 at 10:49













  • You push empty data to the vector, and then push 3 other data in the loop. The total is 4.

    – Yksisarvinen
    Nov 16 '18 at 10:51













  • your whole code is equivalent to vector<data> myvect(4);. Are you intending to assign some numbers to the members of d1? Possibly from some iostream?

    – Caleth
    Nov 16 '18 at 11:01
















0















I am trying to insert data into a vector which has the data type as structure. But when i am doing that its returning me a size one greater than what it should be.



struct data{
int cID;
int arrival;
int service;
};

vector<data> myvect;

int main()
{
data d1;

myvect.push_back(data());

for(int i = 0; i < 3 ; i++){
int i1 = i + 1;
int i2 = i + 2;
int i3 = i + 3;

i1 >> d1.cID;
i2 >> d1.arrival;
i3 >> d1.service;

myvect.push_back(d1);
}
cout << myvect.size();

return 0;
}









share|improve this question


















  • 4





    greater than what it should be What should it be according to you? According to me, it should be 4.

    – DeiDei
    Nov 16 '18 at 10:49













  • You push empty data to the vector, and then push 3 other data in the loop. The total is 4.

    – Yksisarvinen
    Nov 16 '18 at 10:51













  • your whole code is equivalent to vector<data> myvect(4);. Are you intending to assign some numbers to the members of d1? Possibly from some iostream?

    – Caleth
    Nov 16 '18 at 11:01














0












0








0








I am trying to insert data into a vector which has the data type as structure. But when i am doing that its returning me a size one greater than what it should be.



struct data{
int cID;
int arrival;
int service;
};

vector<data> myvect;

int main()
{
data d1;

myvect.push_back(data());

for(int i = 0; i < 3 ; i++){
int i1 = i + 1;
int i2 = i + 2;
int i3 = i + 3;

i1 >> d1.cID;
i2 >> d1.arrival;
i3 >> d1.service;

myvect.push_back(d1);
}
cout << myvect.size();

return 0;
}









share|improve this question














I am trying to insert data into a vector which has the data type as structure. But when i am doing that its returning me a size one greater than what it should be.



struct data{
int cID;
int arrival;
int service;
};

vector<data> myvect;

int main()
{
data d1;

myvect.push_back(data());

for(int i = 0; i < 3 ; i++){
int i1 = i + 1;
int i2 = i + 2;
int i3 = i + 3;

i1 >> d1.cID;
i2 >> d1.arrival;
i3 >> d1.service;

myvect.push_back(d1);
}
cout << myvect.size();

return 0;
}






c++ vector






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 10:48









Kasichainula KeshavKasichainula Keshav

35




35








  • 4





    greater than what it should be What should it be according to you? According to me, it should be 4.

    – DeiDei
    Nov 16 '18 at 10:49













  • You push empty data to the vector, and then push 3 other data in the loop. The total is 4.

    – Yksisarvinen
    Nov 16 '18 at 10:51













  • your whole code is equivalent to vector<data> myvect(4);. Are you intending to assign some numbers to the members of d1? Possibly from some iostream?

    – Caleth
    Nov 16 '18 at 11:01














  • 4





    greater than what it should be What should it be according to you? According to me, it should be 4.

    – DeiDei
    Nov 16 '18 at 10:49













  • You push empty data to the vector, and then push 3 other data in the loop. The total is 4.

    – Yksisarvinen
    Nov 16 '18 at 10:51













  • your whole code is equivalent to vector<data> myvect(4);. Are you intending to assign some numbers to the members of d1? Possibly from some iostream?

    – Caleth
    Nov 16 '18 at 11:01








4




4





greater than what it should be What should it be according to you? According to me, it should be 4.

– DeiDei
Nov 16 '18 at 10:49







greater than what it should be What should it be according to you? According to me, it should be 4.

– DeiDei
Nov 16 '18 at 10:49















You push empty data to the vector, and then push 3 other data in the loop. The total is 4.

– Yksisarvinen
Nov 16 '18 at 10:51







You push empty data to the vector, and then push 3 other data in the loop. The total is 4.

– Yksisarvinen
Nov 16 '18 at 10:51















your whole code is equivalent to vector<data> myvect(4);. Are you intending to assign some numbers to the members of d1? Possibly from some iostream?

– Caleth
Nov 16 '18 at 11:01





your whole code is equivalent to vector<data> myvect(4);. Are you intending to assign some numbers to the members of d1? Possibly from some iostream?

– Caleth
Nov 16 '18 at 11:01












2 Answers
2






active

oldest

votes


















1














Apart from the bit shifting statements (e.g. i1 >> d1.cID;) that have no effect, the code is fine and inserts a total of four items to the vector.






share|improve this answer
























  • I'm not so sure. Members of struct are not initialized in this case (see here stackoverflow.com/questions/8280023/… ) , and bitshift operators yield ub, when right operand > number of bits in type of left operand. And as values of members of d1 are not determined, the chances are at least one othe them is > then number of bits in int, and so code triggers ub.

    – Andrew Kashpur
    Nov 16 '18 at 11:14





















0














You're pushing a default initialised item into the vector before the loop.






share|improve this answer
























    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%2f53336306%2fwhen-trying-to-insert-the-data-into-a-vector-for-data-type-structure-its-returni%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Apart from the bit shifting statements (e.g. i1 >> d1.cID;) that have no effect, the code is fine and inserts a total of four items to the vector.






    share|improve this answer
























    • I'm not so sure. Members of struct are not initialized in this case (see here stackoverflow.com/questions/8280023/… ) , and bitshift operators yield ub, when right operand > number of bits in type of left operand. And as values of members of d1 are not determined, the chances are at least one othe them is > then number of bits in int, and so code triggers ub.

      – Andrew Kashpur
      Nov 16 '18 at 11:14


















    1














    Apart from the bit shifting statements (e.g. i1 >> d1.cID;) that have no effect, the code is fine and inserts a total of four items to the vector.






    share|improve this answer
























    • I'm not so sure. Members of struct are not initialized in this case (see here stackoverflow.com/questions/8280023/… ) , and bitshift operators yield ub, when right operand > number of bits in type of left operand. And as values of members of d1 are not determined, the chances are at least one othe them is > then number of bits in int, and so code triggers ub.

      – Andrew Kashpur
      Nov 16 '18 at 11:14
















    1












    1








    1







    Apart from the bit shifting statements (e.g. i1 >> d1.cID;) that have no effect, the code is fine and inserts a total of four items to the vector.






    share|improve this answer













    Apart from the bit shifting statements (e.g. i1 >> d1.cID;) that have no effect, the code is fine and inserts a total of four items to the vector.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 11:01









    Krzysiek KarbowiakKrzysiek Karbowiak

    538310




    538310













    • I'm not so sure. Members of struct are not initialized in this case (see here stackoverflow.com/questions/8280023/… ) , and bitshift operators yield ub, when right operand > number of bits in type of left operand. And as values of members of d1 are not determined, the chances are at least one othe them is > then number of bits in int, and so code triggers ub.

      – Andrew Kashpur
      Nov 16 '18 at 11:14





















    • I'm not so sure. Members of struct are not initialized in this case (see here stackoverflow.com/questions/8280023/… ) , and bitshift operators yield ub, when right operand > number of bits in type of left operand. And as values of members of d1 are not determined, the chances are at least one othe them is > then number of bits in int, and so code triggers ub.

      – Andrew Kashpur
      Nov 16 '18 at 11:14



















    I'm not so sure. Members of struct are not initialized in this case (see here stackoverflow.com/questions/8280023/… ) , and bitshift operators yield ub, when right operand > number of bits in type of left operand. And as values of members of d1 are not determined, the chances are at least one othe them is > then number of bits in int, and so code triggers ub.

    – Andrew Kashpur
    Nov 16 '18 at 11:14







    I'm not so sure. Members of struct are not initialized in this case (see here stackoverflow.com/questions/8280023/… ) , and bitshift operators yield ub, when right operand > number of bits in type of left operand. And as values of members of d1 are not determined, the chances are at least one othe them is > then number of bits in int, and so code triggers ub.

    – Andrew Kashpur
    Nov 16 '18 at 11:14















    0














    You're pushing a default initialised item into the vector before the loop.






    share|improve this answer




























      0














      You're pushing a default initialised item into the vector before the loop.






      share|improve this answer


























        0












        0








        0







        You're pushing a default initialised item into the vector before the loop.






        share|improve this answer













        You're pushing a default initialised item into the vector before the loop.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 11:08









        JamesJames

        954




        954






























            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%2f53336306%2fwhen-trying-to-insert-the-data-into-a-vector-for-data-type-structure-its-returni%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

            Bressuire

            Vorschmack

            Quarantine