[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.


  • Moderators

    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.


  • Moderators

    How familiar are you with C++ and templates?



  • [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. :D

    Learned => 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. :(



  • 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).



  • [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);
    }
    @

    //mainwindow.h

    @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="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/.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.