|
Hey guys,
Alright I've got a simple double for-loop here using the stl's vector.
// go thru the vector of Critical Packet lists
for(int i=0; i<_PacketLists.size(); i++)
{
// go thru the specified Critical Packet list
for(int j=0; j<_PacketLists[i].size();j++)
{
if(_PacketLists[i][j].seqNumber == seqNum)
{
Erase Vector here ......
}
}
}
The vector class has an erase function, but it only takes iterators. Is there any way to get around using iterators?I've tried using them with a vector inside a vector and has proven troublesome to me in the past.
Any help would be much appreciated!!!
Thanks in advance,
Jay
www.jdaigner.com
jay@jdaigner.com
|
|
|
|
|
There is a clear() method that will erase all vector contents.
|
|
|
|
|
I took the liberty of rewriting your vague snippet:
for(int i = 0; i < _PacketLists.size(); ++i)
{
PacketList& packetList = _PacketLists[i];
for(int j = 0; j < packetList.size(); ++j)
{
Packet& packet = packetList[j];
if(packet.seqNumber == seqNum)
{
}
}
} If that's what you meant, then this is how I would have done it from the beginning:
for(std::vector<PacketList>::iterator listIt = _PacketLists.begin(); listIt != _PacketLists.end(); ++listIt)
{
PacketList& packetList = *listIt;
for(PacketList::iterator packetIt = packetList.begin(); packetIt != packetList.end(); ++packetIt)
{
Packet& packet = *packetIt;
if(packet.seqNumber == seqNum)
{
packetList.erase(packetIt);
return;
}
}
}
jc0dex wrote: Is there any way to get around using iterators
Why should you?
--
The Blog: Bits and Pieces
|
|
|
|
|
Yea my snippet was very vague I apologize.
I have a vector<vector<_packet> _PacketList ... Im not sure how to loop thru the outside _Packetlist, and for each outside _Packetlist loop through all of it's packets. Inside that loop is where I need to check each individual packet's sequence number against the one passed in.
I hope this clarifies things because the sample code given doesn't really match what I'm trying to do.
And why the attitude :-P
|
|
|
|
|
jc0dex wrote: Im not sure how to loop thru the outside _Packetlist, and for each outside _Packetlist loop through all of it's packets
I showed you how to do it in my previous post.
jc0dex wrote: And why the attitude :-P
What?
--
The Blog: Bits and Pieces
|
|
|
|
|
for(std::vector::iterator listIt = _PacketLists.begin(); listIt != _PacketLists.end(); ++listIt)
{
PacketList& packetList = *listIt;
for(PacketList::iterator packetIt = packetList.begin(); packetIt != packetList.end(); ++packetIt)
{
Packet& packet = *packetIt;
if(packet.seqNumber == seqNum)
{
packetList.erase(packetIt);
return;
}
}
}
- What exactly is PacketList representing? I have a vector<_PACKETS> inside another vector called _PacketLists. (_PACKETS is a small struct)
That's the critical piece I'm not understanding to this problem.
Would it be like vector<_PACKET>&packetlist = *listIt?
And then I could use packetlist.begin() to iterate thru the other list?
I'm a still little confused tho ..
thanks for all the help
|
|
|
|
|
Oh and also what would be the template type of the vector in the first for-loop? A vector<vector<_packet> > ?
|
|
|
|
|
Assuming this is what you have:
typedef std::vector< _PACKETS > PacketList;
std::vector< PacketList > _PacketLists; then this is probably what you want:
for(std::vector< PacketList >::iterator listIt = _PacketLists.begin(); listIt != _PacketLists.end(); ++listIt)
{
PacketList& packetList = *listIt;
for(PacketList::iterator packetIt = packetList.begin(); packetIt != packetList.end(); ++packetIt)
{
_PACKETS& packets = *packetIt;
if(packets.seqNumber == seqNum)
{
packetList.erase(packetIt);
return;
}
}
} which simply means that you iterate over all elements of the outer vector, and for each inner vector, all elements is iterated until the one matching the seqNum criteria is found. It's then erased from that inner vector and the iteration is stopped.
If you want to continue searching the remaining inner vectors (which I'd guess), then break instead of return .
--
The Blog: Bits and Pieces
|
|
|
|
|
Hi,
I have a problem about this routine:
#include < iostream >
using namespace std;
class X {
public:
X() { cout << "c1" << endl; }
X(int i):i(i) { cout << "c2" << endl; }
X(const X& x) { cout << "c3" << endl; }
int i;
};
X get(X i) {
X x(i);
return x;
}
int main() {
X& x = get(1);
x.i++;
return 0;
}
I think after I call get(1), a temporary object X should be
created, it's const so cannot be a lvalue whose value is changeable.
but the code above could be compiled without any complain. Could you
kindly tell me where am I wrong? Thank you very much!
Best regards
-- modified at 0:29 Thursday 30th March, 2006
|
|
|
|
|
Twinsen724 wrote: where am I wrong
I am not sure, but I think that the X(const X& x) constructor takes a const parameter, but does not create a const object. What is const in your code is certainly the '1' given as parameter to the get function.
This should fail:
int main() {
X y;
y.i=1;
X& x = get(y);
x.i++;
return 0;
}
~RaGE();
|
|
|
|
|
Hi Rage,
Thank you for your reply!
I compiled your code, but it works. I guess the problem is
the return value of "X get(X)". It generates const by the compiler
because it is a temporary. But I don't write it as "const X get(X)"
and I restore it as a general reference, so no error displayed while
compiling. Hope my guess is right. Thanks anyway!
Best regards
Twinsen
-- modified at 21:46 Thursday 30th March, 2006
|
|
|
|
|
Twinsen,
Hey man .. looks to me like this ..
If you call get(1) it returns to you the newly created x object, which you store as a reference (or even simpler "a copy"). When you go to increment the value for i it's completely valid because you initialized nothing to being constant and are simply changing the variable inside the class.
Maybe I'm missing something huge, but I didn't see anything besides one of the constructors (which was never called) that takes in a const object.
Hope this helps,
Jay
|
|
|
|
|
Hi Jay,
Thanks to your reply~
Your remark is reasonable. I think the key point of the problem
is that what get() returns is a temporary object which is default
const by the compiler, so I think it isn't a lvalue. Your reply makes
me trust that the compiler in fact generates a const
temporary, but because the return type is "X" other than "const X",
a implicit conversion happened so that the complier didn't complain
when compiling the file. Am I right?
Thanks a lot!
Best regards
Twinsen
-- modified at 21:36 Thursday 30th March, 2006
|
|
|
|
|
Twinsen,
I believe this is very likely the case, the implicit conversion may be going on behind the scenes. What compiler are you using?
-- Jay
|
|
|
|
|
Hi Jay,
I use cl.exe of MS VC++6.0 IDE.
Best regards
Twinsen
|
|
|
|
|
Same here.
I think we're both correct here. If anyone else reading this knows otherwise please let me know
Glad I could be a little help Twinsen!
Cheers,
-- Jay
|
|
|
|
|
Hi Jay,
Your remark is very helpful!
Best regards
Twinsen
|
|
|
|
|
Assuming you have a class hierarchy like:
CBase
CUser
CMember
CMember* pMember = new CMember();
When you check the class name of the pMember pointer using RTTI (if available) you would likely get CMember
If you checked the value of a the this pointer inside a CMember method, say:
CMember::doSomething()
{
CRuntimeClass* pRT = this->GetRuntimeClass();
CString cls_name = pRT->m_lpszClassName;
}
cls_name I would assume would be CMember
However, if you performed this same action in say:
CUser::doSomething()
{
CRuntimeClass* pRT = this->GetRuntimeClass();
CString cls_name = pRT->m_lpszClassName;
}
Would cls_name be CUser despite the instantiated object being of type CMember???
I could just test this code, but I no longer have a VC++ install on my computer and I'm in an argument with a friend :P
Thanks
It's frustrating being a genius and living the life of a moron!!!
|
|
|
|
|
I wrote a ManagedC++/.NET wrapper around Uniscribe. Which is the lowest level international text rendering (win32) API as I understand it.
With it I'm able to do custom rendering of formatted international text.
That's all good.
(For the curious who don't know it, you use various ScriptXXX to perform text analysis and then you finally render 'glyphs' (as opposed to characters) with ScriptTextOut)
Now come the problem, when I try to print.
I even manage to achieve perfect WYZIWIG behavior, that's cool and it works well on my work computer, on many customer's computer, on my home computer.
BUT, for some customer the preview is completely blank.
And for some other the printed page is completely blank as well.
What could it be?
How to fix that?
Any ideas?
Yep I have already (and unsuccessfuly) tried news://news.microsoft.com/microsoft.public.win32.programmer.international
|
|
|
|
|
Hi.
If i want to hide a program all windows, only need hide all top-level windows? How about child windows?
Thank you.
|
|
|
|
|
If a window is hidden you can't see its children.
Steve
|
|
|
|
|
Hi;
1|2|3
4|5|6
7|8|9
That is a tic tac in c++.
I want to replace 5 by X and 3 by 0, but I have problem SET().
Can I do the following way move?
set X (int);
if( board [4]== 5)
return X;
-- modified at 9:34 Thursday 30th March, 2006
|
|
|
|
|
What language are you programming in ?
~RaGE();
|
|
|
|
|
I tried to submit an article today but had a mistake:
I can not find place to upload my files, at which step? how many (zipped or image) files I should prepare?
A special image tool for Windows C++ programmers, don't miss it!
A nice hyper tool for optimizing your Microsoft html-help contents.
Includeh10
|
|
|
|
|
Go here and click the "Modify this article" link.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|