Using Remove |
There
are two types of remove operations allowed on any object that inherits from the
class os_element_group .
Use the remove_first()
function to find the first occurrence of a key and remove it from an element
group. If no matching element is found, remove_first()
returns false .
In the following example, four os_list_item
objects are defined. The list items named item1 and item4
are given keys named my_first_key and my_last_key
respectively. A key named my_middle_key is given to
the two middle list items, item2 and item3
.
An ordered list containing all
four items is then created. When the function remove_first()
is called, it finds and removes the first occurrence of my_middle_key
.
The list is inserted in the Web page twice to show the list both before and after the remove operation. The first list contains all four list items. The second list contains only item1 , item3 , and item4 . The first occurrence of my_middle_key ( item2 ) is removed.
#include <iostream.h>
#include <ospace/web.h>
int main()
{
os_page page( "Remove1 Example" );
os_list_item item1( "item1" );
os_list_item item2( "item2" );
os_list_item item3( "item3" );
os_list_item item4( "item4" );
item1.key( "my_first_key" );
item2.key( "my_middle_key" );
item3.key( "my_middle_key" );
item4.key( "my_last_key" );
os_ordered_list ol( "Before:" ) ;
ol << item1 << item2 << item3 << item4;
page << ol;
ol.remove_first( "my_middle_key" );
ol.header( "After: item2 has been removed" );
page << ol;
cout << page;
return 0;
}

Use the function remove_all()
to find all occurrences of a key and remove them from an element group. The
function remove_all() returns the number of
elements removed. If no matching elements are found, no element is removed and
0 (zero) is returned.
The
following example is identical to the previous example with one exception:
remove_all() is called to find all occurrences of
my_middle_key and remove them from the ordered list.
This results in the second list on this Web page containing only item1
and item4 . All occurrences of my_middle_key
( item2 and item3 ) are
removed.
#include <iostream.h>
#include <ospace/web.h>
int main()
{
os_page page( "Remove2 Example" );
os_list_item item1( "item1" );
os_list_item item2( "item2" );
os_list_item item3( "item3" );
os_list_item item4( "item4" );
item1.key( "my_first_key" );
item2.key( "my_middle_key" );
item3.key( "my_middle_key" );
item4.key( "my_last_key" );
os_ordered_list ol( "Before:" );
ol << item1 << item2 << item3 << item4;
page << ol;
ol.remove_all( "my_middle_key" );
ol.header( "After: item2 and item3 have been removed" );
page << ol;
cout << page;
return 0;
}

Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.