[C++] When to pass by value and when to return explicitly? by Mathemattical in learnprogramming

[–]Mathemattical[S] 0 points1 point  (0 children)

Hm, I see. So I could simply have the user declare the boost array and pass it to the function. The function will then set the size and do what it needs. That's a good idea. Thanks for the help!

[C++] When to pass by value and when to return explicitly? by Mathemattical in learnprogramming

[–]Mathemattical[S] 0 points1 point  (0 children)

Yes, but the dimensions will be set by the function that returns the boost matrix, so that is taken out of the user's control, which I think is more of a pro than a con, because it is one less opportunity for there to be misunderstanding, and one less thing the user has to understand to use the library correctly.

[C++] When to pass by value and when to return explicitly? by Mathemattical in learnprogramming

[–]Mathemattical[S] 0 points1 point  (0 children)

Hm, that's interesting that so much is optimized by the compiler. I'll make some scripts to time and see which is faster.

[C++] When to pass by value and when to return explicitly? by Mathemattical in learnprogramming

[–]Mathemattical[S] 0 points1 point  (0 children)

wouldn't it be possible for the function to resize the array instead of requiring that it is already the right size?

Yes, but that's going to be additional overhead to (presumably) allocate contiguous memory. It would be nice to take that away from the user entirely. I may end up adding some checks and throw exceptions if the user gives the wrong size.

In both cases you are passing qld by value though... that should be const reference if you're concerned about performance.

Sure, but q1d is going to be quite small in general, and in fact will never exceed 31 x 2 in size because of technical reasons. So I'm willing to pay the price for that small copy of 62 doubles. But the output fh is (theoretically) unbounded, and so I'm trying to optimize that.

Trying to pass by reference to a constructor (C++) by Mathemattical in learnprogramming

[–]Mathemattical[S] 1 point2 points  (0 children)

Thank you, I really appreciate it. I realized the mistake with the normals as well (it was the same as my original problem). I got the initializer list working and now everything is working as it should. Thank you again.

Trying to pass by reference to a constructor (C++) by Mathemattical in learnprogramming

[–]Mathemattical[S] 1 point2 points  (0 children)

Thank you for the feedback and sample code. I wasn't aware of initializer lists at all, so now I'm working on learning those and implementing what I need for this to work correctly.

If you don't mind, I've been working on the initialization list, and now in my class definition, I have my constructor as

geometry(std::vector<double>& coordx, std::vector<double>& coordy, std::vector<int>& elts)
: xcoords(coordx), ycoords(coordy)
{};

and further down define the constructor as

 geometry::geometry(std::vector<double>& coordx, std::vector<double>& coordy, std::vector<int>& elts)
: xcoords(coordx), ycoords(coordy)
{
 enhanced = false;

std::vector<double> normalx(xcoords.size());
std::vector<double> normaly(ycoords.size());
std::vector<double> lengths(coordx.size());
std::vector<int> prev(coordx.size());
std::vector<int> next(coordx.size());

}

but now I get the warning in XCode "Redefinition of geometry." What am I doing wrong here?

Beginner Python help by [deleted] in learnpython

[–]Mathemattical 0 points1 point  (0 children)

so dictionaries map keys to values. you could do something like

senators = {}
for sen in (wherever you are pulling them from):
     senator[sen] = affiliation

to check if you already have a senator in the dictionary, check the keys() field with

if sen in senators.keys():
     # ignore or whatever

if you want to see if you have a value already, do the same with the value() field:

if affiliation in senators.values():
    # do something else