|
One solution is a multimap since it allows multiple same elements by design, thus I doubt there is a lookup.
Kuphryn
|
|
|
|
|
The "look if the item is there" is just a byproduct of finding the correct insertion point, so can't really be removed...personally, I wouldn't bother verifying that the item's not in the map, I do something like this:
std::pair<iterator, bool> insertionResult = map.insert(std::make_pair(key, value));
if (!insertionResult.second) insertionResult.first->second = value;
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Stuart Dootson wrote:
is just a byproduct of finding the correct insertion point
good point, it's probaly a silly question.
I have to check if the item is in the map before, it's an "overwrite or add" operation, which needs additional housekeeping if it's an overwrite.
If I could find a souvenir / just to prove the world was here [sighist]
|
|
|
|
|
maps overwrite by default. IIRC, the return value is a map<T>::value_type which means you can examine what was overwritten.
--
Only in a world this sh*tty could you even try to say these were innocent people and keep a straight face.
|
|
|
|
|
There's a version of std::map::insert that accepts an iterator as a hint to the appropriate insertion place. You can take advantage of this if you previously save the position of the immediately preceding element in the map, as the following example shows:
#pragma warning(disable:4786)
#include <map>
using namespace std;
template <class map_type>
map_type::iterator insert_if_not_present(map_type& m,const map_type::value_type& v)
{
map_type::iterator it=m.lower_bound(v.first);
if(it!=m.end()){
if(!m.key_comp()(v.first,it->first)){
return it;
}
}
if(it!=m.begin())--it;
return m.insert(it,v);
}
int main(void)
{
map<int,int> m;
insert_if_not_present(m,make_pair(1,1));
insert_if_not_present(m,make_pair(2,2));
insert_if_not_present(m,make_pair(3,3));
insert_if_not_present(m,make_pair(2,2));
return 0;
}
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
hi,
I'm developing ATL ActiveX EXE server that runs as a Window services using ATL wizard.
This server contains one ATL object (CTest).
How I can set the server so that when it started (via Window Services Applet), the server will :
1) initiate/create CTest.
2) Call method/function in this ATL object (CallMe).
3) destroy CTest when server stopped.
Thanx in advance.
Regards.
|
|
|
|
|
Hi.
How do you definte a comparison function for an container of std::pair other than map? I need to search a container of std::pair. For example:
typedef std::deque<std::pair<std::string, int=""> > deqStringInt;
deqStringInt example;
example.push_back(std::pair<"December", 21>);
// How do you search an element?
std::find_if(example.begin(), example.end(), std::bind2nd(???
// Functor
class Search : std::binary_function<std::pair<std::string, int="">, std::string, bool>
{
public:
bool operator()(const std::pair<std::string, int=""> &lp, const std::string &rp)
{
return lp->first == rp;
}
};
Thanks,
Kuphryn
|
|
|
|
|
#pragma warning(disable:4786)
#include <queue>
#include <string>
#include <algorithm>
typedef std::deque<std::pair<std::string,int> > deqStringInt;
struct my_search
{
my_search(const std::string s):s(s){}
bool operator()(const std::pair<std::string,int>& c) const
{
return c.first==s;
}
private:
std::string s;
};
int main(void)
{
deqStringInt example;
example.push_back(std::make_pair(std::string("December"), 21));
std::find_if(example.begin(),example.end(),my_search("December"));
return 0;
}
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Nice! Thanks.
Here is another solution and the one I implemented.
class Searc : std::binary_function<pairstringint, std::string,="" bool="">
{
public:
bool operator()(const pairStringInt &lp, const std::string &rp) const
{
return (lp.first == rp);
}
};
std::find_if(data.begin(), data.end(), std::bind2nd(Search(), theString))
Kuphryn
|
|
|
|
|
Hi.
Is it possible to have an unsorted map and/or multimap STL container? The map is an extremely useful STL container because of its key and value feature. I need to set a map container to not sort its keys, i.e. insert them if they do not exist, but in a queue fashion as in FIFO.
Thanks,
Kuphryn
|
|
|
|
|
Just combine the key and value into another structure/class or use std::pair and then use std::deque as your container. It works well as a FIFO where as by definition, a map is sorted and would perform poorly as a FIFO.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Nice! Thanks
Can you give a quick example of std::pair in other containers beside the map? I did not know it was possible to use std::pair as a datatype. How about searches?
Kuphryn
|
|
|
|
|
No. You want vector<< pair <type1, type2 > >
A map cannot deliver it's performance guarentees if it does not sort the values.
A hash map might, I'm not sure.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not
as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
Nice! Does search works for std::pair as map's std::pair? In other words, do you have to write a special search algorithm to find an element in std::vector<std::pair<t, u=""> >?
Kuphryn
|
|
|
|
|
You can't use [] on a vector in that way ( you can by providing a numeric index ), but you can write a functor to find based on the first element, yes.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not
as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
|
hi all,
i am stuck. I want to make a setup for my VC application as we have in VC. But can't find anything helpful.
if anyone can suggest me anything would be a great help.
thanks in advance
Himanshu
|
|
|
|
|
There is a large variety of setup toolkits to fit any budget. We use Wise InstallBuilder but there are a number of lower-cost options as well. For example take a look at InnoSetup. For a list of others, visit any popular download site such as download.com or tucows.com.
-Anatoly
Anatoly Ivasyuk is co-founder of DTLink Software, a company specializing in Internet software and technologies. He is the author of DTLink's Windows products: AnswerTool, FAQTool, AppUpdate, and Personal Stock Monitor
|
|
|
|
|
hi~
I apologize being not good at English.
I wanna serialize my own data for network/file/etc...
I thought that stream of c++ standard library is suitable.
but, there's no facilities exactly for my use.
1. I wanna store stream data into memory.(not file)
2. insertion and extraction must operate in binary mode.
// example of usage
// proc A
omystream ms; // mystream is that I wanted..
int n = 1;
short s = 2;
ms << n << s;
// or ms << int_b(n) << short_b(s);
// int_b(), short_b() is custom manipulators
::send(ms.buf(),...); // send to network
// proc B
BYTE buf[4096];
::revc(buf,...); // receive from network
imystream ms(buf, dwSizeReceived); // dwSizeReceived = 6
int n;
short s;
ms >> n >> s; // n = 1, s = 2
// or ms >> int_b(n) >> short_b(s);
// result
data occupy 6 bytes memory. (4 for n(int), 2 for s(short))
send and receive 6 byte of memory.
memory layout:
00 00 00 01 00 02 // 6 byte
not
"000000010002" // 12 byte
this mean not string but binary.
how can I achieve it?
derive strstream or stream?
derive stream_buf?
I need your advice.
thanks in advance.
|
|
|
|
|
What you are looking for is ::CreateStreamOnHGlobal.
You might consider SAFEARRAY(BYTE) as well (instead of).
|
|
|
|
|
The simplest way would be to deliminate all the data. In the case of streams, it would be easiest to use spaces, as the stream's default behaviour is to skip them.
ostringstream out;
int n = 1;
short s = 2;
out << n << ' ' << s;
istringstream in ( out.str () );
n = s = 0;
in >> n >> s;
cout << "n = " << n << ", s = " << s << endl;
cheers,
-B
|
|
|
|
|
hi,
I'm developing ATL ActiveX DLL that used by ASP/VBScript client.
This server uses VARIANT as a parameter input :
HRESULT UseThis([in] VARIANT StringParam, [out, retval] long *ret);<br />
<br />
STDMETHODIMP CObj::UseThis(VARIANT StringParam, long *ret)<br />
{<br />
*ret = 1;<br />
<br />
if((StringParam.vt & VT_ARRAY) && (StringParam.vt & VT_BSTR))<br />
{ <br />
SAFEARRAY *pString = StringParam.parray;<br />
BSTR *bstrArray;<br />
CComBSTR bstrRet;<br />
SafeArrayAccessData(pString, (void**)&bstrArray);<br />
<br />
for(ULONG i = 0; i < pString->rgsabound->cElements; i++)<br />
{<br />
bstrRet = bstrArray[i]; <br />
}<br />
SafeArrayUnaccessData(pString);<br />
SafeArrayDestroy(pString);<br />
<br />
*ret = 0;<br />
}<br />
<br />
return S_OK;<br />
}
When I try to use the function from VB client it OK, but when used from ASP/VBScript client it just FAILED !
VBscript client running on IIS:
Dim obj<br />
Dim ret<br />
Dim strParam(2)<br />
<br />
Set obj = Server.CreateObject("MyATL.Obj") <br />
<br />
strParam(0) = "We"<br />
strParam(1) = "love"<br />
strParam(2) = "COM" <br />
<br />
ret = obj.UseThis(strParam)<br />
<br />
Response.Write "Result is : " & ret<br />
<br />
Set obj = Nothing
Thank u very very much in advance . Any advice, help and suggestions strongly aprreciated.
Regards.
|
|
|
|
|
Din Krop wrote:
SAFEARRAY *pString = StringParam.parray;<br />
BSTR *bstrArray;<br />
CComBSTR bstrRet;<br />
SafeArrayAccessData(pString, (void**)&bstrArray);
I did the following when using variants + safearrays:
long lowerBounds, upperBounds;<br />
SafeArrayGetLBound( StringParam.parray, 1, &lowerBounds);<br />
SafeArrayGetUBound( StringParam.parray, 1, &upperBounds);<br />
long nArgs = (upperBounds - lowerBounds) + 1;<br />
CComVariant* pvVariant;<br />
HRESULT hr = SafeArrayAccessData(StringParam.parray, (void**) &pvVariant);<br />
<br />
USES_CONVERSION;<br />
for(int i = 0; i < nArgs; i++)<br />
{<br />
char szTemp[255];<br />
if (pvVariant[i].vt == VT_BSTR)<br />
strcpy(szTemp, OLE2T(pvVariant[i].bstrVal));<br />
}<br />
hr = SafeArrayUnaccessData(StringParam.parray);<br />
if (pvVariant)<br />
delete pvVariant;<br />
This may help.
Regards
Barry
|
|
|
|
|
1. The following is wrong on several levels, it will create huge runtime problems. Please do not follow that example.
if (pvVariant)
delete pvVariant;
2. I am not familiar with CComVariant, but the next 2 lines look dangerous as well
CComVariant* pvVariant;
HRESULT hr = SafeArrayAccessData(StringParam.parray, (void**) &pvVariant);
safer approach fould be
VARIANT* pvVariant;
|
|
|
|
|
AleXO wrote:
it will create huge runtime problems.
Funny, never had any, boundschecker, + lint never complained either. Why would it cause problems then? Or should you really leave a dangling pointer?
Regards
Barry
|
|
|
|
|