C++ STL list size - c++

i've got some problem/ i don't understand something and it cause this problem :)
Here is some code:
list<Walec>lista;
remove_copy_if(vec.begin(),vec.end(), lista.begin(),Warunek_na_Wysokosc(sredniasu));
copy(lista.begin(),lista.end(),ostream_iterator<Walec>(cout, " \n"));
And here is Warunek_Na_Wysokosc definition:
struct Warunek_na_Wysokosc
{
double wys;
Warunek_na_Wysokosc(const double& h_): wys(h_){ }
bool operator()(const Walec& w)
{
return w.h >= wys;
}
};
The thing is i get some memory problems if i let it be like that, i mean instructions in main function, and there are none if i set list size. But i want it to be set automatically, should i use some insert function? How to correct this? :) Thanks!

I think you want to use inserter here:
remove_copy_if(vec.begin(),vec.end(),
std::inserter(lista,lista.end()),
Warunek_na_Wysokosc(sredniasu));

Related

How can i use bool to create a file

Hi can someone help me with this function:
bool createfile (string path);
It is supposed to create a file but my problem is:
What exactly the true or false have to do with creating a file?! How can I use it?
The bool is the return type of the function createfile(). Without the definition it is impossible to tell for sure what exactly this value is supposed to be, but often it is used to return if the function was successful in doing what it is supposed to do, in this case, create a file.
What exactly the true or false have to do with creating a file?!
You might want to return true if the file was successfully created or false otherwise.
How can I use it?
This depends on the body of the function and the purpose that you want to use the function for.
Quick answer
To directly answer the "How can I use it" part of your question:
You call it this way:
string path = "/path/to/my/file.txt";
bool returnedValue = createfile(path);
As for "What exactly the true or false have to do with creating a file?!", like mentionned in the other answers, it might indicate the success or failure of the operation, but you might want to double-check that, because the actual value will depend on the implementation of bool createfile(string path)...
Comprehensive answer
It seems you need some help interpreting the syntax of bool createfile(string path);
What we need to clarify here is that in c++ (and many other languages), the first word used in the function declaration is the return type.
You could compare this to some arbitrary mathematical function of the following form: here
x = a + b
In this case, x is the result of the addition function.
Assuming all the elements above are numbers, we could translate this in c++, like so:
int a = 0;
int b = 5;
int x = a + b;
We could extract the example above in a function (to reuse the addition), like so:
int add(int a, int b)
{
return a + b;
}
and use it in the following way (with a main to put some execution context around it):
int main()
{
int x = add(0,5);
return 0;
}
Here are some other examples of functions:
// simple non-member function returning int
int f1()
{
return 42;
}
// function that returns a boolean
bool f2(std::string str)
{
return std::stoi(str) > 0;
}
You'll find more details here. It might seem like a lot to take in (the page is dense with information), but it is a true reference.

variables alteration in c++

i got a ridiculous problem.
i have a class within inside an array member.i have a get method and a set method for the array.
the problem is that i call the set(to update) method to change the variables within the array and i see with the debugger that the variables do actually update.then when i call immediately the get method just after the set method i found the variables of the array been changed back to their ancient values.
here is the code approximately :
object.updatFunction();//sort of set method
//nothing in between
Type variable=object.getFunction();
added code:
void Cube::updtCornersNextToCentr()
{
int iHalfSide=m_SideSize/2;
int centerX(m_Center.x()),centerY(m_Center.y()),centerZ(m_Center.z());
m_CubeCornerVertices[0].setX(centerX-iHalfSide);
m_CubeCornerVertices[0].setY(centerY+iHalfSide);
m_CubeCornerVertices[0].setZ(centerZ-iHalfSide);
m_CubeCornerVertices[1].setX(centerX+iHalfSide);
m_CubeCornerVertices[1].setY(centerY+iHalfSide);
m_CubeCornerVertices[1].setZ(centerZ-iHalfSide);
//.......
m_CubeCornerVertices[7].setX(centerX+iHalfSide);
m_CubeCornerVertices[7].setY(centerY-iHalfSide);
m_CubeCornerVertices[7].setZ(centerZ+iHalfSide);
}
QVector3D * Cube::getCubeCornerVertices()const
{
static QVector3D temp[8];
for(int i=0;i<8;i++)
{
temp[i]=m_CubeCornerVertices[i];
}
return &temp[0];
}
The problem was really ridiculous, i didn’t want to let this post ambiguous.it’s a very beginner fault and it’s all about a missing « & » that caused me to update a copy.
Actually i did above simplify write the next code :
object.updatFunction();//sort of set method
//nothing in between
Type variable=object.getFunction();
And the more real code was something like:
m_WorldSpace->getCube().updtCornersNextToCentr()
//nothing in between
const QVector3D corners[8]=m_WorldSpace->getCube().getCubeCornerVertices();
all the problem was in the getCube() function which instead of being something like this :
Cube& WorldSpace::getCube()//here is the missing "&"
{
return m_Cube;
}
I wrote this
Cube WorldSpace::getCube()//this caused to get and update just a temporary copy
{
return m_Cube;
}
someone can say that i got multiple levels of getters which blinded me.
thank you.

c++ Iterate through a list to call a certain function

E.g. a class Unit has three functions:
class Unit{
void StandUp();
void SitDown();
void Die();
}
I have a list of pointers list<Unit*> UnitList;
When I want everyone to stand up:
void EveryoneStandUp(){
for(list<Unit*> it = UnitList.begin(); it != UnitList.eng(); it++){
(*it)->StandUp();
}
}
Now if I want everyone to SitDown, I would copy the code above and change StandUp() to SitDown(). For every new function I write, if I want everyone to do it, I have to have another for-loop body in my code.
Is it possible to put this for-loop body in another function, which I can reuse whenever I want to call a certain function from all of the members in the UnitList?
I feel like this must have answers somewhere else, I tried googling but have little idea which keywords I should look for. Thanks for answers!
You may do:
void Everyone(void (Unit::*method)())
{
for (std::list<Unit*>::iterator it = UnitList.begin(); it != UnitList.end(); it++){
((*it)->*method)();
}
}
And call it
Everyone(&Unit::StandUp);
but in c++11, your example may be rewritten as:
for (auto* unit : UnitList) {
unit->StandUp();
}
which seems clear enough.
you can use c++ algorithms available,
http://www.cplusplus.com/reference/algorithm/for_each/
It can be solved by having a helper function, which does the actual looping and have the member function to be called as an argument.
Something like this:
void UnitHelperFunction(void (Unit::*func)())
{
for (...)
{
((*itr)->*func)();
}
}
void EveryoneStandUp()
{
UnitHelperFunction(&Unit::StandUp);
}

Wrap native method with input parameter fixed size array

I have a problem with wrapping methods on the C++ CLI, which have a fixed size array as input parameter.
This is the method I need to wrap:
BOOL SetNetworkMask(BYTE ucValue[4]);
What is a decent method to do this? Of all the possibilities, I tried this one, but It seems the most stupid:
bool RFDeviceWrap::SetNetworkMask(byte ucValue[4])
{
return this->m_RFDevice->SetNetworkMask(ucValue[4]);
}
All arrays degrade to pointers, so you can use this :
bool RFDeviceWrap::SetNetworkMask(byte * ucValue) {
return this->m_RFDevice->SetNetworkMask(ucValue[4]);
}

How could I change a vector which has points into a vector which has their value

I just has a vector like that
vector<User*> users;
I know it is not a good programming style...
now I have a function
vector<User> getAllUser(void)
{
}
What I had tried is
vector<User> getAllUser(void)
{
vector<User> result;
for (vector<User*>::iterator it = users.begin(); it != users.end(); it++)
{
result.push_back(**it);
}
return result;
}
But it didn't work.
Could someone to help me? Thankyou very much. I am just a beginner to STL
You code should work, but you made a typo:
result.push_bach(**it);
push_bach is not a declared function for std::vector, so I'm assuming that's where the error lies. I would recommend you get a decent compiler, which should point this error out to you right away, without having to go through stackoverflow.
To fix, use the proper method name, push_back instead:
result.push_back(**it);
If you must use pointers, use a nice std::shared_ptr<User> wherever you would use User*, and don't mix and match heap and stack allocated objects.
If you didn't really need to use pointers everywhere, make sure that User has a copy constructor defind (eg, User::User(const User& rhs) { /* ... */ })