[SOLVED] How to convert vector iterator to int?
-
I don't recommend you to combine STL and Qt at this case. It would be better to use "QVector":http://qt-project.org/doc/qt-4.8/QVector.html in a Qt app.
-
[quote author="leon.anavi" date="1342706182"]I don't recommend you to combine STL and Qt at this case. It would be better to use "QVector":http://qt-project.org/doc/qt-4.8/QVector.html in a Qt app.[/quote]
I guess using of STL is the least problem.
-
[quote author="Wilk" date="1342706373"][quote author="leon.anavi" date="1342706182"]I don't recommend you to combine STL and Qt at this case. It would be better to use "QVector":http://qt-project.org/doc/qt-4.8/QVector.html in a Qt app.[/quote]
I guess using of STL is the least problem.[/quote]
Well at least seems like a bad design for this case.
Btw I didn't understand what is not working. Is the code compiling? Where is actualList initialized?
-
[quote author="leon.anavi" date="1342706978"]
Well at least seems like a bad design for this case.
Btw I didn't understand what is not working. Is the code compiling? Where is actualList initialized?
[/quote]On this code I give ~15+ error. :D
-
[quote author="Wilk" date="1342706158"]Why do you use operator()? As you may see "here":http://www.cplusplus.com/reference/algorithm/sort/ youк class must implement operator< for using it with sort algorithm.[/quote]
So, did you think this?
@class theList
{
QString procName;
public:
short procID;
short exist;
void set_value(QString, short, short);
theList operator< (theList);
};theList theList::operator <(theList param)
{
theList *temp = new theList;
temp = this;
if(temp->procID < param.procID)
std::swap(temp, param);
return (temp);
}@I got 19 errors.
-
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. -
Mulţumesc. Am să încerc.
[Edit: Google translation: Thank you. I will try. -- mlong]
-
[quote author="I-sty" date="1342708076"]Mulţumesc. Am să încerc.[/quote] Use english in the english part of the forum, so that everybody can understand what we are talking ;)
//we have a small romanian sub-forum, there we can use use romanian language
//I forgot to include QString (not that is matters - as it's only example code) in the code above - it's updated now. -
I apologize from everyone. :)
-
[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. -
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. -
Thanks Zlatomir.
I fixed my project ;).
Continue "here":http://qt-project.org/forums/viewthread/18919/.