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 Updated to NodeBB v4.3 + New Features

[SOLVED] How to convert vector iterator to int?

Scheduled Pinned Locked Moved C++ Gurus
23 Posts 5 Posters 15.3k Views 1 Watching
  • 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 last edited by
    #10

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

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

    1 Reply Last reply
    0
    • W Offline
      W Offline
      Wilk
      wrote on last edited by
      #11

      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.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on last edited by
        #12

        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.

        http://anavi.org/

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Wilk
          wrote on last edited by
          #13

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

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leon.anavi
            wrote on last edited by
            #14

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

            http://anavi.org/

            1 Reply Last reply
            0
            • I Offline
              I Offline
              I-sty
              wrote on last edited by
              #15

              [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

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

              1 Reply Last reply
              0
              • I Offline
                I Offline
                I-sty
                wrote on last edited by
                #16

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

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

                1 Reply Last reply
                0
                • ZlatomirZ Offline
                  ZlatomirZ Offline
                  Zlatomir
                  wrote on last edited by
                  #17

                  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.

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

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    I-sty
                    wrote on last edited by
                    #18

                    Mulţumesc. Am să încerc.

                    [Edit: Google translation: Thank you. I will try. -- mlong]

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

                    1 Reply Last reply
                    0
                    • ZlatomirZ Offline
                      ZlatomirZ Offline
                      Zlatomir
                      wrote on last edited by
                      #19

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

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

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        I-sty
                        wrote on last edited by
                        #20

                        I apologize from everyone. :)

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

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          I-sty
                          wrote on 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
                          • ZlatomirZ Offline
                            ZlatomirZ Offline
                            Zlatomir
                            wrote on 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 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

                              • Login

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