|
Thanks for your quick and comprehensive answer.
Are you aware of any such proxy-objects? Perhaps auto_ptr would do the trick?
Copying could be quite expensive since much more data could exist in the CBook class.
Jeremy Pullicino
C++ Developer
Homepage
|
|
|
|
|
Never ever put an auto_ptr in a standard container.
auto_ptr has a funny copy semantic, and you end up with your objects getting deleted while being copied.
You could use boosts[^]shared_ptr, which does reference counting and has a more intuitive copy semantic.
Or you can simply make your own class: Constructor that takes an Book-object, a few Get/Set functions for the underlying object, assignment-op and copy constructor that deletes its contained object and takes ownership of the new one and a set of operators that access the contained Book-object to determine the sorting sequence.
Who is 'General Failure'? And why is he reading my harddisk?!?
|
|
|
|
|
All of the sort algorithm's in STL have approximately O(N log N) complexity.
Maps on the other hand have an insertion complexity of O(N * log(size() + N)), so it may be quicker to add all of the books to a map, which is always sorted rather than having to do it explicitly. It may depend on the application.
SGI STL Complexity[^]
jhwurmbach correctly pointed out that using pointers will be cheaper, but missed that the sort algorithms can optionally take a predicate. In cases like this, you are almost always going to use a functor to sort your data based on your criteria.
jhwurmbach wrote
Maybe you can write some sort of proxy-class. ... Additionally you would probably need ...
This isn't really true, raw pointers are fine, you don't need anything extra in the container, the extra stuff will go into the sort criteria function object.
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 you aim;
Yours is the Earth and everything that's in it.
Rudyard Kipling
|
|
|
|
|
Hello,
I would like to insert an element into a list container during iteration.
How would I do that? Please refer my comment in the code.
<br />
list<long> myList;<br />
myList.push_back(2);<br />
myList.push_back(4);<br />
myList.push_back(6);<br />
myList.push_back(8);<br />
myList.push_back(10);<br />
myList.push_back(12);<br />
myList.push_back(14);<br />
<br />
list<long>::iterator lItemItr;<br />
for(lItemItr= myList.begin(); lItemItr!= myList.end(); lItemItr++)<br />
{<br />
if(*lItemItr == 8)<br />
myList.insert( lItemItr + 1, *lItemItr);
}
How would I do that?
thank you
Sonork 100.41263:Anthony_Yio
|
|
|
|
|
If you want to insert after the 10, you need to compare with 10, not withsome other number!?!
Also, MSDN states about std::list::insert: "Each of the member functions inserts, before the element pointed to by it in the controlled sequence, a sequence specified by the remaining operands. The first member function inserts a single element with value x and returns an iterator that points to the newly inserted element."
SO, this lines should work:
if (*lItemIter == 10)
{
lItemIter++;
list<long>::iterator newElem = myList.insert (lItemIter, 8);
}
Who is 'General Failure'? And why is he reading my harddisk?!?
|
|
|
|
|
Probably I would play around with the return "newElem" to do some trick here.
thanks
Sonork 100.41263:Anthony_Yio
|
|
|
|
|
<br />
list<<long>>::iterator lItemItr;<br />
for(lItemItr= myList.begin(); lItemItr!= myList.end(); lItemItr++)<br />
{<br />
if(*lItemItr == 10) myList.insert( lItemItr , 8);<br />
}
Jeremy Pullicino
C++ Developer
Homepage
|
|
|
|
|
Of course, I could just do it this way but this codes is just to demonstrate my another problem. It is not my actual problem. My actual problem is way too long to be explained here.
In short. I do not want to insert in this direct way.
if(*lItemItr == 10) myList.insert( lItemItr , 8);
I want to insert during if(*lItemItr == 8)
Any idea?
thank you
Sonork 100.41263:Anthony_Yio
|
|
|
|
|
Anthony_Yio wrote:
I want to insert during if(*lItemItr == 8)
Any idea?
No.
Insert during a comparsion? How could that work?
Do you mean you want to assign to some iterator?
I am sorry, but as long as your actual problem is way too long to be explained, you will get only solutions that are way too short to be helpful.
Who is 'General Failure'? And why is he reading my harddisk?!?
|
|
|
|
|
Not during comparison.
Anyway,
it is okay. I will work it out.
Thanks.
Sonork 100.41263:Anthony_Yio
|
|
|
|
|
I think I should stick to vector.
vector allows iterator + 1;
Sonork 100.41263:Anthony_Yio
|
|
|
|
|
Anthony_Yio wrote:
I think I should stick to vector.
Depends on what you want:
Insertion into the middle of a vector is extremely expensive, as the vector is guarateed to occupy continous memory, and needs to copy anything after the insertion point.
Good is that you can do iterator arithmetics like it+6
List handles insertion in the middle better, but you cant do iterator arithmetics.
You can only increment iterators, i.e. move to the next element.
Who is 'General Failure'? And why is he reading my harddisk?!?
|
|
|
|
|
thank you for the information.
Regards,
Anthony Yio
Sonork 100.41263:Anthony_Yio
|
|
|
|
|
Hi,
I have a VC++(console)/ATL Script host app in which i create COM objects(say for Buttons) dynamically .I like to handle events for these Objects.Will it be possible for to handle events using IConnectionPoint/Sinks ?. Or this is possible only for in case of MFC app.Pls help me.Should i need to migrate from Console to MFC?
Thnks
Ram
|
|
|
|
|
Yes, it is possible to handle COM events in win32 console applications using ATL or raw C++.
Kuphryn
|
|
|
|
|
Can you pls brief me on this.And give me any samples or links related to this if possible.I am strugling with this for a while.When there are more than one button in aconsole how to know which button was clicked.Pls help.I will be really grateful.
Thnks in advance
ram
|
|
|
|
|
Hello Sir:
I know that these are two excellent ATL books.
1. Developer's Workshop to COM and ATL 3.0 by Andrew Troelsen
2. ATL Internals by Brent Rector, Chris Sells, and Jim Springfield
I want to know who did konw where i can get them? Thank in advance.
|
|
|
|
|
Here and here
--
You know me. I sure know you.. Everyone of you!
|
|
|
|
|
Hello, sir:
I have a question about ATL and WTL. That is what is different from ATL and WTL. And how can i get WTL? Can you tell me some about WTL? Thanks a lot.
|
|
|
|
|
|
|
Hi Dears
I want to develop a MS Word Addin. The main functionality of this addin is:
--> I have to convert MS Word file (.doc) into (.pdf) file format.
What should I take as first step to implement this functionality.
Kindly guide/help me in the form of some code/link.
I'll be grateful to you.
Kind Regards
Atif
Watch Your Thoughts for they will become your actions.
Watch Your Actions for they will become your habits.
Watch Your Habits for they will become your beliefs.
Watch Your Beliefs for they will determine your destiny.
|
|
|
|
|
Hello!
What are the required steps in order to send a reference of an object through a event?
Thanks a lot in advance.
|
|
|
|
|
What kind of object? If the object is supported by COM universal marshal, then you can pass it using VARIANT. Otherwise, one solution is custom marshaling.
Kuphryn
|
|
|
|
|
Thanks a lot for your reply.
Here is what I intend to do: I have a Component with 2 Classes: ObjectSender and ObjectToBeSent. ObjectSender has one event: SendObject(IObjectToBeSent **pObject). My idea is to pass a reference of ObjectToBeSent to a Visual Basic client. However, when calling the Invoke method of the client, pVarResult returns an error; so the VB client does not execute its event handler.
In other words, I do not know how to send a reference of a COM object by using IDispatch::Invoke(). Can you please help me?
HRESULT Fire_SendObject()
{
...
CComVariant* pvars = new CComVariant[1];
CComObject<cobjecttobesent> *pObjectToBeSent;
IObjectToBeSent *pIObjectToBeSent;
CComObject<cobjecttobesent>::CreateInstance(&pObjectToBeSent);
pObjectToBeSent->QueryInterface(
IID_IObjectToBeSent, (void **)&pIObjectToBeSent);
....
for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++)
{
....
pvars[0] = pIObjectToBeSent;
pvars[0].vt = VT_UNKNOWN | VT_BYREF;
pvars[0].ppunkVal = (IUnknown **)&pIObjectToBeSent;
...
hr=pDispatch->Invoke(...) // This does not work
...
}
}
Thanks again for your kind attention.
|
|
|
|