Last active
April 15, 2022 16:00
-
-
Save EdoardoVignati/d40fededf3ca5b43ede551f53e2d0c46 to your computer and use it in GitHub Desktop.
Header and prototype for function taking a reference to vector as argument in c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
==== my_main.cpp ==== | |
#include <iostream> | |
#include "my_vector.hpp" | |
using namespace std; | |
int main() | |
{ | |
vector<int> myvec = {1, 2, 3}; | |
add_to_vector(myvec, 4); | |
for(int elem : myvec){ | |
cout << elem << endl; | |
} | |
return 0; | |
} | |
==== my_vector.hpp ==== | |
#include <vector> | |
#ifndef MY_VECTOR | |
#define MY_VECTOR | |
void add_to_vector(std::vector<int>&, int); | |
#endif | |
==== my_vector.cpp ==== | |
#include "my_vector.hpp" | |
void add_to_vector(std::vector<int> &myvector, int mynum){ | |
myvector.push_back(mynum); | |
} | |
=== Instructions === | |
- Compile with > g++ *.cpp *.hpp -o my_main | |
- Run with > ./my_main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment