How do you know the duplicate key insertion in STL map
-
Is there any way to indicate the duplicate key insertion in STL map. Here the key should be a class object.
-
Is there any way to indicate the duplicate key insertion in STL map. Here the key should be a class object.
#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 -
#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 Diderothi , 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
-
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
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;
-
#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 DiderotOr alternatively, use count, as that returns an integer that can be tested in a boolean fashion (makes for more concise code)
-
Or alternatively, use count, as that returns an integer that can be tested in a boolean fashion (makes for more concise code)
-
I meant "use the
count
method" - assume we've got a map calla_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'
-
I meant "use the
count
method" - assume we've got a map calla_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'
Aha! I didn't know about the
count()
method for ordinary maps. I thought it was a puremultimap
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 theismember()
method? -- -
Aha! I didn't know about the
count()
method for ordinary maps. I thought it was a puremultimap
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 theismember()
method? --Especially when you look at
std::binary_search
, expecting to find something that returns aniterator
and find it returns abool
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' -
I meant "use the
count
method" - assume we've got a map calla_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'
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.