[SOLVED] How to convert vector iterator to int?
-
wrote on 19 Jul 2012, 10:33 last edited by
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.
-
wrote on 19 Jul 2012, 10:57 last edited by
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.
-
wrote on 19 Jul 2012, 11:01 last edited by
[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. -
wrote on 19 Jul 2012, 11:11 last edited by
How familiar are you with C++ and templates?
-
wrote on 19 Jul 2012, 11:13 last edited by
[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.
-
wrote on 19 Jul 2012, 11:27 last edited by
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. -
wrote on 19 Jul 2012, 11:36 last edited by
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. :(
-
wrote on 19 Jul 2012, 11:43 last edited by
I guess it would be easier to implement operator< for your class that's stored in your actualList (I mean objects of this class are in actualList).
-
wrote on 19 Jul 2012, 12:55 last edited by
[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/.
-
wrote on 19 Jul 2012, 13:47 last edited by
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);
};@ -
wrote on 19 Jul 2012, 13:55 last edited by
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.
-
wrote on 19 Jul 2012, 13:56 last edited by
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.
-
wrote on 19 Jul 2012, 13:59 last edited by
[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.
-
wrote on 19 Jul 2012, 14:09 last edited by
[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?
-
wrote on 19 Jul 2012, 14:11 last edited by
[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
-
wrote on 19 Jul 2012, 14:13 last edited by
[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.
-
wrote on 19 Jul 2012, 14:25 last edited by
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. -
wrote on 19 Jul 2012, 14:27 last edited by
Mulţumesc. Am să încerc.
[Edit: Google translation: Thank you. I will try. -- mlong]
-
wrote on 19 Jul 2012, 14:33 last edited by
[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. -
wrote on 19 Jul 2012, 14:38 last edited by
I apologize from everyone. :)
6/23