Changing value of a class outside the class - c++

i have the class Team which has the attribute "Points".
In another sheet.cpp i want to change this points with some function like this:
void Result(Team a, Team b)
{
int startWerte[] = { 8, 8 };//just random start values)
std::vector< int > punkte(startWerte, startWerte + sizeof(startWerte) / sizeof(int));
points[0]=a.getpoints();
points[1]=b.getpoints();
Here follows some calculation which ends in the final values for the points stored in points2. Now i want to set them as the points of the teams, so they are stored.
a.setpoints(points[0])
b.setpoints(points[1]);
They are the correct values, but whenever this function ends the values are not stored in team.points correctly. If i do it by letting the Result function return the points2 vector to lets say vector testvector in the int main() it works. Example
vector<int> testvector;
testvector =Result(TeamA, TeamB) {//Same code than before follows
TeamA.setpoints(testvector[0];
TeamB.setpoints(testvector[1];
If i the repeat the Result-function everythin is stored correct. Is there no way to store the value for the points of the team class outside the int main ()?

I think your problem is that you are passing Team by value rather than by reference.
Change the Result method to
void Result(Team & a, Team & b)
and everything should be fine.

Related

Adding class objects stored in vector using operator overloading

EDIT: OK, being completely confused by what I actually needed to do and the fact that I didn't want to put specifics, it got too confusing, so I am re-writing my question. Thank you everyone who tried to help.
I have a class Something that has char a, float b, float c. The values are being read in from a file and stored as a vector <Something> vec. I would like to add the objects and display the sum in the end.
How would I go about doing that?
as a more specific example, that actually relates to what I am doing:
I have a class Force that holds char type, float xArg, float yArg. The values are read in from a file and stored in a vector <Force> f. I need to find a sum of all of the forces in that vector and display the result.
Hope this is better
Use std::accumulate with a custom accumulator:
std::accumulate(std::begin(vec), std::end(vec), 0.f,
[](auto acc, auto const& something) {
return acc + something.b;
});
In addition to using std::accumulate, you can iterate over the items of vec and accumulate the values from the items.
double sum = 0;
for(auto& item : vec )
{
sum += item.b;
}

C++ Sorting a std::list of object by their value

I'm working on a basic DFS algorithm for my oriented graph in c++.
What i got, is a class Vertex which have a char value and a list of adjacence.
Given that, i'm trying to sort a list of object (in my case vertex, but let's say something else) by its value.
My class is something like that:
class Foo {
private:
char x;
list<Foo*> listOfChildrenObject;
};
let's say we got 5 Foo's object.
A,B,C,D,E
now, A is the father of every object in my program.
let's say i've inserted them in this way:
A (the father), D, C, B, E
and then, i want to print them in order:
A,B,C,D,E
and for that, i want to use listOfChildrenObject.sort() to do that.
there's a way to sort my list of object by the char value?
Ok, i think i got it.
sorry for the dumb and repeated question, i resolved adding a lambda function to my sort, and changing the list to a vector (i realized that i had no reason to use a list)
in this way:
std::sort(v.begin(), v.end(), [](Foo* a, Foo* b) {return a->getValue() < b->getValue(); });

HashSet c++ clarification

I'm lost on this topic I have been studying. In my class we are implementing our own hash set class. Thus we have an underlying data structure , like a vector or array , and use a hash function to quickly determine whether an element is in the set it not . That is the part I do not follow. How would a hash function be used for this determination ?
imagine you have an underlying array of size 100, and you can only insert values from 0 to 99.
something like this:
class UselessHashMap
{
public:
void insert(int value){
_arr[hash(i)] = i;
}
private:
int hash(int i) { return i };
std::array<int,100> _arr;
}
now, imagine you want to store more than 100 elements, and you can't have an array that has an infinite (std::numeric_limits::max() )size.
In this case, your hash function will have to return you a value between 0-99, and of course your UselessHashMap class will need to take care of collisions as well, because that function could return the same value for different inputs.

Passing a pointer to an array of arrays to a function

I have a main function which sets up the following variables:
double matrix[numVectors][size] = {
{0.183963, 0.933146, 0.476773, 0.086125, 0.566566, 0.728107, 0.837345, 0.885175, 0.600559, 0.142238},
{0.086523, 0.025236, 0.252289, 0.089437, 0.382081, 0.420934, 0.038498, 0.626125, 0.468158, 0.247754},
{0.969345, 0.127753, 0.736213, 0.264992, 0.518971, 0.216767, 0.390992, 0.242241, 0.516135, 0.990155}
};
double result1[size], result2[size];
double *ptr_matrix = &matrix[0];
double *ptr_result1 = &result1[0];
double *ptr_result2 = &result2[0];
What the above is trying to do is:
Create an array with three rows of 10 doubles
Create two empty arrays of 10 doubles
Create a pointer to the matrix
Create pointers to the two empty arrays
Then, I'm trying to pass all three pointers to another function. This other function will iterate over the matrix rows (only the rows, it doesn't visit the whole matrix space), perform a computation using the row (as an array). The end result is the two empty arrays declared at the beginning end up each becoming one row from the matrix.
Here is the prototype of the second function:
void smallestSum(double (*mat)[size], int num, double *first, double *second)
This function goes through each combination of the matrix rows (0/1, 0/2, 1/2) and checks the sums of their values. The two arrays producing the smallest sum eventually become result1 and result2 from above.
Seeing as this is the first time I'm really delving into pointer/array/matrix territory, I have a few questions:
Am I correctly "getting" a pointer to the matrix? Or do I need to get a pointer to the first value of the matrix instead?
In smallestSum(), can I iterate over the array as I would normally (using for (int i = 0; i < num; i++)?
You need to change the definition of ptr_matrix, as it's not a pointer to a single double, but to the whole row:
double (*ptr_matrix)[size] = &matrix[0];
Then, you can call the function as follows:
smallestSum(ptr_matrix, numVectors, ptr_result1, ptr_result_2);
Inside smallestSum, you can iterate both over rows and over columns.
Note that size must be known at compilation time.
If the function doesn't modify the matrix, consider adding const to the type of its first argument.
The answer abacabadabacaba gave is mostly correct except that size does not need to be known at compile time. If you include size as a parameter to the function you can use it as part of the type for other parameters to that function:
void smallestSum(int size, double (*mat)[size], int num, double *first, double *second)

Getting all variables declared in a scope - C++

Is it possible to make a struct or class in C++ to get a list of the defined variables declared within a scope in C++. I don't want to hard-code these arrays.
For example, I have:
int a;
int b;
int c;
int d;
How can I get my program to print out what all the integers are in a program?
I want to be able to find the integers in the program that are incremented by one. (allints)++?
If one needs to iterate over a bunch of objects of the same type the typical data structure one uses is an array (old school) or a collection of some kind, depending on secondary requirements like uniqueness, sortedness etc. (new school). This would work with arrays of built-in types as well as with arrays of structs.
At some stage, you'll need to hardcode it.
You can create a class that contains your ints and make a function to autoincrement all by one.
Something like:
class allints {
int a, b, c, d;
public:
void autoincrement() {
++a, ++b, ++c, ++d;
}
}
Or simply a vector of ints and loop it to autoincrement, as following:
allints = vector<int> (4, 0); //size 4, at the start value of 0.
void autoincrement(vector<int> v) {
for(int i: v) ++i;
}
EDIT:
Noticing that you would be using structs or Classes, just group them in a vector and iterate them (like my 2nd example above)
Inside the class constructor I do vectorofPlatforms.pushback(this);
I have posted my own answer as none of you had thought of this approach, but I've giving you upvotes for your nice answers.