|
#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
|
|
|
|
|
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<iostream.h>
#include <algorithm>
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 );
return b;
}
/*
bool operator<(const A& objref) const
{
if((((la==objref.la)&&(lb==objref.lb)&&(lc==objref.lc))?1 ))
{
cout<<"Attempting insert a duplicate value -"<<"("<<la<<","<<lb<<","<<lc<<")"<<" -="" "<<endl;
="" }
="" return="" la<objref.la=""
="" ||(!(objref.la<la)="" &&="" lb<objref.lb)
="" ||(!(objref.la<la)&&="" (!(objref.lb<lb))&&="" lc<objref.lc);
="" *="" bool="" operator="" !="(const" a&="" objref)="" const
="" {
="" if(((la="=objref.la)&&(lb==objref.lb)&&(lc==objref.lc))?1 )
" 0;
="" 1;
="" unsigned="" long="" sum();
="" void="" displayvalues()="" cout<<"("<<la<<","<<lb<<","<<lc<<")"<<"="" ";
="" friend="" class="" asortcriterion;
};
unsigned="" a::sum="" ()
{
="" (la+lb+lc);
}
="" for="" function="" predicate
*="" operator()="" returns="" whether="" a="" person="" is="" less="" than="" another="" person
*=""
class="" asortcriterion=""
{
public:
="" (const="" p1,="" const="" p2)="" if((((p1.la="=p2.la)&&(p1.lb==p2.lb)&&(p1.lc" =="p2.lc))?1 ))
" cout<<"attempting="" insert="" duplicate="" value="" -"<<"("<<p1.la<<","<<p1.lb<<","<<p1.lc<<")"<<"="" p1.la<p2.la="" ||(!(p2.la<p1.la)="" p1.lb<p2.lb)
="" ||(!(p2.la<p1.la)&&="" (!(p2.lb<p1.lb))&&="" p1.lc<p2.lc);
=""
};
="" maptest.cpp="" :="" defines="" the="" entry="" point="" console="" application.
=""
#include="" "stdafx.h"
#include="" "maptest.h"
#include<map="">
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= new float(3.30);
pfd =new float(4.40);
pfe =new float(5.50);
Aptrmap objmap;
objmap.insert(std::make_pair(objFive,pfe));
// objmap[objFive]=pfe;
objmap.insert(std::make_pair(objOne,pfa));
objmap.insert(std::make_pair(objTwo,pfb));
objmap.insert(std::make_pair(objThree,pfc));
objmap.insert(std::make_pair(objFour,pfd));
float pf=9;
objmap[objThree]=&pf;
// print all element values
Aptrmap::iterator pos;
for (pos = objmap.begin(); pos != objmap.end(); ++pos)
{
(pos->first).DisplayValues();
cout <<*( pos->second )<<endl;
}
="" cout="" <<="" endl;
=""
="" aptrmap::const_iterator="" postwo="objmap.find(objThree);
" cout<<*(postwo-="">second)<<endl;
="" aptrmap::const_iterator="" postwo1="objmap.find(objFive);
" cout<<*(postwo1-="">second)<
|
|
|
|
|
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!
#include <iostream>
#include <map>
#include <boost\shared_ptr.hpp>
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.la<<","<<o.lb<<","<<o.lc<<")";
return out;
}
namespace std { // needed for std::copy to work
ostream& operator<<(ostream& out, const pair<A, boost::shared_ptr<float> >& o)
{
out << "[" << o.first << "," << *(o.second) << "]";
return out;
}
}
typedef std::map<A, boost::shared_ptr<float> > 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<float> 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 <Aptrmap::iterator, bool> 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;
// objmap[objFive]=pfe;
it =objmap.insert(std::make_pair(objOne,pfa));
// 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;
it =objmap.insert(std::make_pair(objTwo,pfb));
// 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;
it =objmap.insert(std::make_pair(objThree,pfc));
// 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;
// check if this value is a duplicate before inserting
if (!is_duplicate(objmap, objFour))
{
it =objmap.insert(std::make_pair(objFour,pfd));
if (it.second)
cout << *(it.first) << " was inserted" << endl;
}
else
cout << objFour << " was a duplicate, will not insert it" << endl;
boost::shared_ptr<float> pf(new float(9));
objmap[objThree]=pf;
// print all element values
copy(objmap.begin(), objmap.end(), ostream_iterator<pair<A, boost::shared_ptr<float> > >(cout, "\n"));
Aptrmap::const_iterator posTwo=objmap.find(objThree);
if (posTwo != objmap.end())
cout<<*(posTwo->second)<<endl;
Aptrmap::const_iterator posTwo1=objmap.find(objFive);
if (posTwo1 != objmap.end())
cout<<*(posTwo1->second)<<endl;
return 0;
}
HTH
---
"Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot
-- modified at 8:49 Monday 14th November, 2005>>
|
|
|
|
|
Or alternatively, use count, as that returns an integer that can be tested in a boolean fashion (makes for more concise code)
|
|
|
|
|
Could you please elaborate further? I'm really not sure what you mean by "use count".
--
Pictures[^] from my Japan trip.
|
|
|
|
|
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'
|
|
|
|
|
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?
--
|
|
|
|
|
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'
|
|
|
|
|
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.
|
|
|
|
|
Is there a way to use commands like cout in a command line in a WTL program? This could be very useful for debugging to monitor how and when events are being called (for example).
|
|
|
|
|
A GUI app can call AllocConsole() to create a console window. See the MSDN topic "Reading and Writing Blocks of Characters and Attributes" for some sample code.
You can also write the debug stuff with ATLTRACE (or the API itself, OutputDebugString() ) and then run an app that shows the debug output.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | 1ClickPicGrabber | NEW~! CP SearchBar v3.0 | C++ Forum FAQ
Pinky, are you pondering what I'm pondering?
I think so Brain, but if we shaved our heads, we'd look like weasels!
|
|
|
|
|
Okay thanks! Works perfeclty
|
|
|
|
|
Hi all, I am trying to send a binary file via webservice.
The web method takes in the file as a byte[] and the stub function generated by Visual Studio takes an ATLSOAP_BLOB type as the file.
My question is: how do I construct an ATLSOAP_BLOB from a binary file? I can read in the file as byte[] or char[] but ATLSOAP_BLOB requires unsigned char* as the data.
Do I need to manually encode my char* using base64 to unsigned char*?
Any suggestion would be appreciated!
|
|
|
|
|
Hi folks,
I'm making a shell extension (the worst documented part of programming ever) and I want to handle keyboard accelerator commands for it. In a dialog based you'd add TranslateAccelerator to the message loop but in a extension I have no such loop. How do I catch such accelerator commands?
I'm making a namespace extension so I want to be able to handle them both in the treeview and the shellview.
Would really appreciate any ideas. I've searched all over internet for this but I'm pretty new to extension programming so it's hard to find things. Thanks for you help.
/Benny
|
|
|
|
|
Hi
I have taken ATL COM Appwizard to create ATL Service and selected Service as my option. My intention is that i have to create a service which starts my Server Application as a service which is written in VC++. My Server Application is Dialog based. When the Service is started how should i know that service is started.
Where i have to insert code to start my Service. please help me with code.
Thanks in Advance.
|
|
|
|
|
|
What would be the best way to implement additional features to an existing Activex developed via ATL, without breaking the current contract ?
There are a bunch of clients using the current code, and would not like to break the current functionality, in other words, I would like that new and old clients use the same Activex without breaking.
Thanks in advance
|
|
|
|
|
hello
I developed a activeX com component(.ocx) which is having only two property tabs named "set properties" and "extended". I want to add another property page tab in property sheet view. How can i add and use it.
-- modified at 1:26 Monday 7th November, 2005
|
|
|
|
|
In Dialog Resource Say Insert and select Property Page and say ok....
Now a new property page is created...create a class for it using class wizard and use it.
|
|
|
|
|
Hi everybody,
I have created a WTL MDI application. When it runs and the childframe window opens in the mainframe, I can capture the command messages generated by menu items selection in the OnForwardMsg handler of the childframe window. But I can't get the messages generated by toolbar buttons click with the same menu items identifiers.
I want to know what is wrong here. Why can't I get command messages by toolbar buttons click in the childframe window?
yours,
Roozbeh.
|
|
|
|
|
Hi!
I'm using ATL to create ADO connection connection
but linker errors occur.
unresolved external symbol _CLSID_CADORecordset
unresolved external symbol _IID_IADORecordset
unresolved external symbol _CLSID_CADOConnection
unresolved external symbol _IID_IADOConnection
my code :
# include <atlbase.h>
# include <adoid.h>
# include <adoint.h>
# include <comdef.h>
CComPtr<ADOConnection> p_Con;
CComPtr<ADORecordset> p_Rst;
CoCreateInstance (CLSID_CADOConnection, NULL,
CLSCTX_INPROC_SERVER,IID_IADOConnection,
(LPVOID *) &p_Con);
CoCreateInstance (CLSID_CADORecordset, NULL,
CLSCTX_INPROC_SERVER,
IID_IADORecordset, (LPVOID *) & p_Rst);
Pleas help
B2C
|
|
|
|
|
|
|
|
Hi Friend,
I want to create a link list using STL. I am doing the following
-----------------.h file---------------
struct CNode
{
long x;
_bstr_t b;
_bstr_t c;
};
class A
{
...
typedef std::list<cnode*> SEC_MSG_DATA;
SEC_MSG_DATA node;
...
}
--------------------------------------------------
I am trying to acces the member of the structure CNode through the object pointer of class A (pA->node.x) however i am getting the error
"is not a member of 'list<struct cnode="" *,class="" std::allocator<struct="" *=""> >'"
Can you please help me.
Thanks
|
|
|
|
|