Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. How do you know the duplicate key insertion in STL map

How do you know the duplicate key insertion in STL map

Scheduled Pinned Locked Moved ATL / WTL / STL
c++
10 Posts 5 Posters 0 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.
  • S Offline
    S Offline
    sarath_babu
    wrote on last edited by
    #1

    Is there any way to indicate the duplicate key insertion in STL map. Here the key should be a class object.

    J 1 Reply Last reply
    0
    • S sarath_babu

      Is there any way to indicate the duplicate key insertion in STL map. Here the key should be a class object.

      J Offline
      J Offline
      Jonas Larsson
      wrote on last edited by
      #2

      #include < iostream > #include < map > using namespace std; int main() { map < int, int > themap; pair < map < int, int > ::iterator, bool > res = themap.insert(pair < int,int > (1,2)); if (res.second) cout << "a value was inserted" << endl; else cout << "a value was updated" << endl; res = themap.insert(pair < int,int > (1,3)); if (res.second) cout << "a value was inserted" << endl; else cout << "a value was updated" << endl; return 0; } If you have to be sure before inserting elements, you'll need to use find before inserting. --- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot

      S S 2 Replies Last reply
      0
      • J Jonas Larsson

        #include < iostream > #include < map > using namespace std; int main() { map < int, int > themap; pair < map < int, int > ::iterator, bool > res = themap.insert(pair < int,int > (1,2)); if (res.second) cout << "a value was inserted" << endl; else cout << "a value was updated" << endl; res = themap.insert(pair < int,int > (1,3)); if (res.second) cout << "a value was inserted" << endl; else cout << "a value was updated" << endl; return 0; } If you have to be sure before inserting elements, you'll need to use find before inserting. --- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot

        S Offline
        S Offline
        sarath_babu
        wrote on last edited by
        #3

        hi , I have an code, in that code i want know when i will get a duplicate value since values are generated at runtime. could you know the duplicate value by overloading < and == operators. #include #include class A { unsigned long la; unsigned long lb; unsigned long lc; public: A() { la=lb=lc=0; } A(unsigned long ln,unsigned long lm,unsigned long lo) { la=ln; lb=lm; lc=lo; } A(const A& objref) { la=objref.la ; lb=objref.lb; lc=objref.lc; } A& operator =(const A& objref) { if(this!=&objref) { la=objref.la; lb=objref.lb; lc=objref.lc; } return *this; } bool operator==(const A& objref) const { bool b=(((la==objref.la)&&(lb==objref.lb)&&(lc==objref.lc))?1:0); return b; } /* bool operator<(const A& objref) const { if((((la==objref.la)&&(lb==objref.lb)&&(lc==objref.lc))?1:0)) { cout<<"Attempting insert a duplicate value -"<<"("< using namespace std; typedef std::map Aptrmap; int main(int argc, char* argv[]) { A objOne(0,1,2); A objTwo(0,1,2); A objThree(0,1,1); A objFour(3,4,0),objFive(4,4,5); float *pfa,*pfb,*pfc,*pfd,*pfe; pfa= new float(1.10); pfb= new float(2.20); pfc= ne

        J 1 Reply Last reply
        0
        • S sarath_babu

          hi , I have an code, in that code i want know when i will get a duplicate value since values are generated at runtime. could you know the duplicate value by overloading < and == operators. #include #include class A { unsigned long la; unsigned long lb; unsigned long lc; public: A() { la=lb=lc=0; } A(unsigned long ln,unsigned long lm,unsigned long lo) { la=ln; lb=lm; lc=lo; } A(const A& objref) { la=objref.la ; lb=objref.lb; lc=objref.lc; } A& operator =(const A& objref) { if(this!=&objref) { la=objref.la; lb=objref.lb; lc=objref.lc; } return *this; } bool operator==(const A& objref) const { bool b=(((la==objref.la)&&(lb==objref.lb)&&(lc==objref.lc))?1:0); return b; } /* bool operator<(const A& objref) const { if((((la==objref.la)&&(lb==objref.lb)&&(lc==objref.lc))?1:0)) { cout<<"Attempting insert a duplicate value -"<<"("< using namespace std; typedef std::map Aptrmap; int main(int argc, char* argv[]) { A objOne(0,1,2); A objTwo(0,1,2); A objThree(0,1,1); A objFour(3,4,0),objFive(4,4,5); float *pfa,*pfb,*pfc,*pfd,*pfe; pfa= new float(1.10); pfb= new float(2.20); pfc= ne

          J Offline
          J Offline
          Jonas Larsson
          wrote on last edited by
          #4

          Some of your post was eaten and it's still not clear to me if you want to know if an item was a dublicate before or after you insert it into the map. Here's your code (rewritten some so I could get it to compile) [Edited:] Doh! I was under the impression that map<>::insert updates an elemet already in the map. That is not correct, sorry for the confusion! :doh: #include #include #include using namespace std; class A { unsigned long la; unsigned long lb; unsigned long lc; public: A() : la(0), lb(0), lc(0) {} A(unsigned long ln,unsigned long lm,unsigned long lo): la(ln), lb(lm), lc(lo) {} A(const A& o) : la(o.la), lb(o.lb), lc(o.lc) {} bool operator==(const A& o) const { return la == o.la && lb == o.lb && lc == o.lc; } bool operator !=(const A& o) const { return !(*this == o); } unsigned long sum() { return (la+lb+lc); } bool operator < (const A& o) const { if (la == o.la) if (lb == o.lb) return lc < o.lc; else return lb < o.lb; else return la < o.la; } friend ostream& operator<<(ostream& out, const A& o); }; ostream& operator<<(ostream& out, const A& o) { out<<"("< >& o) { out << "[" << o.first << "," << *(o.second) << "]"; return out; } } typedef std::map > Aptrmap; bool is_duplicate(const Aptrmap& themap, const A& o) { return themap.find(o) != themap.end(); } int main(int argc, char* argv[]) { A objOne(0,1,2); A objTwo(0,1,2); A objThree(0,1,1); A objFour(0,1,2); A objFive(4,4,5); boost::shared_ptr pfa(new float(1.10)), pfb(new float(2.20)), pfc(new float(3.30)), pfd(new float(4.40)), pfe(new float(5.50)); Aptrmap objmap; pair it; it =objmap.insert(std::make_pair(objFive,pfe)); // check if we inserted a new value or updated an old item if (it.second) cout << *(it.first) << " was inserted" << endl; else cout << *(it.first) << " was a duplicate" << endl;

          1 Reply Last reply
          0
          • J Jonas Larsson

            #include < iostream > #include < map > using namespace std; int main() { map < int, int > themap; pair < map < int, int > ::iterator, bool > res = themap.insert(pair < int,int > (1,2)); if (res.second) cout << "a value was inserted" << endl; else cout << "a value was updated" << endl; res = themap.insert(pair < int,int > (1,3)); if (res.second) cout << "a value was inserted" << endl; else cout << "a value was updated" << endl; return 0; } If you have to be sure before inserting elements, you'll need to use find before inserting. --- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            Or alternatively, use count, as that returns an integer that can be tested in a boolean fashion (makes for more concise code)

            J 1 Reply Last reply
            0
            • S Stuart Dootson

              Or alternatively, use count, as that returns an integer that can be tested in a boolean fashion (makes for more concise code)

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #6

              Could you please elaborate further? I'm really not sure what you mean by "use count". :) -- Pictures[^] from my Japan trip.

              S 1 Reply Last reply
              0
              • J Jorgen Sigvardsson

                Could you please elaborate further? I'm really not sure what you mean by "use count". :) -- Pictures[^] from my Japan trip.

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                I meant "use the count method" - assume we've got a map call a_map and that we're trying to put something keyed by the number 16 into it:

                if (!a_map.count(16)) a_map.insert(std::make_pair(16, something_else));
                

                rather than

                if (a_map.end() == a_map.find(16))
                   a_map.insert((std::make_pair(16, something_else)));
                

                It just always seems sort of wrong that there's no "test for the presence of a value in a map Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

                J T 2 Replies Last reply
                0
                • S Stuart Dootson

                  I meant "use the count method" - assume we've got a map call a_map and that we're trying to put something keyed by the number 16 into it:

                  if (!a_map.count(16)) a_map.insert(std::make_pair(16, something_else));
                  

                  rather than

                  if (a_map.end() == a_map.find(16))
                     a_map.insert((std::make_pair(16, something_else)));
                  

                  It just always seems sort of wrong that there's no "test for the presence of a value in a map Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

                  J Offline
                  J Offline
                  Jorgen Sigvardsson
                  wrote on last edited by
                  #8

                  Aha! I didn't know about the count() method for ordinary maps. I thought it was a pure multimap method. > It just always seems sort of wrong that there's no "test for the presence of a value in a map Indeed! Where's the ismember() method? --

                  S 1 Reply Last reply
                  0
                  • J Jorgen Sigvardsson

                    Aha! I didn't know about the count() method for ordinary maps. I thought it was a pure multimap method. > It just always seems sort of wrong that there's no "test for the presence of a value in a map Indeed! Where's the ismember() method? --

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    Especially when you look atstd::binary_search, expecting to find something that returns an iterator and find it returns a bool instead... I use the STL a lot, and do think it's pretty damn good....but some bits of it just seem a bit bizarro... Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

                    1 Reply Last reply
                    0
                    • S Stuart Dootson

                      I meant "use the count method" - assume we've got a map call a_map and that we're trying to put something keyed by the number 16 into it:

                      if (!a_map.count(16)) a_map.insert(std::make_pair(16, something_else));
                      

                      rather than

                      if (a_map.end() == a_map.find(16))
                         a_map.insert((std::make_pair(16, something_else)));
                      

                      It just always seems sort of wrong that there's no "test for the presence of a value in a map Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

                      T Offline
                      T Offline
                      Tim Smith
                      wrote on last edited by
                      #10

                      Except that using count does much more than you need. Much like using strlen/size to test to see if a string is empty. Thus it is a performance problem. Even with a map, count with get the lower bound and then the upper bound for that key. Then it computes the delta of the two iterators. (V7 implementation) If you want to create some form of a test, create a template function to do the find and then test for end. Then again, you can just use the insert and avoid the needless double search. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups