I'm trying to understand this block of code here:
#include <iostream>
using namespace std;
#define mymult(a, b) a*b
inline int mymult1(int a, int b) {return a*b;}
int main() {
cout << "mymult(2+2, 3+3) = " << mymult(2+2, 3+3) << "\n";
cout << "mymult1(2+2, 3+3) = " << mymult1(2+2, 3+3) << "\n";
}
mymult = 11, and mymult1 = 24. I know that '#define's essentially work via call by name, rather than call by value. However, I'm having trouble understanding why the value it returns is 11... and not 24. What causes this?
Option 1:
In the case of:
#define mymult(a, b) a*b
a and b are treated like place holder strings and when you call mymult, the parameters a and b are just copied as they were written. In other words:
mymult(2+2, 3+3) = 2+2*3+3
where a = 2+2, b = 3+3.
Therefore you may call mymult as follows:
mymult( (2+2), (3+3) )
where a = (2+2), b = (3+3).
This will be interpreted as:
mymult( (2+2), (3+3) ) = (2+2)*(3+3)
and return the value of 24 as expected.
Option 2:
If we are allowed to modify the #define statement then an alternative way of doing this is defining it with the parentheses as follows:
#define mymult(a, b) (a)*(b)
This will give the same expected result since a and b will be put directly into the parentheses as they are. In other words:
mymult(2+2, 3+3) = (2+2)*(3+3) = 24
where a = 2+2, b = 3+3.
Option 3:
Stick with the inline function as defined in OP:
inline int mymult(int a, int b) {return a*b;}
Good luck!
Related
I wanted to write a function to swap two integers. Despite everything looks fine, values are not swapped.
Here is my code:
#include <iostream>
using namespace std;
void mySwap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int a = 5, b = 4;
mySwap(a, b);
cout << a << ' ' << b << endl;
return 0;
}
Output: 5 4
Please, help me understand the reason. Any help is appreciated.
You are copying arguments a and b. Change them to reference.
void mySwap(int & a, int & b)
What you are doing right now is swapping a local Copy of the variables instead of the real variables.
What you need to do, to fix it is to change your function a little bit(just add &, this makes it take a reference of the variables and not create a local copy of them)
void mySwap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
To manipulate with the values passed as arguments in main() directly, use a reference (followed by an ampersand sign &) as shown:
#include <iostream>
void mySwap(int& a, int& b) // using reference here
{
int temp; // remove for alternative option described at the bottom
temp = a; // a = a + b;
a = b; // b = a - b;
b = temp; // a = a - b;
}
int main(void)
{
int a = 5, b = 4;
mySwap(a, b); // passed 5, 4 and original values are manipulated.
std::cout << a << ' ' << b << std::endl;
return 0;
}
As soon as you pass the variables as the function arguments, it'll change originally too.
On the contrary, if you don't use that, the program will just create two local variables that will only be visible inside the function, then even after swapping them, you won't get success.
Another method of swapping between two variables (without a temporary variable):
a = a + b;
b = a - b;
a = a - b;
Edit: I replaced the phrase 'in one line' by 'in a single line statement' since this is what I was looking for
Let's say we have the following variables at hand:
int a = 5;
int b = 9;
Is there a way to compress this ...
a--;
b--;
... into in a single line statement??
The question is not about decrementing multiple variables in a for loop,
since this seems to be a common yet unrelated question.
You probably mean "in a single statement", not just "in a single line".
Then you can use the comma-operator:
(a--,b--);
// use a template
template<class ... Args>
void decr(Args& ... args){
(... , --args);
}
decr(a,b,c);
// or, in C++20, auto
void decr(auto& ... args){
(... , --args);
}
You could just write the statements in one single line, like this :
a--, b--;
(thanks to #Aziz for the improvement with the comma instead of the semicolon)
You can do like :
int a = 5;
int b = 4;
(a -= 1), (b -= 1);
std::cout << a << b;
Output: 43
You can try something like :
#include <iostream>
using namespace std;
main ()
{
int a = 5, b = 9;
a--, b--;
cout << a;
cout << b;
return 0;
}
Output:
48
I am quite new to c++ and I am building a model studying certain mutations in genes. My "genes" are defined as a function of two doubles, a and b. A single gene is saved in a std::pair format. The whole genome consists of four of these genes collected in a std:array.
I perform some changes on the genes and want to write the information in a text file for analysis. The way I have currently implemented this is tedious. I have separate functions (8 in total) which collect the information like g[i].first, g[i[.second etc. for every i in the array. I feel this could be done much more efficiently.
Relevant code:
Declaration of data type:
using gene = std::pair<double, double>;
using genome = std::array<gene, 4>;
Function in which I create a genome called g:
genome Individual::init_Individual()
{
double a1, a2, a3, a4 = -1.0;
double b1, b2, b3, b4 = 0.0;
gene g1{ a1,b1 };
gene g2{ a2,b2 };
gene g3{ a3,b3 };
gene g4{ a4,b4 };
genome g{g1,g2,g3,g4};
return g;
}
Example of collect function:
double get_Genome_a1() { return g[0].first; };
Function in which I write information to a text file:
void Individual::write_Statistics(unsigned int &counter)
{
//Generate output file stream
std::ofstream ofs;
ofs.open("data.txt", std::ofstream::out | std::ofstream::app);
ofs << counter << std::setw(14) << get_Genome_a1() << std::setw(14)
<< get_Genome_a2() << std::setw(14) << get_Genome_b1() <<
std::setw(14) << get_Genome_b2() << "\n";
}
ofs.close();
}
etc, etc. So the final result of my data file in this example looks like this:
1 a1 a2 b1 b2
2 a1 a2 b1 b2
3 a1 a2 b1 b2
etc, etc.
My question:
I am currently storing the two doubles in a std::pair, which I collect in a std::array. Is this an efficient storage mechanism or can this be improved?
Is there a way to directly reference an individual element from my custom data type "genome" using only one function to write every element away in the exact same manner as I am doing now (with fourteen spaces between every element)? Something in pseudocode like: get_Genome() {return g;};, and when you call it you can specify the element like: get_Genome([0].first) which would be the first value stored in the first pair of the array, for example.
Happy to learn, any insight is appreciated.
Your storage is good. Neither pair nor array requires indirect/dynamic allocation, so this is great for cache locality.
As for referencing elements, no, not exactly like that. You could have an enum with members FIRST, SECOND then pass that as another argument to get_Genome. But, honestly, this doesn't seem to me to be worthwhile.
Overall, your approach looks great to me. My only suggestions would be:
Re-use one ofstream
…rather than opening and closing the file for every sample. You should see substantial speed improvements from that change.
You could make one in your main or whatever, and have write_Statistics take a std::ostream&, which would also be more flexible!
Initialise a bit quicker
All those declarations in init_Individual may get optimised, but why take the risk? The following is pretty expressive:
genome Individual::init_Individual()
{
const double a = -1.0;
const double b = 0.0;
return {{a, b}, {a, b}, {a, b}, {a, b}};
}
It's worth noting here that your double initialisations were wrong: you were only initialising a4 and b4; your compiler ought to have warned you about this. But, as shown, we don't need all of those anyway as they [are intended to] have the same values!
Your array looks good, however using std::pair in this situation might make it a bit more tedious. I would create 2 simple classes or structures one to represent a gene and the other to represent your genome. I'd still use array. The class might look something like this:
#include <array>
const int genesPerGenome = 4; // change this to set how many...
struct Gene {
double a_;
double b_;
Gene() = default;
Gene(double a, double b) : a_(a), b_(b) {}
};
struct Genome {
std::array<Gene, genesPerGenome> genome_;
int geneCount_{0};
Genome() = default;
void addGene(const Gene& gene) {
if ( geneCount_ >= genesPerGenome ) return;
genome_[geneCount_++] = gene; // post increment since we added one
}
};
Then I would have a stand alone function that would generate your genome as such:
void generateGenome( Genome& genome ) {
for (int i = 0; i < 4; i++) {
// When looking at your example; I notices that the genes were all
// initialized with [-1.0,0.0] so I used Gene's constructor to init
// them with those values.
Gene gene(-1.0, 0.0);
genome.addGene(gene);
}
}
Then to couple these together, I'll just print them to the console for demonstration. You can then take this approach and apply it to what ever calculations that will be done and then writing the results to a file.
#include <array>
#include <iostream>
int main() {
Genome genome;
generateGenome( genome );
// printing to console here is where you would do your calculations then write to file
for ( int i = 0; i < 4; i++ ) {
if ( i >= genome.geneCount_ ) break; // prevent accessing beyond array bounds
std::cout << (i+1) << " [" << genome.genome_[i].a_ << "," << genome.genome_[i].b_ << "]\n";
}
return 0;
}
-Output- - No calculations, only the initialized values:
1 [-1,0]
2 [-1,0]
3 [-1,0]
4 [-1,0]
Maybe this will help. From here you can write a operartor<<() function that will take an ostream reference object and a const reference to a Genome and from there you should be able to print the entire Genome to file in a single function call.
-Edit-
User t.niese left a comment with a valid point that I had overlooked. I was using a static variable in the addGene() function. This would work okay as long as you are working only with a single Genome, but if you had more than one Genome object, every time you'd call the addGene() function this value would increase and you wouldn't be able to add more than gene to each genome due to the condition of the if statement in the addGene() function.
I had modified the original code above to fix this limitation. Here I removed the static variable and I introduced two new variables; one is a const int that represents how many genes per genome as it will be used to define the size of your array as well as checking against how many genes to add to that genome. The other variable I added is a member variable to the Genome class itself that keeps track of how many genes there are per each Genome object.
Here is an example of what i meant in my comment by overloading the operator [].
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
class Genome {
public:
typedef std::pair<double, double> gene;
private:
double a1 = -1.0, a2 = -1.0, a3 = -1.0, a4 = -1.0;
double b1 = 0.0, b2 = 0.0, b3 = 0.0, b4 = 0.0;
gene g1{ a1,b1 };
gene g2{ a2,b2 };
gene g3{ a3,b3 };
gene g4{ a4,b4 };
public:
Genome() {}
const double operator[] (std::string l) const {
if (l == "a1") {return g1.first;}
else if (l == "b1") {return g1.second;}
else if (l == "a2") {return g2.first;}
else if (l == "b2") {return g2.second;}
else if (l == "a3") {return g3.first;}
else if (l == "b3") {return g3.second;}
else if (l == "a4") {return g4.first;}
else if (l == "b4") {return g4.second;}
else {
throw std::invalid_argument("not valid label");
}
}
void setvalue(std::string l, double x) {
if (l == "a1") {g1.first = x;}
else if (l == "b1") {g1.second = x;}
else if (l == "a2") {g2.first = x;}
else if (l == "b2") {g2.second = x;}
else if (l == "a3") {g3.first = x;}
else if (l == "b3") {g3.second = x;}
else if (l == "a4") {g4.first = x;}
else if (l == "b4") {g4.second = x;}
else {
throw std::invalid_argument("not valid label");
}
}
void write_Statistics(unsigned int counter) {
std::ofstream ofs;
ofs.open("data.txt", std::ofstream::out | std::ofstream::app);
ofs << counter
<< std::setw(14) << (*this)["a1"] << std::setw(14) << (*this)["a2"]
<< std::setw(14) << (*this)["b1"] << std::setw(14) << (*this)["b2"] << "\n";
ofs.close();
}
}
};
I don't know if you may find useful to access to the individual genes by a label instead of an index, but this is what this overload do.
int main(int argc, char **argv) {
Genome a = Genome();
std::cout << a["b1"] << std::endl; #this prints 0
a.setvalue("b2", 3.0);
std::cout << a["b2"] << std::endl; #this prints 3
a.write_Statistics(0);
return 0;
}
I'm learning C++ through Sololearn. Below is a code to find the largest of two numbers.
#include <iostream>
using namespace std;
int max(int a, int b){
if (a > b) {
return a;
}
return b;
}
int main() {
cout << max(7, 4) << endl;
return 0;
}
Result - 7
But shouldn't it return b also since there's return b in function????
Only one return statement will execute within a function. As soon as the code encounters the first return it will immediately leave the function and no further code will execute.
The answer of CoryKramer says it all.
Still, to avoid the confusion you bumped into, I would prefer:
#include <iostream>
using namespace std;
int max(int a, int b){
if (a > b) {
return a;
}
else {
return b;
}
}
int main() {
cout << max(7, 4) << endl;
return 0;
}
Alternatively you could use:
return a > b ? a : b;
The latter line is a so called 'conditional expression' (or 'conditional operator'). If the phrase before the ? is true, it returns the part between the ? and the :, else it returns the part after the : .
It is explained in detail here.
if (a > b) (7>4) ==> Condition becomes True so return a executed and max function return from there only, its not reach to return b, that's why its not execute return b.
You can use in return a > b ? a : b operator.
Operator return will
terminate the current function and returns the result of the expression to the caller
http://en.cppreference.com/w/cpp/language/return
After you passed the condition
if (a>b)
edited -> thanks to athul
return will evaluate a and put it as result of function.
If a is lesser then b - you will not meet this condition and you will hit
return b;
To understand it, you may add:
cout << max(2, 4) << endl;
cout << max(2, 1) << endl;
into the main section.
PS it is better to use at least codeblocks, which is advised in LearnC++ to enter their examples
Working with the below source code (it is open source) and I've never seen parenthesis after a variable name. UDefEnergyH is definitely a variable as can be seen in line 1. Can anyone tell me what these parenthesis are doing? Don't really know how to Google this. Thanks.
bins[0] = UDefEnergyH.GetLowEdgeEnergy(size_t(0));
vals[0] = UDefEnergyH(size_t(0)); //Don't know what this does???
sum = vals[0];
for (ii = 1; ii < maxbin; ii++) {
bins[ii] = UDefEnergyH.GetLowEdgeEnergy(size_t(ii));
vals[ii] = UDefEnergyH(size_t(ii)) + vals[ii - 1];
sum = sum + UDefEnergyH(size_t(ii));
}
And it is declared here in the header file:
G4PhysicsOrderedFreeVector UDefEnergyH;
It appears operator() is overloaded for the tyupe of UDefEnerfyH.
One way to do this is this solution
#include <iostream>
using namespace std;
struct MJ {
void GetLowEdgeEnergy(size_t arg) {
cout << "GetLowEdgeEnergy, arg = " << arg << endl;
}
void operator ()(size_t arg) {
cout << "operator (), arg = " << arg << endl;
}
};
int main() {
MJ UDefEnergyH;
UDefEnergyH.GetLowEdgeEnergy(42);
UDefEnergyH(42);
return 0;
}
It seems you are referring to the field in the class G4SPSEneDistribution. Its type is G4PhysicsOrderedFreeVector. And have a look at its members here. As you can see there is operator() overloaded and apparently this is what is called on the second line. It is not very easy to find out what that does, but if you have a look at the comment in the header file for G4PhysicsVector, you will see:
00100 // Returns simply the value in the bin specified by 'binNumber'
00101 // of the dataVector. The boundary check will not be Done. If
00102 // you want this check, use the operator [].
This is what is known as direct initialization, in which it first constructs the object with '0' as an immediate parameter, and then assigns it to the first index of the vals array.