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. check if a value is already present in vector
Forum Updated to NodeBB v4.3 + New Features

check if a value is already present in vector

Scheduled Pinned Locked Moved Solved C++ Gurus
9 Posts 4 Posters 980 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.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on last edited by Natural_Bugger
    #1

    Hello,

    i'm trying create a database of name & email's.
    but i end up with 2049 records in the vector according to QT Creator debug, instead of the 30 delivered.

           vector< pair <string, string> > data;
    
    for(........){
    
            string firstName = ...........; // automated data
            string emailID = ................;// automated data
    
            if(data.begin() == data.end()){
            //if(data.empty() == 0){
                std::cout << "add first record" << std::endl;
                data.push_back( make_pair(firstName,emailID) );
            }
            else{
            for(auto ii : data){
                std::cout << "ii: " << ii.first << std::endl;
                if((ii.first == firstName)&&((ii.second == emailID))){
                    std::cout << "already in database" << std::endl;
                }
                else{
                    data.push_back( make_pair(firstName,emailID) );
                }
            }
            }
    }
    

    what am i doing wrong?
    everything looks ok, if i view the data in QT Creator where it holds due to a segmentation fault.

    if((ii.first == firstName)&&((ii.second == emailID))){
    

    compare values are valid where it stals.
    accept having way more records in the vector than expected.

    this is the data:
    riya riya@gmail.com
    julia julia@julia.me
    julia sjulia@gmail.com
    julia julia@gmail.com
    samantha samantha@gmail.com
    tanya tanya@gmail.com
    riya ariya@gmail.com
    julia bjulia@julia.me
    julia csjulia@gmail.com
    julia djulia@gmail.com
    samantha esamantha@gmail.com
    tanya ftanya@gmail.com
    riya riya@live.com
    julia julia@live.com
    julia sjulia@live.com
    julia julia@live.com
    samantha samantha@live.com
    tanya tanya@live.com
    riya ariya@live.com
    julia bjulia@live.com
    julia csjulia@live.com
    julia djulia@live.com
    samantha esamantha@live.com
    tanya ftanya@live.com
    riya gmail@riya.com
    priya priya@gmail.com
    preeti preeti@gmail.com
    alice alice@alicegmail.com
    alice alice@gmail.com
    alice gmail.alice@gmail.com

    "some" people have 2 email addresses

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      C++ basics - you modify the vector while iterating over it - this can't work.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      N 1 Reply Last reply
      1
      • N Natural_Bugger

        Hello,

        i'm trying create a database of name & email's.
        but i end up with 2049 records in the vector according to QT Creator debug, instead of the 30 delivered.

               vector< pair <string, string> > data;
        
        for(........){
        
                string firstName = ...........; // automated data
                string emailID = ................;// automated data
        
                if(data.begin() == data.end()){
                //if(data.empty() == 0){
                    std::cout << "add first record" << std::endl;
                    data.push_back( make_pair(firstName,emailID) );
                }
                else{
                for(auto ii : data){
                    std::cout << "ii: " << ii.first << std::endl;
                    if((ii.first == firstName)&&((ii.second == emailID))){
                        std::cout << "already in database" << std::endl;
                    }
                    else{
                        data.push_back( make_pair(firstName,emailID) );
                    }
                }
                }
        }
        

        what am i doing wrong?
        everything looks ok, if i view the data in QT Creator where it holds due to a segmentation fault.

        if((ii.first == firstName)&&((ii.second == emailID))){
        

        compare values are valid where it stals.
        accept having way more records in the vector than expected.

        this is the data:
        riya riya@gmail.com
        julia julia@julia.me
        julia sjulia@gmail.com
        julia julia@gmail.com
        samantha samantha@gmail.com
        tanya tanya@gmail.com
        riya ariya@gmail.com
        julia bjulia@julia.me
        julia csjulia@gmail.com
        julia djulia@gmail.com
        samantha esamantha@gmail.com
        tanya ftanya@gmail.com
        riya riya@live.com
        julia julia@live.com
        julia sjulia@live.com
        julia julia@live.com
        samantha samantha@live.com
        tanya tanya@live.com
        riya ariya@live.com
        julia bjulia@live.com
        julia csjulia@live.com
        julia djulia@live.com
        samantha esamantha@live.com
        tanya ftanya@live.com
        riya gmail@riya.com
        priya priya@gmail.com
        preeti preeti@gmail.com
        alice alice@alicegmail.com
        alice alice@gmail.com
        alice gmail.alice@gmail.com

        "some" people have 2 email addresses

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @Natural_Bugger
        Additional to @Christian-Ehrlicher: even if your code did not crash the iteration, it would add a new pair every time an existing pair did not match. This can hardly be what is intended, can it?

        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          C++ basics - you modify the vector while iterating over it - this can't work.

          N Offline
          N Offline
          Natural_Bugger
          wrote on last edited by
          #4

          @Christian-Ehrlicher @JonB

          what would be the approach then?
          if i can't iterate, but yet have to check if the value exists.

          JonBJ 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Natural_Bugger said in check if a value is already present in vector:

            what would be the approach then?

            use a new vector, iterator only over the initial size, ...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • N Natural_Bugger

              @Christian-Ehrlicher @JonB

              what would be the approach then?
              if i can't iterate, but yet have to check if the value exists.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @Natural_Bugger
              Of course you can iterate. But you cannot modify the list by adding while you do so. Clearly you need to iterate to check if something is present, and only add after the iteration if it was not.

              1 Reply Last reply
              1
              • J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7
                template<typename T1, typename T2>
                bool contains(T1 container, T2 value){
                     auto result = std::find(container.begin(), container.end(), value);
                     return  result != std::end(container);
                }
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                
                
                    std::vector<int> v {1,2,3,4,5,6,7};
                
                    for(int i (0); i < 10; i++) {
                        if(!contains(v,i))
                            v.push_back(i);
                    }
                
                    qDebug() << v;
                
                
                //    return a.exec();
                }
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                3
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #8

                  @J-Hilk : I would change it to

                  std::vector<int> v {1,2,3,4,5,6,7};
                  const auto initialSize = v.size();
                  for (int i = 0; i < initialSize; i++) {
                    if (!contains(v, i))
                      v.push_back(i);
                  }
                  

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  N 1 Reply Last reply
                  2
                  • Christian EhrlicherC Christian Ehrlicher

                    @J-Hilk : I would change it to

                    std::vector<int> v {1,2,3,4,5,6,7};
                    const auto initialSize = v.size();
                    for (int i = 0; i < initialSize; i++) {
                      if (!contains(v, i))
                        v.push_back(i);
                    }
                    
                    N Offline
                    N Offline
                    Natural_Bugger
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher @J-Hilk @JonB

                    thank you all.

                    this is how i solved it, i just had to adapt it to pair.

                            if (std::find(data.begin(), data.end(), std::pair<std::string, string>(firstName, emailID)) != data.end())
                                    std::cout << "Element found";
                            else{
                                    std::cout << "Element not found";
                                    data.push_back( make_pair(firstName,emailID) );
                                }
                    
                    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