Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [SOLVED] How to convert vector iterator to int?
Forum Update on Monday, May 27th 2025

[SOLVED] How to convert vector iterator to int?

Scheduled Pinned Locked Moved C++ Gurus
23 Posts 5 Posters 15.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    I-sty
    wrote on 19 Jul 2012, 14:47 last edited by
    #21

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

    Sorry, for my bad English. My first language is Hungarian.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      Zlatomir
      wrote on 19 Jul 2012, 15:04 last edited by
      #22

      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.

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • I Offline
        I Offline
        I-sty
        wrote on 19 Jul 2012, 15:08 last edited by
        #23

        Thanks Zlatomir.
        I fixed my project ;).


        Continue "here":http://qt-project.org/forums/viewthread/18919/.

        Sorry, for my bad English. My first language is Hungarian.

        1 Reply Last reply
        0

        21/23

        19 Jul 2012, 14:47

        • Login

        • Login or register to search.
        21 out of 23
        • First post
          21/23
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved