// Create a list and list iterator.
std::list<type> lst;
std::list<type>::iterator itr;
// To add to the front of a list, use the push_front method:
lst.push_front( typeVariable );
// To add to the end of a list, use the push_back method:
lst.push_back( typeVariable );
// To take from the beginning of a list, use the pop_front method:
lst.pop_front( typeVariable );
//To take from the end of a list, use the pop_back method:
lst.pop_back( typeVariable );
//To loop through a list, you must use an iterator.
//An iterator is is essentially a pointer to an object in the list.
//To loop through from the beginning of a list to end, use begin() and end():
for( itr = lst.begin(); itr != lst.end(); itr++ );
//To loop through from the end of a list to the beginning, use rbegin() and rend():
for( itr = lst.rbegin(); itr != lst.rend(); itr++ );
//You use an iterator just like a pointer.
itr->IteratorMethod();
// Assuming your iterator is of a class that has GetStats() as a method:
itr->GetStats();
std::list< Person* > personList; // define a list to hold pointers to Person objects
Person *pers = new Person;
personList.push_back( pers ); // append the pointer to the end of the list.
personList.remove( pers ); // delete from list (object not destructed, because it's only a pointer)
delete pers;
for( std::list< Person* >::iterator i = personList.begin(); // create an iterator 'i' and assign the first iterator in the list to it (begin)
i != personList.end(); // end() points beyond the last element, and not AT the last element!
++i ) // increment to the next iterator
{
Person *pers = *i; // dereference iterator 'i' which returns the pointer it encapsulates
pers->whatever();
}
#include <vector>
#include <list>
#include <string>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cctype>
// This is used with for_each below
void printChar( const char c )
{
std::cout << c;
}
// Ditto
void to_upper( char &c )
{
c = toupper( c );
}
int main()
{
// Create a string.
std::string myString( "This is my string." );
// Create a char vector, and copy the string's contents into it.
std::vector< char > myVec( myString.begin(), myString.end() );
// Print both.
std::cout << "String: " << myString << '\n'
<< "Vector: " << &myVec[0] << '\n';
reverse( myString.begin(), myString.end() );
reverse( myVec.begin(), myVec.end() );
std::cout << "String: " << myString << '\n'
<< "Vector: " << &myVec[0] << '\n';
// Use vector as replacement for char[] array with legacy C functions.
std::vector< char > charReplacement( 32 );
snprintf( &charReplacement[0], charReplacement.size(), "Fun: %d %s.",
3, "little piggies" );
std::cout << &charReplacement[0] << '\n';
// Call printChar on each element:
for_each( myString.begin(), myString.end(), printChar );
for_each( charReplacement.begin(), charReplacement.end(), printChar );
// Convert to uppercase:
for_each( charReplacement.begin(), charReplacement.end(), to_upper );
for_each( myString.begin(), myString.end(), to_upper );
std::cout << '\n' << &charReplacement[0] << '\n'
<< myString << '\n';
}
for( int n = 0; n < 5; ++n )
{
std::cout << n << '\n';
}
void arena_data::remove_combatant( CHAR_DATA * ch )
{
list < arena_combatant_data *>::iterator fighter;
if( !ch )
{
bug( "%s: NULL ch", __FUNCTION__ );
return;
}
for( fighter = arena->fighters.begin( ); fighter != arena->fighters.end( ); fighter++ )
{
arena_combatant_data *i = *fighter;
if( ch == i->combatant )
{
restore_attribs( ch );
arena->fighters.remove( i );
delete i;
i = NULL;
return;
}
}
return;
}
list < arena_combatant_data * > fighters;
void arena_data::remove_combatant( CHAR_DATA * ch )
{
list < arena_combatant_data *>::iterator fighter, fighter2;
if( !ch )
{
bug( "%s: NULL ch", __FUNCTION__ );
return;
}
for( fighter = arena->fighters.begin( ); fighter != arena->fighters.end( ); fighter = fighter2 )
{
fighter2 = fighter;
fighter2++;
arena_combatant_data *i = *fighter;
if( ch == i->combatant )
{
restore_attribs( ch );
arena->fighters.remove( i );
delete i;
i = NULL;
return;
}
}
return;
}
void arena_data::remove_combatant( CHAR_DATA * ch )
{
std::map<CHAR_DATA *, arena_combatant_data *>::iterator entry = arena->fighters.find(ch);
if(entry != arena->fighters.end()
{
restore_attribs( ch );
delete entry.second;
arena->fighters.erase(entry);
}
}
void arena_data::remove_combatant( CHAR_DATA * ch )
{
std::map<CHAR_DATA *, arena_combatant_data *>::iterator entry = arena->fighters.find(ch);
if(entry != arena->fighters.end()
{
restore_attribs( ch );
delete entry.second;
arena->fighters.erase(entry);
}
}
Thanks in advance,
KeB