|
There is this huge STL map created inside a class object that is unique (meaning, there is only ONE such class object).
It reaches a point where I DON'T want to add anything more to the map, but would like to keep reusing it (meaning, empty out some of its content and keep the map for reuse). Because of its size, it seems almost impractical to do a "find" on the thousands of keys I would like to eliminate, thereby making room for the new entries I would like to insert.
I am looking for a way to accomplish what I'm trying to do, without having to make C++ do it (meaning, I would like to see if there is a way to have STL take care of the situation itself).
Thanks for any insight.
William
Fortes in fide et opere!
|
|
|
|
|
check out STL algorithms. (use remove_if())
sample codes below.
using namespace std;
int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,2,6);
INSERT_ELEMENTS(coll,4,9);
INSERT_ELEMENTS(coll,1,7);
PRINT_ELEMENTS(coll,"coll: ");
//remove all elements with value 5
vector<int>::iterator pos;
pos = remove (coll. begin(), coll.end(), //range
5); //value to remove
PRINT_ELEMENTS(coll,"size not changed: ");
//erase the "removed" elements in the container
coll. erase (pos, coll.end());
PRINT_ELEMENTS(coll,"size changed: ");
//remove all elements less than 4
coll.erase(remove_if (coll.begin(), coll.end(), //range
bind2nd(less<int>(),4)), //remove criterion
coll.end());
PRINT_ELEMENTS(coll,"<4 removed: : ");
}
for replacing element in container. Use replace_copy_if(...)
BTW, my sample code is not using std::map but more or less the same I suppose.
Hope this helps.
Sonork 100.41263:Anthony_Yio
|
|
|
|
|
Thanks for replying.
Yes, I recognized your sample as the one on page 379 of Josuttis' book, "The C++ Standard Library." However, if you were to look on the page before that, page 378 (the third bullet from the bottom), it reads, "Due to modifications, you cannot use these algorithms for an associative container." One of the algorithms it is referring to, is "remove_if()".
Map is an associative container, and therefore you cannot use "remove_if()" on it!!
William
Fortes in fide et opere!
|
|
|
|
|
WREY wrote:
Map is an associative container, and therefore you cannot use "remove_if()" on it!!
Well, that's a finer version of the truth. The truth is that associative containers are designed such that client code cannot reorder the elements. The sole reason for that design reason was to allow implementations to use binary search trees and hash maps - data structures which does not allow arbitrary order of elements.
--
Watcha' gonna do, when Hulkamania runs wild on you!?
|
|
|
|
|
Write your own remove/filter function?
template <typename Coll, typename Predicate>
void filter(Coll& coll, const Predicate& p) {
typename Coll::iterator i = coll.begin(), end = coll.end();
while(i != end) {
if(p(*i))
i = coll.erase(i);
else
++i;
}
}
struct MyFilter {
bool operator()(const std::pair<Key, Value>& v) const {
}
};
filter(myColl, MyFilter());
That ought to work?
--
Watcha' gonna do, when Hulkamania runs wild on you!?
|
|
|
|
|
Thanks for replying.
What you have described above, looks very similar to the "for_each" algorithm, which is what I'll probably end up using in combination with "stable_partition".
William
Fortes in fide et opere!
|
|
|
|
|
I'm sorry, but stable_partition can't be used with associative types, as you cannot control the order of the items (which is what stable_partition does).
Also, be aware that for_each cannot be used to manipulate the collection which you iterating. You may manipulate the individual items, but not the overall collection, because you could end up screwing up iterators for for_each.
--
They say the most horrible things, but I hear violins.
When I close my eyes, I'm at the center of the sun.
|
|
|
|
|
Thanks again for replying.
Having reread my earlier reply, I can see where I misrepresented my intent in the ways I proposed using "stable_partition" along with "for_each" (and I'm glad you caught me on that point).
My thoughts are (and it starts off this way), "OK. I am thinking about using a list (or a vector) container to arrive at a workable solution for what I'm doing. The map container currently being used, is so huge, I definitely have to find another way to ease its load (meaning, not adding anything more to it) while NOT creating another 'map' as part of the solution. A better solution (it seems) must come with the ability for that next container to be able to expand and contract (considering maps do not lend themselves handily for doing that, hence the 'vector', or the 'list' is what comes to mind). However, I'd like to obtain the ability to do bsearch on that new container, hence the thought about 'stable_partition'." (End of Part 1.)
"Part 2" deals with the use of the "for_each" algorithm with regards to the continuing support of the map, currently being used. Through the use of "for_each" with its function object, I am thinking about doing "in-place" updates, thereby freezing the size of the map (so to speak), making no more addition to it.
Therefore, my proposed solution to the current map situation lies in using both "for_each" and "stable_partition" combined, but in different routines.
I'm glad you caught me on that point because I can see where some explaining was in order.
William
Fortes in fide et opere!
|
|
|
|
|
Hi out there!
What happens, if I initialize a std::istrstream with a NULL - pointer?? I guess, it could happen something like
1. will throw immediately.
2. will throw, when I try to >> somevalue
3. will show undefined behaviour (because it tries to guess the size of the string (char*)NULL points to ... and this is just undefined)
4. will work without problem and show standard behaviour (read: same behaviour as if I'd pass a valid char* and had read past the end of the stream)
Hope someone can clear this topic for me.
Best regards, Ralph
P.S: Sorry for posting this in THE LOUNGE first ... I'm not that much used to this !
|
|
|
|
|
Hi,
I have created a com server using ATL as Windos NT service.
I also added an interface to the server i.e STA interface.Every thing works fine... The com server is maintaining a socket connection in a worker thread.
My problem starts like this... I have written a client application that uses the interface... When two clients are open no problem, if three clients are open no problem, then if i start closing each client one by one (in any order) then no problem. When i close the last client application along with the client ther service also terminates (it gets out of the message loop) and hence the socket connection. I want to maintain the socket connection even if no client is using the interface....
I am desperate for an answer as i could not find any solution.
Setup
1.visual C++, version 6.0
2.Project type ATL COM Service.
3.How it was executed the service? By clicking run button on the toolbar.
one more thing,
How do i make the service startup when windows boots?
My God is more powerfull Than Your God. (the line that divides the world)
|
|
|
|
|
Hi all
I wrote a pretty big MFC application. I have many frames and UI controls, I've changed the default message routing a bit. It works fine except one little problem: when i moving the mouse over its windows - the CPU jumps upto 100%. It happens as result of UPDATE_COMMAND_UI notifications.
Does anybody met such problem? know any utilities that can help to monitor the app and see what catches the CPU?
thanks.
|
|
|
|
|
Hi all. As I'm haveing problems with OLE DB database access, and I've tried everything that came to my mind to solve the problems, I would appreciate if someone gives me a sample of a working application (or a link where I can find it) so I can see what's I'm doing wrong. I have a .mdb that I really want to update, but I can't!! I can read it, i can insert a new record, but i can't update the record. I use ATL 7.1 without the attributed code, just plain wizard generated code plus additions.
Thanks in advance
PS: I forgot to say that articles submited to CP didn't help much in discovering what's wrong, neither MSDN helped me.
"semper aliquid haeret", Bacon.
-- Sebastián.
|
|
|
|
|
This book could be of some help:
"Database programming with Visual C++ 6" by Lyn Robison, published by Sams.
|
|
|
|
|
Thanks, but I don't want a book, and seems a bit outdated. I just need some working code to see how it works.
"semper aliquid haeret", Bacon.
-- Sebastián.
|
|
|
|
|
I have had success with the Microsoft example OnlineAddressBook from the MSDN library (comes with Visual Studio .NET Enterprise). I believe you can download it off the msdn site as well. It shows record retrieval, update, delete etc...
|
|
|
|
|
I'm delving into WTL from MFC... it sounds great, and since MS has released 7.1 it could only get better, I hope.
However, I understand that it doesn't work as well with the IDE, and have seen similar problems myself. Perhaps you can clarify these for me?
- I'm using VC++ 2002. Does anyone see signifigant difference between VC2002/WTL7 and between VC2003/WTL7.1 in the area of working with the IDE?
- I've included the WTL\include directory as required, and all seems well. However, with some WTL classes, pressing F12 takes me to an afx___.h header versus a WTL header... among other name "conflicts" with MFC, or so it appears via the intellisense- compiles and runs OK though. The intellisense doesn't work quite right... it doesn't recognize WTL classes all of the time.
Any suggestions? From what I've seen, perhaps this is normal?
Thank you. (And thanks Microsoft!)
|
|
|
|
|
Hi, I'm using WTL with VS 2003 and I don't have any problem. Be sure your stdafx.h includes only WTL/ATL stuff. Strip all afx***.h from it.
-- Sebastián.
|
|
|
|
|
Thanks, maybe the upgrade is worth a look.
|
|
|
|
|
Do not count too much on IDE, except creating a new project using a WTL wizard. Mostly you'd do everything by hand which is not that bad.
If you really get interested in WTL there is a wtl group hosted by Yahoo: http://groups.yahoo.com/group/wtl/[^].
Good luck,
Igor.
|
|
|
|
|
If the IDE detects BEGIN_MSG_MAP(class) in your class, you can automatically add window message handlers. It also detects if your class is dependent on a dialog resource (you can add event handlers from the dialog editor). And that's about it I guess. The intellisense support for WTL is very limited. Since many names in WTL is also present in MFC, the IDE will be fooled by this and show you MFC-versions of the intellisense data. But don't worry though, most of the time WTL is a reflection of MFC interface wise.
If intellisense is lying, there's always the header files which I find easily navigable.
I've never tried using WTL with the wtl:: namespace though. That might correct the intellisense problems.
--
Watcha' gonna do, when Hulkamania runs wild on you!?
|
|
|
|
|
Hi Geeks,
I have created a new COM server as a NT-service using ATL. I have added a new coclass, interface and method. It builds, and the service runs ok. When I instantiate the server from a client, it works. However, when I call QueryInterface on this object for my new interface, it returns E_NOINTERFACE. I have checked the ATL generated BEGIN_COM_MAP AND END_COM_MAP block, which does have my new interface in there.
Any suggestions will be appreciated.
Thanks.
|
|
|
|
|
Check the registry, see if your interface has been properly registered. Also look at the OLE/COM object viewer, it lists all interfaces.
Igor.
|
|
|
|
|
Ok now i have a wstring and other string, how do i convert between them...
any support for BSTR will also help me.
Is there veeeeeeeery good documentation on wstring and string...
MSDN is of little help.
thank you.
My God is more powerfull Than Your God. (the line that divides the world)
|
|
|
|
|
If you're using ATL (or even if you're not), use the ATL string conversion macros:
#include <atlbase.h>
#include <atlconv.h>
#include <string>
void this_is_your_function()
{
USES_CONVERSION;
std::string s = "whatever";
std::wstring ws = A2CW(s.c_str());
}
Alternatively (without using ATL), you can use the Win32 functions MultiByteToWideChar and WideCharToMultiByte to convert between ASCII and Unicode.
HTH
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Sometimes this is enough:
<br />
wstring w = L"This is a string";<br />
string s;<br />
copy (w.begin(), w.end(), back_inserter(s));<br />
<br />
string s1 = "This is another string";<br />
wstring w1;<br />
copy (s1.begin(), s1.end(), back_inserter(w1));<br />
<br />
|
|
|
|
|