[SOLVED] How to convert vector iterator to int?
-
wrote on 19 Jul 2012, 14:47 last edited by
[quote author="Zlatomir" date="1342707933"]The first code you posted should work, the problems are somewhere else (in the code that you didn't post), also for the operator() use const references as parameters (you passed by value), anyway here is a simple piece of code that works:
@
#include <vector>
#include <iostream>
#include <QString>
#include <algorithm>class theList
{
QString procName;
public:
short procID;
short exist;
void set_value(QString name, short e, short id)
{
procName = name;
exist = e;
procID = id;
}
};struct myclass {
bool operator() (const theList& left, const theList& right) { //here use const & instead of value
return(left.procID < right.procID );
}
} myobject;int main()
{
std::vector<theList> actualList;
for(int i = 0; i != 10; i++)
{
theList obj;
obj.set_value("o1", 10 - i, 10 - i);
actualList.push_back(obj);
}
for(int i = 0; i != 10; i++)
{
std::cout << actualList[i].procID << " ";
}
std::cout << std::endl;std::sort(actualList.begin(), actualList.end(), myobject);
for(int i = 0; i != 10; i++)
{
std::cout << actualList[i].procID << " ";
}
}
@
You can use it as an example to fix yours or you can post more code and tell us the errors you get.[/quote]Yes!!! :D
Thanks very much Zlatomir.
This is working. -
wrote on 19 Jul 2012, 15:04 last edited by
Now it's up to you to fix your project ;) //ask when you are in trouble
Also it doesn't hurt to know the other approach that was mentioned before: overloading operator< for your class. -
wrote on 19 Jul 2012, 15:08 last edited by
Thanks Zlatomir.
I fixed my project ;).
Continue "here":http://qt-project.org/forums/viewthread/18919/.
21/23