when trying to insert the data into a vector for data type structure its returning wrong size
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
add a comment |
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
4
greater than what it should be
What should it be according to you? According to me, it should be4
.
– DeiDei
Nov 16 '18 at 10:49
You push emptydata
to the vector, and then push 3 otherdata
in the loop. The total is 4.
– Yksisarvinen
Nov 16 '18 at 10:51
your whole code is equivalent tovector<data> myvect(4);
. Are you intending to assign some numbers to the members ofd1
? Possibly from some iostream?
– Caleth
Nov 16 '18 at 11:01
add a comment |
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
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
c++ vector
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 be4
.
– DeiDei
Nov 16 '18 at 10:49
You push emptydata
to the vector, and then push 3 otherdata
in the loop. The total is 4.
– Yksisarvinen
Nov 16 '18 at 10:51
your whole code is equivalent tovector<data> myvect(4);
. Are you intending to assign some numbers to the members ofd1
? Possibly from some iostream?
– Caleth
Nov 16 '18 at 11:01
add a comment |
4
greater than what it should be
What should it be according to you? According to me, it should be4
.
– DeiDei
Nov 16 '18 at 10:49
You push emptydata
to the vector, and then push 3 otherdata
in the loop. The total is 4.
– Yksisarvinen
Nov 16 '18 at 10:51
your whole code is equivalent tovector<data> myvect(4);
. Are you intending to assign some numbers to the members ofd1
? 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
add a comment |
2 Answers
2
active
oldest
votes
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.
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
add a comment |
You're pushing a default initialised item into the vector before the loop.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
You're pushing a default initialised item into the vector before the loop.
add a comment |
You're pushing a default initialised item into the vector before the loop.
add a comment |
You're pushing a default initialised item into the vector before the loop.
You're pushing a default initialised item into the vector before the loop.
answered Nov 16 '18 at 11:08
JamesJames
954
954
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
4
greater than what it should be
What should it be according to you? According to me, it should be4
.– DeiDei
Nov 16 '18 at 10:49
You push empty
data
to the vector, and then push 3 otherdata
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 ofd1
? Possibly from some iostream?– Caleth
Nov 16 '18 at 11:01