Sorted Vector


This is a dynamic array that automatically sorts itself. Access can be done via a fast binary search with the lowest access time. The user can focus on adding items to the array and does not have to worry about sorting or re-sizing the array.

insert() and add() are identical except insert uses a faster pass by reference.

remove() is for removing an element, while removeAt() removes the object at a given index.

modify() changes the object at a given index, it then re-sorts the array.

find() and locate() return the index at which an element is found, or -1 when the object is not in the array. find() uses pass by reference, which is the only difference between the two methods.

number() returns the actual number of sorted objects, not the size of array. Thus, number() is generally smaller than size, and is a safe abstraction to use in loops.

The following is an example using sorted vector.

Example <ospace/advanced/examples/Sarray1.cpp>
#include <ospace/std/iostream>
#include <ospace/std/string>
#include <ospace/advanced/SortVctr.h>

using namespace std;


int 
main( ) 
   {
   os_Sorted_Vector<string> sVecStirng;

   // Use add when passing literal values

   sVecStirng.add("one");
   sVecStirng.add("two");
   sVecStirng.add("nine");

   string temp;

   // Use insert for passing objects by reference.

   temp = "five";
   sVecStirng.insert(temp);
   temp = "eight";
   sVecStirng.insert(temp);

   // The loop prints the entire sorted array
   for (int i = 0; i < sVecStirng.number(); i++)
      cout << sVecStirng[i] << endl;

   return 0;
   }


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