|
Hi all,
I am new to STL and am developing an application that will take any kind of values as keys.But the key_compare function object is then making a comparison of the addresses of the keys rather than their values. Can somebody tell me how should i go about in my attempt to compare the values of the keys and not their addresses. Sample code below:
<code>#include<map>
#include<iostream>
int main()
{
using namespace std;
map< void*,void*,less<void*> > m1;
map< void*,void*,less<void*> >::key_compare kcl=m1.key_comp();
map< void*,void*>::iterator pIter;
char* do=new char[100];
strcpy(do,"this");//key1
char* dont="that";//value mapped with key1
m1.insert(map< void*,void*,less<void*> >::value_type(do,dont));
char* do1=new char[100];
strcpy(kc,"this");//key2
char* dont1="that";//value mapped with key2
m1.insert(map< void*,void*,less<void*> >::value_type(do1,dont1));
//problem is in next line-how do i compare the values in the keys..what
//happens now is a comparison of address
bool bresult=kcl(do,do1);//this should always return true as values
//are equal
if(bresult==true)
{
cout<<"Match";
for(pIter=m1.begin();pIter!=m1.end();pIter++)
{
cout << " " << (char *)(pIter -> first);
cout << "." << endl;
}
}
else
{
cout<<"Mismatch";
for(pIter=m1.begin();pIter!=m1.end();pIter++)
{
cout << " " << (char *)(pIter -> first);
cout << "." << endl;
}
for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
cout << " " << (char *)(pIter -> second);
cout << "." << endl;
}
cin.get();
}</code>
"When life offers you Lemons,Learn to make Lemonade"
|
|
|
|
|
As you point out yourself, having void * as the key type for your map defaults to comparing addresses instead of the contents. One easy solution is to use something better behaved as the key, like std::string (this, also, has the nice extra benefit that you are not risking storing pointers to deleted contents, as it can be the case with your current scheme.)
But I guess you have a valid reason for storing void pointers inetsad, since you talk about "an application that will take any kind of values as keys". If you absolutely need to store void pointers, how do you expect your app to work when the keys stored are of different types? (like for instance one is pointing to a char array and the other to a double )
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanks for the quick response Joaquin..Well actually i would be comparing like valued keys only(there will be a sort of switch case to compare keys of same type)so issue of keys being of different types does not affect me.This is precisely why am going for a void *.
As regards storing pointers to deleted contents,i shall be using smart pointers so that issue will also be taken care of.
What i am looking for is some kind of an overloaded function for less trait in the map that does the comparison here for the values and not the addresses.Is that possible?
|
|
|
|
|
Well, you can have something like this:
struct void_ptr_compare
{
bool operator()(void* x, void* y)const
{
return strcmp((const char *)(x),(const char *)(y))<0;
}
};
...
map< void*,void*,void_ptr_compare > m1;
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
that solves all my problems(for now at least). Exactly what i wanted but did not know how to go about it. Thanks Joaquin!!
|
|
|
|
|
I have created a custom listview in which different control (Edit, Combo, Button) are placed on each subitem.
Now every time Items are redrawn whole screen flicker. Reason for this that came to my mind is movement of each control that causes the flickering effect. I have already using the DeferPos function to move the controls in one go.
How can i remove the flicker problem. Secondly is this a correct approach to show control on each subitem.
Thanks
|
|
|
|
|
Hi,
I have the following struct:
<br />
typedef struct _MyStruct {<br />
int one;<br />
int two;<br />
} *pMyStruct, MyStruct;<br />
from which I constructed vector:
<br />
typedef std::vector<MyStruct> MyStructContainer;<br />
typedef std::vector<MyStruct>::iterator MyStructIter;<br />
In the one of my functions, I walk through all elements of vector:
<br />
MyStructContainer c;<br />
MyStructIter iter;<br />
pMyStruct p;<br />
<br />
for (iter = c.begin(); iter != c.end(); iter++)<br />
{<br />
<br />
};<br />
How to get a pointer to MyStruct by iterator?
Sincerely Yours,
RadioShark
|
|
|
|
|
p=&(*iter); Hope this helps.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hi
I've been browsing the job sites and a lot of places are asking for STL / ATL. I've been a 'straight' MFC developer for years. (Not in the "not gay" sense of straight, more in the Not STL / COM / ActiveX sense).
Anyway - as far as I can tell...
STL : Standard Template Library - vectors, maps, lists, deques - basically template driven containers, that have iterators you can pass to algorithms for fast, simple, flexible data handling.
ATL : Active Template Library - for developing graphic activex components?
WTL : WTF!
Am I about right / way of mark?
Cheers
Angel
===========================================================
The sooner you fall behind, the longer you have to catch up
|
|
|
|
|
STL : Standard Template Library - vectors, maps, lists, deques - basically template driven containers, that have iterators you can pass to algorithms for fast, simple, flexible data handling.
Strictly speaking, algorithms are as esential a part of STL as containers. For some reason, people tend to focus on containers alone.
ATL : Active Template Library - for developing graphic activex components?
I'd say (I'm no ATL expert) ATL is primarly used to do COM programming with a little C++ style encapsulation, rather than directly against the raw COM API. ATL can be applied to writing ActiveX controls, but also any other COM object.
WTL (Windows Template Library) is a windowing framework built on top of ATL. A replacement for MFC, though (allegedly) much more elegant. AFAIK, WTL is no longer maintained by Microsoft (it started as a private project inside the company, actually) but I think a bunch of folks in the open source realm are maintaining it now.
What puzzles me is why STL and ATL are so frequently listed together: apart from relying on tecnhiques belonging to what's called "modern C++", there's little relationship between both libraries.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Joaquín M López Muñoz wrote:
What puzzles me is why STL and ATL are so frequently listed together: apart from relying on tecnhiques belonging to what's called "modern C++", there's little relationship between both libraries.
Part of the reason might be that when doing ATL you typically don't use MFC's supporting structure of general purpose classes - collections, etc.. So you turn to STL instead. I can see how that could have been the case historically - before STL had been incorporated into C++ proper.
Today, however, STL shouldn't really be seen as some separate thing. Every C++ developer worth their salt should have some familiarity with it - whether doing MFC or otherwise.
Kevin
|
|
|
|
|
|
You're not too far off. Like you, I'm a longtime MFC developer, and until recently I'd hardly touched ATL at all...and only lightly dabbled in STL.
That's changing fast now, as I'm currently working on a couple of projects which aren't MFC based (one's a lightweight Win32 application, and the other a Visual Studio add-in).
For those, ATL, WTL and STL are essential. Unfortunately, the documentation for WTL and STL is not to be up to the standards we're used to for MFC and ATL, but given access to the source it's more an annoyance than anything.
Good luck!
Anna
Riverblade Ltd - Software Consultancy Services
Anna's Place | Tears and Laughter
"Be yourself - not what others think you should be"
- Marcia Graesch
"Anna's just a sexy-looking lesbian tart"
- A friend, trying to wind me up. It didn't work.
|
|
|
|
|
Hi,
Does anyone know what the most efficient method is for copying a large list or vector of data (about 25000 elements)?
cheers,
|
|
|
|
|
std::copy is probably your best bet. Using it is going to ensure that your compiler has the best chance of optimising (no post increments or missing consts on iterators) and is easy to read for code maintainers.
If you're worried about this operation, and it's a possible bottleneck it is worth profiling the various options. Because seemingly obvious optimisations (like calling reserve() on the vector) might not do as much to help as you think. See the article by Andrew Koenig and Barbara E. Moo "Are Vectors Really Fastest?" published in the August 2004 issue of the C/C++ Users Journal.
memcpy is only an option if your data elements are POD (Plain Old Data), but even then in some (most?) situations the code that std::copy will generate is exactly the same as the memcpy code.
Custom STL allocators might help if your objects are reasonably small (a few bytes) - although it would be rare that this would be a preformance issue.
If you can keep you head when all about you
Are losing theirs and blaming it on you;
If you can dream - and not make dreams your master;
If you can think - and not make thoughts your aim;
Yours is the Earth and everything that's in it.
Rudyard Kipling
|
|
|
|
|
|
memcpy() ?
TOXCCT >>> GEII power
|
|
|
|
|
Hi,
can I use the WTL menu (with bitmap buttons) in an MFC Application?
(I need a bitmap menu, with some additional features, that would best work in both MFC and ATL/WTL components)
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist Fold With Us! || Agile Programming | doxygen
|
|
|
|
|
Hi,
I'm developing an ATL Server Web Service and I'm having this problem: I perform a stress test (50 concurrent connections) for 15 minutes, and after the test is finished I find that the performance monitor (in the web service object) registers more connections than 50, but the worst part is that these connections don't go totally down. Almost always the fall slowly until they reach 20 connections and then they stay there (I've left the server on for one or two days, but the number of connections don't fall to zero).
These are TCP connections I suppose, and from inside my app I just have access to the http layer. I think that IIS (I'm working with IIS 5.0 and Win2k Server) manages these connections, but I don't know if that's exactly like that. I would appreciate a lot to know if anyone has had this (kind of)problem before, and in that case what was happening.
Thanks,
Federico
|
|
|
|
|
I have two COM class in ATL/DLL:
1º
class Connection
{
interface IConnection
Open(BSTR PAthName)
Close()
//Other methods beginTrans commit roollback.. properties..timeout type etc..
public:
Connection(){//initialize};
StructDB getHandleDB();//return private var m_handleDB
//+++ other methods & properties
private:
StructDB m_handleDB;
//+++ other vars...
}
2º
class Recordset
{
interface IRecordset
Open(BSTR StrSQL, IConnection *Connection)
Close()
//Other properties and methods fields value RecordsCount ..etc
public: //etc
private: //etc
}
I compile the ATL/DLL , I from VB 6 IDE , Refernce my DLL and I write this code:
'VB COde..
dim mConnectionDB as MyLib.Connection
dim rs as MyLib.Recordset
set mConnectionDB=new Connection
mConnectionDB.Open "Database.mydb"
set rs=new Recordset
rs.Open "Select * from Customers",mConnectionDB
rs.close
mConnectionDB.Close
The question is... How in the method Open the recordset class, can I access the method StructDB getHandleDB();//return private var m_handleDB ???????
I don't want put property interface return the handle... this property not necesary from VB.
If I used to CCOmObject I have got new instance the Connection and handle is NULL.
Help ME!! please
|
|
|
|
|
I find a solution!!! I have created a private interface!! Only visible for ATL/DLL.
|
|
|
|
|
Hi,
This following code in a console application:
CoInitialize(0);
HRESULT hr;
SHDocVw::IShellWindowsPtr spSHWinds;
hr = spSHWinds.CreateInstance (__uuidof(SHDocVw::ShellWindows));
====> Returns 0x0x80040154 - Class not registered.
Can someone please help me why I am getting this error?
Any help would be greatly appreciated.
SenPy
|
|
|
|
|
Try registering SHDOCVW.DLL using regsvr32 utility and then running your app.
-------------------------
Have a great day ahead!
Regards,
Sohail Kadiwala
modified 21-Apr-21 21:01pm.
|
|
|
|
|
I create ATL component with a number of methods. Some of them run perform long operations, and I need send to client application status of this. How do I have to add event to my ATL and listen it in a client application ?
|
|
|
|
|
Hello Dudic,
My advise is to create a separate thread in your COM Object to perform the long operations. As and when a change in status occurs in your COM object, you fire an event to your client app.
There are several points you need to note about achieving the above mentioned design :
1. Your thread must be a COM thread. That is, it must call CoInitialize() on start up and call CoUninitialize() just before termination.
2. The event handler interface pointer of your client (which are passed to you when your client connects with your COM Object's connection points) must be marshalled to your COM Object. They -CANNOT- be passed to your thread by simple copying.
I'm going to write an article on COM threading soon and I'll keep you posted when I do.
Best Regards,
Bio.
|
|
|
|
|