[SOLVED] How to convert vector iterator to int?
-
Hi all,
I have two vector.
The vectors are same, just the name and type are different.
I sorting the first with quicksort.
How can I sorting both vectors in this algorithm?I think the part of swap I pick the iterator by first vector I converted to integer and I swapped the second vec.
-
AFAIK the "difference_type":http://qt-project.org/doc/qt-4.8/qvector.html#difference_type-typedef is for that.
However, I am not sure if it is a good idea in your case. Sounds a bit odd. Apparently, you have to make sure that elements of both vectors are staying aligned. So why are you not combing both in one class supporting a comparison operator?
IMHO it looks like a nasty hack what you are trying to achieve.
-
[quote author="koahnig" date="1342695468"]AFAIK the "difference_type":http://qt-project.org/doc/qt-4.8/qvector.html#difference_type-typedef is for that.
However, I am not sure if it is a good idea in your case. Sounds a bit odd. Apparently, you have to make sure that elements of both vectors are staying aligned. So why are you not combing both in one class supporting a comparison operator?
IMHO it looks like a nasty hack what you are trying to achieve. [/quote]
Thanks, your reply koahnig.
Sorry, I don't understand :(.
I am noob. -
[quote author="koahnig" date="1342696263"]How familiar are you with C++ and templates? [/quote]
I learned the class yesterday AM, from "here":http://www.cplusplus.com/doc/tutorial/classes/ :))
But, I'm learning quickly. :DLearned => I have read 3 times.
-
Hello.
If you vectors are the same, why don't you just copy the sorted one into unsorted?
Another question: do you implement qsort yourself? If so, I suggest you not to use iterators for the implementation - just use indexes: it's easier and, probably, quicker. -
I implemented to a file.
The vectors have different types. The first: std::vector <int> tmpVector; and the second is a type of class: std::vector <'class name'> actualList;
So, the second vector have one QString cell, and two integer field.
I want to sort my actualList by the way of ones integer field. So, the first vector in fact is created by these fields.Is a bit too complicating. :(
-
[quote author="koahnig" date="1342695468"]However, I am not sure if it is a good idea in your case. Sounds a bit odd. Apparently, you have to make sure that elements of both vectors are staying aligned. So why are you not combing both in one class supporting a comparison operator?
[/quote]I totally agree! The OOP solution described by koahnig is much better. If you are using STL vector you can sort it using the sort method and a compare method as "shown at this example":http://www.cplusplus.com/reference/algorithm/sort/.
-
I have written this code, but of course that not working.
//sort.h
@struct myclass {
bool operator() (theList left, theList right) {
return(left.procID < right.procID );
}
} myobject;void sort()
{
std::sort(actualList.begin(), actualList.end(), myobject);
}
@@class theList;
extern std::vector <theList> actualList;
...class theList
{
QString procName;
public:
short procID;
short exist;
void set_value(QString, short, short);
};@ -
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.
-
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="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. -
[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.