Using Replace |
There are two types of
replace operations allowed on any object that inherits from the class os_element_group
.
Use the function replace_first()
to find the first occurrence of a key and replace it with another element. If
no matching element is found, replace_first()
returns false .
In the following example, five os_list_item
objects are defined. The first four are assigned keys. The objects 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, named item2
and item3 . A key is not given to the fifth list
item, named replacement .
The ordered list containing only
the first four items is then created and added to the Web page. Following
this, the function replace_first() is called. It
replaces the first occurrence of my_middle_key with
the element replacement .
The list is inserted in the Web page twice to show the list both before and after the replace operation. The first list contains four elements named item1 , item2 , item3 , and item4 , respectively. The second list also contains four elements, but in this case the first occurrence of my_middle_key ( item2 ) is replaced with replacement .
#include <iostream.h>
#include <ospace/web.h>
int main()
{
os_page page( "Replace1 Example" );
os_list_item item1( "item1" );
os_list_item item2( "item2" );
os_list_item item3( "item3" );
os_list_item item4( "item4" );
os_list_item replacement( "replacement item" );
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.replace_first( "my_middle_key", replacement );
ol.header( "After: item2 has been replaced" );
page << ol;
cout << page;
return 0;
}

Use the function replace_all()
to find all occurrences of a key and replace them with another element. The
function replace_all() returns the number of
elements replaced. If no matching elements are found, no element is replaced
and 0 (zero) is returned.
The
following example is identical to the previous example with one exception: replace_all()
is called to find all occurrences of my_middle_key
and replace them with the element replacement .
The first list contains four elements named item1 , item2 , item3 , and item4 , respectively. The second list also contains four elements, but in this case all occurrences of my_middle_key ( item2 and item3 ) are replaced with replacement .
#include <iostream.h>
#include <ospace/web.h>
int main()
{
os_page page( "Replace2 Example" );
os_list_item item1( "item1" );
os_list_item item2( "item2" );
os_list_item item3( "item3" );
os_list_item item4( "item4" );
os_list_item replacement( "replacement item" );
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.replace_all( "my_middle_key", replacement );
ol.header( "After: item2 and item3 have been replaced" );
page << ol;
cout << page;
return 0;
}

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