Easy question regarding parameters of functions!

Oct 6, 2013 at 3:45pm
I have a ton of int vectors in my main function that I pass to another function.

1
2
3
4
void function(std::vector<int> &vec1, std::vector<int> &vec2, std::vector<int> &vec3)
{
   //Random stuff
}


The above is just an example in my actual code I am passing 6,7,8 vectors to a function with longer names. My question is for the sake of efficient coding and readability should I use typedef and instead of using:

std::vector<int> &vec1

use

vi &vec1 //vi = vector int from type def

so my function would look like

void function(vi &vec1, vi &vec2, vi &vec3)

or is there a way to pass multiple variable names of the same variable type at once , thanks :)
Oct 6, 2013 at 3:57pm
multiple variable names of the same variable type at once
std::vector<std::vector<int>> :)

Or use typedefs (in C++11 write using vi = std::vector<int>)
Oct 6, 2013 at 3:58pm
for the sake of efficient coding and readability should I use typedef

It's all a matter of style and clarity to the reader. If using vi as a shorthand for std::vector<int> is clear in your code, then I see nothing wrong with using it.
I think the choice of the name vi to stand for a vector of ints is quite clear.

is there a way to pass multiple variable names of the same variable type at once

No

edit: After seeing MiNiPaa's response, I would clarify by saying "Not as separate arguments".
Last edited on Oct 6, 2013 at 3:59pm
Oct 6, 2013 at 3:59pm
std::vector<std::vector<int>>

How would I write the variable name of each with that , to pass to "function"
Oct 6, 2013 at 4:05pm
Why do you have to pass so many vectors to function? It smells fishy here.
Oct 6, 2013 at 4:09pm
Because each vector contains unique values that need to be present at all times, I have multiple functions that each process the values differently and I don't want to use global variables so I pass them from the main function to the other functions. It works perfectly but I was just wondering if there was a shorter way of writing each individual variable to the function.

Looks as if using typedef will be the solution, its only going to be me coding it but I also love looking at neat efficient code for my own sake, just wondering if its seen as "incorrect" to do so :)
Oct 6, 2013 at 4:13pm
How different your values is? Do they have relation to each other? Couldn't they be grouped into structs?

if its seen as "incorrect" to do so
Absolutely not. If I would write
std::unordered_map<std::string,std::function<std::pair<bool, std::string>(std::string)>> each time I need it...
Oct 6, 2013 at 4:18pm
Couldn't they be grouped into structs?


Possibly! I havent done any work with structs yet (still learning) , would you be able to give me a brief overview of how a struct works and how they could be implemented in this situation? (I will research in depth later today)
Last edited on Oct 6, 2013 at 4:20pm
Oct 6, 2013 at 4:25pm
For example if you need to process Students with first and second name and term grade.
You can do it without structs like:
1
2
3
4
5
6
7
8
9
10
std::cout << "Enter number of students: ";
std::cin >> n;
std::vector<std::string> firstName;
std::vector<std::string> lastName;
std::vector<int> grades;
for (int i=0; i<n; ++i) {
    //...
    firstName.push_back(f);
    //...
}
or, with structs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Student 
{
    std::string firstName;
    std::string lastName;
    int grade;
};
//...
std::cout << "Enter number of students: ";
std::cin >> n;
std::vector<Student> students;
for (int i=0; i<n; ++i) {
    Student student;
    std::cin >> student.firstName;
    //...
    students.push_back(student);
}
And then you can pass ypur students vector to functions and use it like students[0].grade = totalGrades/gradesNum;
Last edited on Oct 6, 2013 at 4:27pm
Topic archived. No new replies allowed.