solved.

code removed
Last edited on
You use the range of vector a while iterating, but try to access the same positions in b and c which got different sizes (they have no elements at start). What you should do is reserve enough space for both b and c.
Hello student2019,

Lines 8 and 12 should be a push_back() just like you did in main.

Hope that helps,

Andy
Thank you so much! it makes much more sense now and why I made my mistake!
Why did you remove the code? Let other beginners learn please.
You have a history of removing your questions once they are answered.

This is an online community, and those who help understand that by helping in a public forum they can help more than one person. Anyone who searches for and finds this question can learn from what has been posted.

By deleting the content of your posts, you disrespect the people who answer your questions and the other folks who search for answers here by asserting that your convenience is more important than the time and effort of others. Please stop deleting the contents of your posts and making it less likely people stick around to answer questions in the future.

[edit: Original post below (Original title: "Vectors help!")]
OP wrote:
Write a function named oddsEvens that takes in 3 integer vectors. The 1st vector will contain some number of integers and the 2nd and 3rd vectors should be passed in empty. The function should fill the 2nd vector with all of the even values in the 1st vector and fill the 3rd vector with all of the odd values in the 1st vector. The first vector must remain unchanged.

For example, if the 1st vector passed in contains the values {6, 5, 2, 10, 11, 4}, then when the function returns, the 2nd vector passed in should contain the values {6, 2, 10, 4} (the even values) and the 3rd vector passed in should contain the values {5, 11} (the odd values).


The function should just return (do nothing) if either the 2nd or the 3rd vector are not passed in empty.

So this is the code I have so far but I am not sure if I am doing this correctly. When i run it, this error message comes up:
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Aborted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
using namespace std;

void oddsEvens(vector<int> &a, vector<int> &b, vector<int> &c){
    for(int i = 0; i < a.size(); ++i){
        if(a.at(i) % 2 == 0){
            b.at(i) = a.at(i);
            cout << b.at(i) << endl;
        }
        else if(a.at(i) % 2 != 0){
            c.at(i) = a.at(i);
            cout << c.at(i) << endl;
        }
    }
    return;
}

int main(){
    vector<int> a;
    a.push_back(6);
    a.push_back(5);
    a.push_back(2);
    a.push_back(10);
    a.push_back(11);
    a.push_back(5);
    vector<int> b;
    vector<int> c;
    
    oddsEvens(a, b, c);    
    
    return 0;

}
Last edited on
@cire: forward looking of you to save the OP and repost it though it shouldn't really come to this. Very poor behavior on OP's part, next time s/he posts at least some of us regulars would have noted
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/201942/
Topic archived. No new replies allowed.