United States    
COMPAQ STORE | PRODUCTS |
SERVICES | SUPPORT | CONTACT US | SEARCH
cxxtitle.gif (12116 bytes)
Compaq C++

Compaq C++
Using Compaq C++ for Tru64 UNIX Systems


Previous Contents Index

6.4.2.2 Upgrading from the Compaq C++ Class Library Stack to the STL Stack

To change your code from using the existing stack to the STL stack, consider the following actions:

6.4.3 Differences Between STL Tutorial and Reference Guide and the Compaq C++ STL

The STL Tutorial and Reference Guide (the Guide) is based on the STL implementation provided by the Hewlett-Packard Company. Because the Compaq C++ STL incorporates changes from the ANSI C++ draft, the two implementations differ slightly. Differences also exist because the Compaq C++ compiler does not currently support all of the new language features specified in the ANSI C++ draft; refer to Section 6.3 for the list of missing language features. This section discusses the remaining differences.

6.4.3.1 Header File Names

The header file names shown in the STL Tutorial and Reference Guide differ from the Compaq C++ header file names, as shown in the following table:
Names Used in the Guide Names Supplied by Compaq C++
algo.h Divided into algorithm and numeric
deque.h deque
function.h functional
iterator.h iterator
list.h list
multiset.h set
map.h map
multimap.h map
set.h set
stack.h Divided into stack and queue
vector.h vector

6.4.3.2 STL Run-Time Support

The STL Tutorial and Reference Guide states that you need to compile the run-time .cpp files that are provided with the Hewlett-Packard implementation, such as random.cpp and tempbuf.cpp.

Compaq C++ already provides this run-time support in an object library and automatically links this library into your application; therefore, you can ignore the Guide's comments about compiling this support.

6.4.3.3 Guide Examples Need to Be Modified

The STL Tutorial and Reference Guide contains many example programs. Appendix B of the Guide shows a World Wide Web address (active as of this printing) from which you can copy these example programs. Because of the differences outlined in the previous sections, these example programs must be modified to work with the Compaq C++ STL. To perform the necessary modifications, Compaq C++ provides the following script:


/usr/lib/cmplrs/cxx/update_stl_book_examples.sh 

Before running this script, copy and prepare the example programs. Use the following steps as guidelines:

  1. Determine whether you want to use the unzip or gunzip facility. For greater simplicity in preparing the example programs, use unzip.
    If neither unzip nor gunzip is available on your system, you can copy them from the following location:


    ftp://ftp.digital.com/pub/Alpha/apps 
    

  2. Copy the example programs from the following World Wide Web address specified in the Guide:


    http://www.aw.com/cp/musser-saini.html 
    

  3. If you are using unzip, unzip the files into a directory as follows:


    unzip diction.zip 
    unzip examples.zip 
    

  4. If you are using gunzip, unzip the files and unpack the examples into a directory as follows:


    gunzip diction.gz 
    gunzip examples.taz 
    tar -xvf examples.tar 
    

    Because the resulting files have uppercase names, you will need to manually rename them to lowercase names.

  5. Change your current working directory to the directory where the example programs exist, and execute the script to modify the files as follows:


    sh /usr/lib/cmplrs/cxx/update_stl_book_examples.sh 
    

Now that your example programs are ready, compile them with the following command:


cxx -nopt -define_templates -writable_strings -I. ex.cpp 

To link ex17-01, you must compile screen.cpp and shape.cpp and link with the resulting object files.

Note

The header file, bstring.h, which is included with the example programs, is based on the string library as defined by an old version of the ANSI C++ draft. Compaq recommends that you do not use this header file and instead use the <string> header provided by Compaq C++.

6.4.3.4 Differences by Chapter

This section describes functional differences between the Compaq C++ STL and the STL Tutorial and Reference Guide by chapter.

Chapter 6

Chapter 18

The Compaq C++ STL description of the reverse_iterator type definition differs from the description in §18.9 of the Guide as follows:

Chapter 19

Chapter 20

Chapter 22

See also Section 6.5.5 for differences between the Guide string class and the Compaq C++ Standard String Library.

6.4.4 Optional Switch to Control Buffering

The inplace_merge, stable_sort, and stable_partition algorithms require the use of a temporary buffer. Two methods are available for allocating this buffer:

By default, the current Compaq C++ library makes use of the preallocated buffer, which avoids the overhead of run-time allocation. If your application requires a buffer that exceeds 16K, it can not take advantage of this default.

If you are concerned with minimizing the use of stack space in your program, or if your application requires a buffer that exceeds 16K, define the __DEC_DYN_ALLOC macro to enable dynamic buffering. Do this by adding the following to your compile command line:


-D__DEC_DYN_ALLOC 

6.5 The basic_string Library

Compaq C++ provides an implementation of the basic_string Library that consists of the basic_string template class, with parameters for the following:

Note

This manual refers to the new standard String class implementation in Compaq C++ as the basic_string Library. This is in contrast with an earlier (still maintained) implementation known as the String Package.

The basic_string Library also declares a string typedef to represent the typical usage, which is a string of normal (skinny) characters. For example:


#include <string> 
 
string str("abc"); // create a string containing "abc" 
str.append('d');   // str now contains "abcd" 
cout << s << endl; // print str to cout 

The wide character typedef (wstring) is not yet supported.

6.5.1 The basic_string Member Functions

The basic_string class can be used as character text, as an array of characters, or as an STL-like sequence of characters. The string member functions described below support these alternative uses.

6.5.1.1 Constructors/Destructors/Assignment

basic_string(const Allocator& a = Allocator());

Constructs an empty string.

basic_string(const basic_string& str, size_type pos = 0, size_type n = npos, const Allocator& a = Allocator());

Constructs a string by copying all elements from str, starting at position pos in str and ending at position pos + n.

basic_string(const charT* s, size_type n,const Allocator& a = Allocator());

basic_string(const charT* s, const Allocator& a = Allocator());

Constructs a string from s. If n is supplied it copies only the first n characters from s.

basic_string(size_type n, charT c, const Allocator& a = Allocator());

Constructs a string with n copies of c; that is: string(4,'a') would construct a string of aaaa.

basic_string(iterator begin, iterator end, const Allocator& a = Allocator());

Constructs a string from the characters between begin and end.

~basic_string();

Destroys the string and frees all associated memory.

basic_string& operator=(const basic_string& str);
basic_string& operator=(const charT* s);

basic_string& operator=(charT c);

Assigns str, s, or c to the current string.

6.5.1.2 Capacity

size_type size() const;

size_type length() const;

Returns a count of the number of characters in the string.

size_type max_size() const;

Specifies the maximum number of elements that can be in the string, usually limited by the amount of available memory.

void resize(size_type n);

If n is less than the current value of size(), resize() truncates the string to length n. If n is greater than the current value of size(), resize() expands the string to length n and pads the string with null characters.

void resize(size_type n, charT c);

If n is less than the current value of size(), resize() truncates the string to length n. If n is greater than the current value of size(), resize() expands the string to length n and pads the string with c characters.

size_type capacity() const;

Returns the number of elements allocated in the string.

void reserve(size_type res_arg);

Reallocates the string if the current capacity is less than res_arg. Invalidates all iterators, pointers, and references to the string.

bool empty() const;

Returns true if length is zero, otherwise returns false.

6.5.1.3 Element access

charT operator[](size_type pos) const;
reference operator[](size_type pos);

charT at(size_type pos) const;
reference at(size_type pos);

The subscript operator returns the character at position pos in the string. The at() function is similar except it throws an out-of-range exception if pos is invalid, that is, if it is greater than the current length of the string.

6.5.1.4 Modifiers

basic_string& operator+=(const basic_string& str);
basic_string& operator+=(const charT* s); basic_string& operator+=(charT c);

basic_string& append(const basic_string& str);
basic_string& append(const charT* s);

Appends str, s, or c to the string and returns *this.

basic_string& append(const basic_string& str, size_type pos,
size_type n);

Appends a string constructed by copying all elements from str onto the string, starting at position pos in str and ending at position pos + n. Returns *this.

basic_string& append(const charT* s, size_type n);

Appends onto the string the string created from the first n characters of s. Returns *this.

basic_string& append(size_type n, charT c);

Appends onto the string n number of the value of c. Returns *this.

basic_string& append(iterator first, iterator last);

Appends onto the current string the string constructed from the elements in the range first to last. Returns *this.

basic_string& assigns(const basic_string& str);

basic_string& assigns(const charT* s);

Assigns str or s to the current string. Returns *this.

basic_string& assign(const basic_string& str, size_type pos, size_type n);

Assigns a string constructed by copying onto the current string all elements from str, starting at position pos in str and ending at position pos + n. Returns *this.

basic_string& assign(const charT* s, size_type n);

Assigns the string created from the first n characters of s to the current string. Returns *this.

basic_string& assign(size_type n, charT c);

Assigns n number of values of c to the string. Returns *this.

basic_string& assign(iterator first, iterator last);

Assigns to the current string the string constructed from the elements in the range first to last. Returns *this.

basic_string& insert(size_type pos1, const basic_string& str);

Inserts str into the string after position pos1. Returns *this. If pos1 is greater than the size of the string, this function throws a length-error exception. Returns *this.

basic_string& insert(size_type pos1, const basic_string& str, size_type pos2, size_type n);

Inserts into the string after position pos1 the elements of str beginning at position pos2 and ending at either pos2+n or the end of str (whichever is smallest). If pos1 is greater than the size of the string or pos2 is greater than the size of str, this function throws a length-error exception. Returns *this.

basic_string& insert(size_type pos, const charT* s);

Inserts the character string s into the string beginning after position pos. Returns *this.

basic_string& insert(size_type pos, const charT* s, size_type n);

Inserts the first n characters of the character string s into the string after position pos. Returns *this.

basic_string& insert(size_type pos, size_type n, charT c);

Inserts n copies of the character c into the string after position pos. Returns *this.

iterator insert(iterator p, charT c);

Inserts the character c into the string before the character referred to by p and returns an iterator pointing at the element immediately following p prior to the element being inserted.

void insert(iterator p, size_type n, charT c);

Inserts the n copies of the character c into the string before the character referred to by p.

void insert(iterator p, iterator first, iterator last);

Inserts before the character referred to by p the characters in the string from first up to but not including last

basic_string& erase(size_type pos = 0, size_type n = npos);

Erases from the string the characters beginning at position pos and ending at position n. Returns *this.

iterator erase(iterator pos);

Erases the character in the string at pos and returns an iterator pointing at the element immediately following p prior to the element being erased.

iterator erase(iterator first, iterator last);

Erases the characters in the string from first up to but not including last and returns an iterator pointing at the element immediately following last prior to the element being erased.

basic_string& replace(size_type pos1, size_type n1, const basic_string& str);

Removes the characters in the string from pos1 to pos1+n1 or to the end of the string (whichever is smaller). Inserts str into the string at pos1. Returns *this. If pos1 is greater than the size of the string, this function throws an out-of-range exception.

basic_string& replace(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n);

Removes the characters in the string from pos1 to pos1+n1 or to the end of the string (whichever is smaller). Inserts the string created by using the characters in str from pos2 to pos2+n or the end of str (whichever is less) into the string at pos1. Returns *this. If pos1 is greater than the size of the string or pos2 is greater than str, this function throws an out-of-range exception. If the size of the string minus the size of the string to be removed is greater than the npos minus the size of the string to be inserted, this function throws an length-error exception.

basic_string& replace(size_type pos1, size_type n1, const charT* s);

Removes the characters in the string from pos1 to pos1+n1 or to the end of the string (whichever is smaller). Inserts s into the string at pos1. Returns *this. If pos1 is greater than the size of the string, this function throws an out-of-range exception.

basic_string& replace(size_type pos1, size_type n1, const charT* s,
size_type n2);

Removes the characters in the string from pos1 to pos1+n1 or to the end of the string (whichever is smaller). Inserts the string created by copying the first n2 characters of s into the string at pos1. Returns *this. If pos1 is greater than the size of the string, this function throws an out-of-range exception.

basic_string& replace(size_type pos1, size_type n1, size_type n2,
charT c);

Removes the characters in the string from pos1 to pos1+n1 or to the end of the string (whichever is smaller). Inserts the string created by copying the first n2 characters of the character c into the string at pos1. Returns *this. If pos1 is greater than the size of the string, this function throws an out-of-range exception.

basic_string& replace(iterator i1, iterator i2, const basic_string& str);

Removes the characters in the range i1 to i2 and inserts str at i1. Returns *this.

basic_string& replace(iterator i1, iterator i2, const charT* s);

Removes the characters in the range i1 to i2 and inserts s at i1. Returns *this.

basic_string& replace(iterator i1, iterator i2, const charT* s, size_type n);

Removes the characters in the range i1 to i2 and inserts n characters of s at i1. Returns *this.

basic_string& replace(iterator i1, iterator i2, size_type n, charT c);

Removes the characters in the range i1 to i2 and inserts n copies of the character c at i1. Returns *this.

basic_string& replace(iterator i1, iterator i2, iterator i3, iterator i4);

Removes the characters in the range i1 to i2 and inserts the characters in the range i3 to i4. Returns *this.

size_type copy(charT* s, size_type n, size_type pos = 0) const;

Replaces s with the elements from the string beginning at position pos and ending at either pos+n or the end of the string (whichever is smaller). If pos is greater than the value of size(), this function throws an out-of-range exception. It returns the size of the new string s. Before calling the copy() function, s must have already been allocated to have as least as many elements as needed to hold the results of the copy.

void swap(basic_string<charT, traits, Allocator>& s);

Swaps the contents (that is, the data members) of s and *this.


Previous Next Contents Index
  

1.800.AT.COMPAQ

privacy and legal statement