| View previous topic :: View next topic |
| Author |
Message |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Sat Aug 06, 2011 12:23 pm Post subject: c++ dynamic array and lists |
|
|
I have this list and iterator
| Code: | list<APawn*> PawnList;
list<APawn*>::iterator PawnIterator; | I'm trying to convert the list to an array like this
| Code: |
int size = (int)PawnList.size();
APawn* Pawns;
Pawns = new APawn[size];
int i = 0;
for ( PawnIterator=PawnList.begin() ; PawnIterator != PawnList.end(); PawnIterator++ ){
Pawns[i] = (APawn*)*PawnIterator;
i++;
}
| but here
Pawns[i] = (APawn*)*PawnIterator;
it says that there is no = operand
where is the problem?
it is within the APawn class or in my method?
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Aug 06, 2011 2:03 pm Post subject: |
|
|
Can I ask why you need to convert one container to another?
_________________
- Retired. |
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Sat Aug 06, 2011 3:02 pm Post subject: |
|
|
working with an array would be much easier than with the list but I can only add the objects with a list since the size of an array is constant and I need it to be variable like in the list.
--------
| Code: | int size = (int)PawnList.size();
APawn* Pawns = new APawn[size];
int i = 0;
for ( PawnIterator=PawnList.begin() ; PawnIterator != PawnList.end(); PawnIterator++ ){
Pawns[i] = *PawnIterator._Ptr->_Myval;
i++; |
this should work I think...
|
|
| Back to top |
|
 |
sponge I'm a spammer
Reputation: 1
Joined: 07 Nov 2006 Posts: 6009
|
Posted: Sat Aug 06, 2011 6:05 pm Post subject: |
|
|
So what you really want is std::vector.
_________________
|
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Sat Aug 06, 2011 8:33 pm Post subject: |
|
|
I do?
I'll look into it. thanks.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Aug 06, 2011 8:41 pm Post subject: |
|
|
What would you be doing after converting it to an array though? Whats preventing you from just using the list instead for whatever you are attempting to do?
Mainly trying to help you with the end result to even see if you need to convert from the list at all. As it seems now you are just adding overhead to your app by converting from one storage type to another for what may be a simple fix.
_________________
- Retired. |
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Sat Aug 06, 2011 10:11 pm Post subject: |
|
|
actually I think I found a way of doing what I wanted just with the list..
I though I needed to get all the elements of the list in an array to be able to compare the elements with each other but I think I can do it with the same for loop
for ( PawnIterator=PawnList.begin() ; PawnIterator != PawnList.end(); PawnIterator++ )
thanks
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Aug 07, 2011 1:08 am Post subject: |
|
|
Just a performance tip in general with iterators, you should define the start/end variables in the same iteration rather then checking against .end() each increment.
You can do that like this:
| Code: | for( std::list<APawn*>::iterator iter = PawnList.begin(),
std::list<APawn*>::iterator iterend = PawnList.end();
iter != iterend; ++iter )
{
// do stuff here..
} |
_________________
- Retired. |
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Sun Aug 07, 2011 3:14 am Post subject: |
|
|
| Code: | for( std::list<APawn*>::iterator iter = PawnList.begin(),
iterend = PawnList.end();
iter != iterend; ++iter )
{
// do stuff here..
} |
like this ?
is it better to do that way even when I'm using the same for loop in another subprogram?
thats why I made the list and iterator global
this is my program:
this are global
| Code: | list<APawn*> PawnList;
list<APawn*>::iterator PawnIterator; |
//Here I make the list
| Code: | void DrawPlayerESP( UCanvas* pCanvas)
{
if (pPC == NULL || pPC->Pawn == NULL || pPC->Pawn->WorldInfo == NULL )
return;
APawn* Target = pPC->Pawn->WorldInfo->PawnList;
while ( Target != NULL )
{
if ( Target != NULL && !Target->bDeleteMe && Target != pPC->Pawn && Target->Health >0 && !Target->IsA(AVehicle::StaticClass()))
{
if(Target->IsA(AcAPBPawn::StaticClass()))
{
APBTarget = (AcAPBPawn*)Target;
if (APBTarget->m_MissionSideInfo.m_nMissionUID !=NULL && APBPawn->m_MissionSideInfo.m_nMissionUID !=NULL && APBTarget->m_MissionSideInfo.m_nMissionUID !=-1 && APBPawn->m_MissionSideInfo.m_nMissionUID !=-1)
{
if (APBTarget->m_MissionSideInfo.m_nMissionUID == APBPawn->m_MissionSideInfo.m_nMissionUID){
PawnList.push_back(Target);
PawnList.unique();
if(IsEnemy (APBTarget)){
Target->Mesh->SetDepthPriorityGroup(2);
ESP(pCanvas, Target, Red, APBGameEngine);
}
else{
ESP(pCanvas, Target, Green, APBGameEngine);
}
}
else
{
for ( PawnIterator=PawnList.begin() ; PawnIterator != PawnList.end(); PawnIterator++ )
{
if (*PawnIterator == Target ){
PawnList.remove(Target);
}
}
}
}
}
}
Target = Target->NextPawn;
}
} |
//Here I use it
| Code: | void test(UCanvas* pCanvas){
int closest = 100000 ;
for(PawnIterator=PawnList.begin() ; PawnIterator != PawnList.end(); PawnIterator++ ){
APawn* Target = *PawnIterator;
FVector STargetPos;
FVector TargetPos = PawnPosition(Target);
STargetPos = WorldToScreen(pCanvas, TargetPos);
FVector Center;
Center.X = pCanvas->ClipX/2;
Center.Y = pCanvas->ClipY/2;
int distance = Distance2D(STargetPos,Center);
if (distance < closest){
closest = distance;
}
}
} |
|
|
| Back to top |
|
 |
|