Monday, April 16, 2018

Vector passing to function by reference and by pointer

#include<vector>
#include<iostream>

using namespace std;

bool setValues(vector<int> &base, vector<int> &compare);
bool setValues(vector<int> *base, vector<int> *compare);

int main(){
    vector<int> vect1;
    vector<int> vect2;
    vect1.push_back(10);
    vect1.push_back(20);
    vect1.push_back(30);

    if (setValues(vect1, vect2))
        cout << "They are equal" << endl;
    else
        cout << "They are not equal" << endl;

    if (setValues(&vect1, &vect2))
        cout << "They are equal" << endl;
    else
        cout << "They are not equal" << endl;


    for (int value:vect1)
        cout << value << " ";

    return 0;
}
bool setValues(vector<int> &base, vector<int> &compare) {
    bool isEqual = true;
    compare.push_back(10);
    compare.push_back(20);
    compare.push_back(30);
    for (int i = 0; i < base.size();i++) {
        if (base.at(i) != compare.at(i))
            return false;
    }
}

bool setValues(vector<int> *base, vector<int> *compare) {
    bool isEqual = true;
    (*compare).push_back(10);
    (*compare).push_back(20);
    (*compare).push_back(30);
    for (int i = 0; i < (*base).size(); i++) {
        if ((*base).at(i) != (*compare).at(i))
            return false;
    }
}

No comments:

Post a Comment