first commit

This commit is contained in:
Jose Caban
2025-06-07 11:34:38 -04:00
commit 0eb2d7c07d
4708 changed files with 1500614 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
/**************************************************************************
*
* accum.cpp - Example program for accumulate.
*
* $Id: accumulate.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <numeric> // for accumulate
#include <vector> // for vector
#include <functional> // for multiplies
#include <iostream> // for cout
#include <examples.h>
int main ()
{
// Typedef for convenience.
typedef std::vector<int, std::allocator<int> > Vector;
// Initialize a vector using an array of integers.
const Vector::value_type arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Vector v1 (arr, arr + sizeof arr / sizeof *arr);
// Accumulate sums and products.
Vector::value_type sum =
std::accumulate (v1.begin (), v1.end (), 0);
Vector::value_type prod =
std::accumulate (v1.begin (), v1.end (), 1,
std::multiplies<Vector::value_type>());
// Output the results.
std::cout << "For the series: ";
for (Vector::iterator i = v1.begin (); i != v1.end (); ++i)
std::cout << *i << " ";
std::cout << "where N = " << v1.size ()
<< "\nThe sum = (N * N + N) / 2 = " << sum
<< "\nThe product = N! = " << prod << std::endl;
return 0;
}

View File

@@ -0,0 +1,87 @@
/**************************************************************************
*
* adj_diff.cpp - Example program for adjacent_difference.
*
* $Id: adj_diff.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <numeric> // for adjacent_difference
#include <vector> // for vector
#include <iostream> // for cout
#include <iterator> // for ostream_iterator
#include <functional> // for multiplies
#include <examples.h>
int main ()
{
// Typedefs for convenience.
typedef std::vector<long, std::allocator<long> > Vector;
typedef std::ostream_iterator <Vector::value_type,
char,
std::char_traits<char> > os_iter;
// Initialize a Vector of ints from an array.
const Vector::value_type arr [] = {
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
};
Vector v (arr + 0, arr + sizeof arr / sizeof *arr);
// Two vectors initialized to all zeros for storing results.
Vector diffs (v.size ()),
prods (v.size ());
// Calculate difference(s) using default operator (minus).
std::adjacent_difference (v.begin (), v.end (), diffs.begin ());
// Calculate difference (s) using the times operator.
std::adjacent_difference (v.begin (), v.end (), prods.begin (),
std::multiplies<Vector::value_type> ());
// Create an ostream_iterator object (for convenience).
os_iter osit (std::cout, " ");
// Output the results.
std::cout << "For the vector: " << std::endl << " ";
std::copy (v.begin (), v.end (), osit);
std::cout << std::endl << std::endl;
std::cout << "The differences between adjacent elements are: "
<< std::endl << " ";
std::copy (diffs.begin (), diffs.end (), osit);
std::cout << std::endl << std::endl;
std::cout << "The products of adjacent elements are: "
<< std::endl << " ";
std::copy (prods.begin (), prods.end (), osit);
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,64 @@
/**************************************************************************
*
* advance.cpp - Example program for advancing iterators.
*
* $Id: advance.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iterator> // for advance
#include <iostream> // for cout, endl
#include <list> // for list
#include <examples.h>
int main ()
{
// Typedefs for convenience.
typedef std::list<long, std::allocator<long> > List;
typedef std::ostream_iterator<List::value_type,
char, std::char_traits<char> > os_iter;
// Initialize a list using an array.
const List::value_type arr [] = { 3, 4, 5, 6, 7, 8 };
List l (arr + 0, arr + sizeof arr / sizeof *arr);
// Declare a list iterator, s.b. a ForwardIterator.
List::iterator itr = l.begin ();
// Output the original list.
std::cout << "For the list: ";
std::copy (l.begin (), l.end (), os_iter (std::cout, " "));
std::cout << std::endl << std::endl
<< "When the iterator is initialized to l.begin (),"
<< "\nit points to " << *itr << std::endl;
// operator+ is not available for a ForwardIterator, so use advance.
std::advance (itr, 4);
std::cout << "\nAfter advance (itr, 4), the iterator points to "
<< *itr << std::endl;
return 0;
}

View File

@@ -0,0 +1,79 @@
/**************************************************************************
*
* auto_ptr.cpp - Example program of auto_ptr.
*
* $Id: auto_ptr.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout, endl
#include <memory> // for auto_ptr
#include <examples.h>
// A simple structure.
class X
{
int i_;
public:
X (int i) : i_ (i) {
std::cout << "X::X (" << i_ << ')' << std::endl;
}
~X () {
std::cout << "X::~X [" << i_ << ']' << std::endl;
}
int get () const { return i_; }
};
int main ()
{
// a implicitly initialized to 0 (the null pointer)
std::auto_ptr<X> a;
// Establish a scope.
if (1) {
// b will hold a pointer to an X.
std::auto_ptr<X> b (new X (12345));
// a will now be the owner of the underlying pointer.
a = b;
}
std::cout << "b destroyed" << std::endl;
// Output the value contained by the underlying pointer.
#ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
std::cout << a->get () << std::endl;
#else
std::cout << (*a).get () << std::endl;
#endif
// The pointer will be delete'd when a is destroyed on leaving scope.
return 0;
}

View File

@@ -0,0 +1,73 @@
/**************************************************************************
*
* binary_search.cpp - Example program of binary search.
*
* $Id: binary_search.cpp 590052 2007-10-30 12:44:14Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for binary_search(), copy(), sort()
#include <deque> // for deque
#include <functional> // for less
#include <iostream> // for cout
#include <iterator> // for ostream_iterator, char_traits
#include <examples.h>
int main ()
{
// Typedefs for convenience.
typedef short Value;
typedef std::deque<Value, std::allocator<Value> > Deque;
typedef std::char_traits<char> Traits;
typedef std::ostream_iterator<Value, char, Traits> Iterator;
const Value arr[] = { 0, 1, 2, 2, 3, 4, 2, 2, 6, 7 };
// Populate and sort the container.
Deque d (arr + 0, arr + sizeof arr / sizeof *arr);
std::sort (d.begin (), d.end ());
// Arbitrary values to search for.
const Value val_1 = 3;
const Value val_2 = 11;
// Try binary_search variants.
const bool found_1 = std::binary_search (d.begin (), d.end (), val_1);
const bool found_2 = std::binary_search (d.begin (), d.end (), val_2,
std::less<Value>());
// Output the sorted sequence.
std::cout << "Container contents: ";
std::copy (d.begin (), d.end (), Iterator (std::cout, " "));
// Output the results of the algorithms.
std::cout << "\nThe number " << val_1 << " was "
<< (found_1 ? "" : "NOT ") << "found";
std::cout << "\nThe number " << val_2 << " was "
<< (found_2 ? "" : "NOT ") << "found\n";
return 0;
}

View File

@@ -0,0 +1,70 @@
/**************************************************************************
*
* binders.cpp - Example program for binder1st & binder2nd.
*
* $Id: binders.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for find_if
#include <functional> // for equal_to, bind1st, bind2nd
#include <iostream> // for cout, endl
#include <vector> // for vector
#include <examples.h>
int main ()
{
typedef std::vector<int, std::allocator<int> > Vector;
typedef std::equal_to<Vector::value_type> equal_to;
const Vector::value_type arr [] = { 1, 2, 3, 4 };
// Set up a vector.
Vector v1 (arr + 0, arr + sizeof arr / sizeof *arr);
// Create an 'equal to 3' unary predicate by binding 3 to
// the equal_to binary predicate.
std::binder1st<equal_to> equal_to_3 = std::bind1st (equal_to (), 3);
// Now use this new predicate in a call to find_if.
Vector::iterator it1 = std::find_if (v1.begin (), v1.end (),
equal_to_3);
// Even better, construct the new predicate on the fly.
Vector::iterator it2 =
std::find_if (v1.begin (), v1.end (),
std::bind1st (equal_to (), 3));
// And now the same thing using bind2nd.
// Same result since equal_to is commutative.
Vector::iterator it3 =
std::find_if (v1.begin (), v1.end (),
std::bind2nd (equal_to (), 3));
// Output results.
std::cout << *it1 << " " << *it2 << " " << *it3 << std::endl;
return 0;
}

View File

@@ -0,0 +1,60 @@
/**************************************************************************
*
* bitset.cpp - Example program for bitset.
*
* $Id: bitset.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <bitset> // for bitset
#include <iostream> // for cout, endl
#include <examples.h>
int main ()
{
// Construct a bitset with 40 elements all initialized to 0.
std::bitset<40> b;
// Bitwise OR the bitset with 5.
b |= 5;
// Write bitset to the standard output.
std::cout << " 4....:....3....:....2....:....1....:....0\n"
<< "b = 5: " << b << '\n';
// Invert the bitset.
b = ~b;
std::cout << "b = ~b: " << b << '\n'
<< "b <<= " << b.count () << ": ";
// Shift the bitset left by the number of bits set.
b <<= b.count ();
// Convert it to a string and write it out again.
std::cout << b.to_string () << '\n';
// Return 0 on success.
return !(1 == b.count () && 1UL == (b >> (b.size () - 1)).to_ulong ());
}

View File

@@ -0,0 +1,89 @@
/**************************************************************************
*
* codecvt.cpp - Example program of codecvt facet.
*
* $Id: codecvt.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1998-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream>
#include <codecvte.h>
#include <examples.h>
int main ()
{
// not used, must be zero-initialized and supplied to facet
std::mbstate_t state = std::mbstate_t ();
// three strings to use as buffers
std::string ins ("\xfc \xcc \xcd \x61 \xe1 \xd9 \xc6 \xe6 \xf5");
std::string ins2 (ins.size (), '.');
std::string outs (ins.size () / ex_codecvt ().encoding (), '.');
// Print initial contents of buffers
std::cout << "Before:\n"
<< ins << '\n'
<< ins2 << '\n'
<< outs << '\n' << '\n';
// Create a user defined codecvt fact
// This facet converts from ISO Latin Alphabet No. 1 (ISO 8859-1)
// to U.S. ASCII code page 437.
// Replace the default codecvt<char, char, mbstate_t>.
std::locale loc (std::cout.getloc (), new ex_codecvt);
// Retrieve the facet from the locale.
typedef std::codecvt<char, char, std::mbstate_t> CodeCvt;
const CodeCvt& cdcvt = std::use_facet<CodeCvt>(loc);
// unused, must be provided to codecvt<>::in/out
const char *const_out_next = 0;
const char *const_in_next = 0;
char *in_next = 0;
char *out_next = 0;
// convert the buffer
cdcvt.in (state, ins.c_str (), ins.c_str () + ins.length (), const_in_next,
&outs [0], &outs [0] + outs.length (), out_next);
std::cout << "After in:\n"
<< ins << '\n'
<< ins2 << '\n'
<< outs << "\n\n";
// zero-initialize (unused) state object
state = std::mbstate_t ();
// Finally, convert back to the original codeset
cdcvt.out (state, outs.c_str (), outs.c_str () + outs.length (),
const_out_next, &ins [0], &ins [0] + ins.length (), in_next);
std::cout << "After out:\n"
<< ins << '\n'
<< ins2 << '\n'
<< outs << '\n';
return 0;
}

View File

@@ -0,0 +1,225 @@
/**************************************************************************
*
* codecvt1.cpp - Example program of codecvt facet.
*
* $Id: codecvt1.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <locale>
#include <numeric>
#include <cstring> // for memcmp(), ptrdiff_t
#include <cwchar> // for mbstate_t
#include <examples.h>
#define TMP_BUFFER_SIZE 1024
// Japanese week days (short/abbreviated and long/normal form) in EUC-JP,
// Shift_JIS and UTF-8 encodings;
const char wd_eucjp [] = {
"\xc6\xfc\x0a\xc6\xfc\xcd\xcb\xc6\xfc\x0a\xb7\xee\x0a\xb7\xee\xcd"
"\xcb\xc6\xfc\x0a\xb2\xd0\x0a\xb2\xd0\xcd\xcb\xc6\xfc\x0a\xbf\xe5"
"\x0a\xbf\xe5\xcd\xcb\xc6\xfc\x0a\xcc\xda\x0a\xcc\xda\xcd\xcb\xc6"
"\xfc\x0a\xb6\xe2\x0a\xb6\xe2\xcd\xcb\xc6\xfc\x0a\xc5\xda\x0a\xc5"
"\xda\xcd\xcb\xc6\xfc\x0a"
};
const char wd_sjis [] = {
"\x93\xfa\x0a\x93\xfa\x97\x6a\x93\xfa\x0a\x8c\x8e\x0a\x8c\x8e\x97"
"\x6a\x93\xfa\x0a\x89\xce\x0a\x89\xce\x97\x6a\x93\xfa\x0a\x90\x85"
"\x0a\x90\x85\x97\x6a\x93\xfa\x0a\x96\xd8\x0a\x96\xd8\x97\x6a\x93"
"\xfa\x0a\x8b\xe0\x0a\x8b\xe0\x97\x6a\x93\xfa\x0a\x93\x79\x0a\x93"
"\x79\x97\x6a\x93\xfa\x0a"
};
const char wd_utf8 [] = {
"\xe6\x97\xa5\x0a\xe6\x97\xa5\xe6\x9b\x9c\xe6\x97\xa5\x0a\xe6\x9c"
"\x88\x0a\xe6\x9c\x88\xe6\x9b\x9c\xe6\x97\xa5\x0a\xe7\x81\xab\x0a"
"\xe7\x81\xab\xe6\x9b\x9c\xe6\x97\xa5\x0a\xe6\xb0\xb4\x0a\xe6\xb0"
"\xb4\xe6\x9b\x9c\xe6\x97\xa5\x0a\xe6\x9c\xa8\x0a\xe6\x9c\xa8\xe6"
"\x9b\x9c\xe6\x97\xa5\x0a\xe9\x87\x91\x0a\xe9\x87\x91\xe6\x9b\x9c"
"\xe6\x97\xa5\x0a\xe5\x9c\x9f\x0a\xe5\x9c\x9f\xe6\x9b\x9c\xe6\x97"
"\xa5\x0a"
};
const struct {
const char* name;
const char* buffer;
} locales [] = {
{ "ja_JP.UTF-8" , wd_utf8 },
{ "ja_JP.Shift_JIS", wd_sjis },
{ "ja_JP.EUC-JP" , wd_eucjp }
};
std::mbstate_t init = std::mbstate_t ();
// Conversion from external representation of characters to internal
// representation of characters and back to external representation
// and checking of the transformation correctness after round-trip.
void do_roundtrip_conversion (const char* locname, const char* buffer)
{
typedef std::codecvt_byname<wchar_t,char,std::mbstate_t> wcodecvt_byname_t;
wchar_t wtmp [TMP_BUFFER_SIZE];
char tmp [TMP_BUFFER_SIZE];
std::mbstate_t state = init;
std::codecvt_base::result res;
// create the named facet
std::locale loc (locname);
const wcodecvt_byname_t& cc =
std::use_facet<wcodecvt_byname_t> (loc);
// set the pointers
const char* cpe = buffer;
const char* cpen = buffer;
const char* cpe_limit = buffer + std::strlen (buffer);
wchar_t* pi = wtmp;
wchar_t* pin = wtmp;
wchar_t* pi_limit = wtmp + TMP_BUFFER_SIZE;
// convert characters in external buffer to internal representation
res = cc.in (state, cpe, cpe_limit, cpen, pi, pi_limit, pin);
// set the pointers and adjust the pi_limit pointer to after
// the last successfully converted character
const wchar_t* cpi = pi;
const wchar_t* cpin = pi;
const wchar_t* cpi_limit = pin;
char* pe = tmp;
char* pen = tmp;
char* pe_limit = tmp + TMP_BUFFER_SIZE;
// convert the characters in internal representation to external
// representation and compare the result with the original buffer
res = cc.out (state, cpi, cpi_limit, cpin, pe, pe_limit, pen);
std::cout << locname << " -> INT -> " << locname << '\n';
std::cout << " Size comparison of buffers yields "
<< (std::codecvt_base::ok == res && (cpen - cpe == pen - pe)
? "equal\n" : "not equal\n");
const int cmp = std::memcmp (cpe, pe, std::min ((cpen - cpe), (pen - pe)));
std::cout << " Content comparison of buffers yields "
<< (cmp ? "not equal\n" : "equal\n");
}
void do_diff_conversion ()
{
typedef std::codecvt_byname<wchar_t,char,std::mbstate_t> wcodecvt_byname_t;
wchar_t wtmp [TMP_BUFFER_SIZE];
char tmp [TMP_BUFFER_SIZE];
std::codecvt_base::result res;
// Conversion from external representation of characters to internal
// representation of characters using two different facets obtained
// from two different locales:
// 1. External to internal: codecvt_byname facet from ja_JP.EUC-JP locale
// transforms the content of wd_eucjp to internal representation;
// 2. Internal to external: codecvt_byname facet from ja_JP.UTF-8 locale
// transforms the internal representation of wd_eucjp to an external
// representation that uses UTF-8 encoding;
// 3. Compare the size and content of the final result with size and
// content of the wd_utf8 buffer.
const std::locale loc1 ("ja_JP.EUC-JP");
const std::locale loc2 ("ja_JP.UTF-8");
const wcodecvt_byname_t& cc1 = std::use_facet<wcodecvt_byname_t> (loc1);
const wcodecvt_byname_t& cc2 = std::use_facet<wcodecvt_byname_t> (loc2);
std::mbstate_t state1 = init;
std::mbstate_t state2 = init;
const std::size_t len = std::strlen (wd_utf8);
// set the pointers
const char* cpe = wd_eucjp;
const char* cpen = wd_eucjp;
const char* cpe_limit = wd_eucjp + std::strlen (wd_eucjp);
wchar_t* pi = wtmp;
wchar_t* pin = wtmp;
wchar_t* pi_limit = wtmp + TMP_BUFFER_SIZE;
// convert external buffer to internal representation
res = cc1.in (state1, cpe, cpe_limit, cpen, pi, pi_limit, pin);
// set the pointers and adjust the pi_limit pointer to after
// the last converted character
const wchar_t* cpi = pi;
const wchar_t* cpin = pi;
const wchar_t* cpi_limit = pin;
char* pe = tmp;
char* pen = tmp;
char* pe_limit = tmp + TMP_BUFFER_SIZE;
res = cc2.out (state2, cpi, cpi_limit, cpin, pe, pe_limit, pen);
std::cout << "\nEUC-JP -> INT -> UTF-8 conversion\n";
std::cout << " Size comparison of buffers yields "
<< (std::codecvt_base::ok == res && std::size_t (pen - pe) == len
? " " : "not ")
<< "equal\n";
const int cmp =
std::memcmp (pe, wd_utf8, std::min ((pen - pe), std::ptrdiff_t (len)));
std::cout << " Content comparison of buffers yields "
<< (cmp ? "not equal\n" : "equal\n");
}
int main ()
{
try {
for (int i = 0; i != sizeof locales / sizeof *locales; i++)
do_roundtrip_conversion (locales [i].name, locales [i].buffer);
do_diff_conversion ();
}
catch (const std::exception &e) {
std::cout << "Caught an exception: " << e.what () << std::endl;
return 1; // Indicate failure.
}
catch (...) {
std::cout << "Caught an unknown exception" << std::endl;
return 1; // Indicate failure.
}
return 0;
}

View File

@@ -0,0 +1,87 @@
/**************************************************************************
*
* collate.cpp - Example program of collate facet.
*
* $Id: collate.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout
#include <locale> // for collate
#include <examples.h>
int main ()
{
const std::string s1 ("blue");
const std::string s2 ("blues");
std::cout << "String 1 : " << s1
<< "\nString 2 : " << s2 << '\n';
// obtain a reference to the collate facet
// installed in the locale imbued in cout
const std::collate<char>& co =
std::use_facet<std::collate<char> >(std::cout.getloc ());
// use the facet to compare the two strings
int res = co.compare (s1.c_str (), s1.c_str () + s1.length (),
s2.c_str (), s2.c_str () + s2.length () - 1);
// strings should collate equal
std::cout << "Strings \""
<< s1.substr(0, s1.length ()) << "\" and \""
<< s2.substr(0, s2.length () - 1)
<< "\" should compare equal : "
<< (res ? "not " : "") << "equal\n";
res = co.compare (s1.c_str (), s1.c_str () + s1.length (),
s2.c_str (), s2.c_str () + s2.length ());
// strings should collate unequal
std::cout << "Strings \""
<< s1.c_str () << "\" and \"" << s2.c_str ()
<< "\" should compare NOT equal : "
<< (res ? "not " : "") << "equal\n";
// compute hash values for the two strings
// expected values:
// ILP32: 431029 and 6896579
// LP64: 1651275109 and 422726428019
const long hash1 = co.hash (s1.c_str (), s1.c_str () + s1.length ());
const long hash2 = co.hash (s2.c_str (), s2.c_str () + s2.length ());
// normalize the two hash values for portability
// between 32-bit and 64-bit longs
const long norm1 = 440460L;
const long norm2 = 7898347L;
// expect 431029 on output
std::cout << "Normalized hash value for \"" << s1.c_str () << "\" : "
<< hash1 % norm1
<< "\nNormalized hash value for \"" << s2.c_str () << "\" : "
<< hash2 % norm2
<< '\n';
return 0;
}

View File

@@ -0,0 +1,51 @@
/**************************************************************************
*
* complex.cpp - Example program for complex.
*
* $Id: complex.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <complex> // for complex
#include <iostream> // for cout, endl
#include <sstream> // for sstream
#include <examples.h>
int main ()
{
// Create two arbitrary complex numbers.
std::complex<double> a (1.2, 3.4);
std::complex<double> b (-9.8, -7.6);
// Perform some arithmetic on the numbers.
a += b;
a /= std::sin (b) * std::cos (a);
b *= std::log (a) + std::pow (b, a);
// Output result in fixed notation.
std::cout.setf (std::ios::fixed, std::ios::floatfield);
std::cout << "a = " << a << ", b = " << b << std::endl;
return 0;
}

View File

@@ -0,0 +1,81 @@
/**************************************************************************
*
* copyex.cpp - Example program for copy.
*
* $Id: copyex.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for copy
#include <iostream> // for cout, endl
#include <iterator> // for ostream_iterator
#include <vector> // for vector
#include <examples.h>
int main ()
{
// Typedef for convenience.
typedef std::vector<short, std::allocator<short> > Vector;
const Vector::value_type d1[] = { 1, 2, 3, 4 };
const Vector::value_type d2[] = { 5, 6, 7, 8 };
// Set up three vectors.
Vector v1 (d1 + 0, d1 + 4),
v2 (d2 + 0, d2 + 4),
v3 (d2 + 0, d2 + 4);
// Set up one empty vector.
Vector v4;
// Copy v1 to v2.
std::copy (v1.begin (), v1.end (), v2.begin ());
// Copy backwards v1 to v3.
std::copy_backward (v1.begin (), v1.end (), v3.end ());
// Use insert iterator to copy into empty vector.
std::copy (v1.begin (), v1.end (), std::back_inserter (v4));
// Copy all four vectors to cout.
std:: ostream_iterator<Vector::value_type,
char,
std::char_traits<char> >
out (std::cout, " ");
std::copy (v1.begin (), v1.end (), out);
std::cout << std::endl;
std::copy (v2.begin (), v2.end (), out);
std::cout << std::endl;
std::copy (v3.begin (), v3.end (), out);
std::cout << std::endl;
std::copy (v4.begin (), v4.end (), out);
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,105 @@
/**************************************************************************
*
* count.cpp - Example program for counting number of elements in container.
*
* $Id: count.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for count, count_if
#include <cassert> // for assert
#include <functional> // bind2nd, less
#include <iostream> // for cout
#include <iterator> // for ostream_iterator
#include <vector> // for vector
#include <examples.h>
// streams out a vector
template <class T>
std::ostream&
operator<< (std::ostream &out,
const std::vector<T, std::allocator<T> > &vec)
{
typedef std::ostream_iterator<T, char, std::char_traits<char> > Iter;
out << "{ ";
std::copy (vec.begin (), vec.end (), Iter (out, " "));
out << "}";
return out;
}
int main ()
{
// Typedef for convenience.
typedef std::vector<short, std::allocator<short> > Vector;
const Vector::value_type a [] = { 1, 2, 3, 4, 5, 5, 7, 8, 9, 10 };
// construct a vector from the array
const Vector vec (a + 0, a + 10);
Vector::size_type n_fives = 0;
Vector::size_type n_sixes = 0;
Vector::size_type n_lt_8 = 0;
#ifndef _RWSTD_NO_EXT_VOID_COUNT
// using the extended overloads of function template count()
// and count_if() provided by this implmentation, count the
// number of fives, sixes, and values less than 8 in the
// sequence
std::count (vec.begin (), vec.end (), 5, n_fives);
std::count (vec.begin (), vec.end (), 6, n_sixes);
std::count_if (vec.begin (), vec.end (),
std::bind2nd (std::less<Vector::value_type>(), 8),
n_lt_8);
assert (2 == n_fives);
assert (0 == n_sixes);
assert (7 == n_lt_8);
#endif // _RWSTD_NO_EXT_VOID_COUNT
// do the same using the standard function templates count()
// and count_if()
n_fives = std::count (vec.begin (), vec.end (), 5);
n_sixes = std::count (vec.begin (), vec.end (), 6);
n_lt_8 = std::count_if (vec.begin (), vec.end (),
std::bind2nd (std::less<Vector::value_type>(), 8));
std::cout << "sequence: " << vec;
std::cout << "\nnumber of fives: " << n_fives
<< "\nnumber of sixes: " << n_sixes
<< "\nnumber of values less than 8: " << n_lt_8 << '\n';
return 0;
}

View File

@@ -0,0 +1,56 @@
/**************************************************************************
*
* ctype.cpp - Example program of ctype facet.
*
* $Id: ctype.cpp 580483 2007-09-28 20:55:52Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2007 Rogue Wave Software, Inc.
*
**************************************************************************/
#include <iostream> // for cout, endl
#include <locale> // for ctype
#include <examples.h>
int main ()
{
std::locale loc;
char s [] = "blues Power";
// Get a reference to the ctype<char> facet.
const std::ctype<char>& ct = std::use_facet<std::ctype<char> >(loc);
// Check the classification of the 'a' character.
std::cout << ct.is (std::ctype_base::alpha, 'a') << std::endl;
std::cout << ct.is (std::ctype_base::punct, 'a') << std::endl;
// Scan for the first upper case character.
std::cout << *ct.scan_is (std::ctype_base::upper,
s, s + sizeof s / sizeof *s)
<< std::endl;
// Convert characters to upper case.
(ct.toupper)(s, s + sizeof s / sizeof *s);
std::cout << s << std::endl;
return 0;
}

View File

@@ -0,0 +1,86 @@
/**************************************************************************
*
* deque.cpp - Example program for deque class.
*
* $Id: deque.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <deque>
#include <iostream>
#include <string>
#include <examples.h>
typedef std::deque<std::string, std::allocator<std::string> > Deque;
Deque deck_of_cards;
Deque current_hand;
void initialize_cards (Deque &cards)
{
cards.push_front ("ace of spades");
cards.push_front ("king of spades");
cards.push_front ("queen of spades");
cards.push_front ("jack of spades");
cards.push_front ("ten of spades");
//
// etc.
//
}
template <class It, class It2>
void print_current_hand (It start, It2 end)
{
while (start < end)
std::cout << *start++ << std::endl;
}
template <class It, class It2>
void deal_cards (It, It2 end)
{
for (int i = 0; i < 5; i++) {
current_hand.insert (current_hand.begin (), *end);
deck_of_cards.erase (end++);
}
}
void play_poker ()
{
initialize_cards (deck_of_cards);
deal_cards (current_hand.begin (), deck_of_cards.begin ());
}
int main ()
{
play_poker ();
print_current_hand (current_hand.begin (), current_hand.end ());
return 0;
}

View File

@@ -0,0 +1,82 @@
/**************************************************************************
*
* distance.cpp - Example program for distance between two iterators.
*
* $Id: distance.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <cassert>
#include <iterator>
#include <iostream>
#include <vector>
#include <examples.h>
int main ()
{
// Typedefs for convenience.
typedef std::vector<int, std::allocator<int> > Vector;
typedef std::ostream_iterator<Vector::value_type,
char,
std::char_traits<char> > os_iter;
// Initialize a vector using an array.
const Vector::value_type arr [] = { 3, 4, 5, 6, 7, 8 };
const Vector v (arr + 0, arr + sizeof arr/ sizeof *arr);
// Declare a vector iterator, s.b. a ForwardIterator.
Vector::const_iterator it = v.begin () + 3;
// Output the original vector.
std::cout << "For the vector: ";
std::copy (v.begin (), v.end (), os_iter (std::cout, " "));
std::cout << '\n';
std::cout << "\nWhen the iterator is initialized to point to "
<< *it << '\n';
// Compute the distance of it from the first element.
Vector::difference_type dist = 0;
#ifndef _RWSTD_NO_EXT_VOID_DISTANCE
// using the extended overload of function template distance()
// provided by this implmentation, compute the distance between
// the two iterators
std::distance (v.begin (), it, dist);
assert (3 == dist);
#endif // _RWSTD_NO_EXT_VOID_DISTANCE
// do the same using the standard function template distance()
dist = std::distance (v.begin (), it);
std::cout << "The distance between the beginning and "
<< *it << " is " << dist << '\n';
return 0;
}

View File

@@ -0,0 +1,72 @@
/**************************************************************************
*
* equal.cpp - Example program for comparing two ranges for equivalence.
*
* $Id: equal.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for equal
#include <functional> // for equal_to
#include <ios> // for boolalpha
#include <iostream> // for cout, endl
#include <iterator> // for ostream_iterator
#include <vector> // for vector
#include <examples.h>
int main ()
{
// Typedef for convenience.
typedef std::vector<int, std::allocator<int> > Vector;
typedef std::ostream_iterator <Vector::value_type,
char,
std::char_traits<char> > os_iter;
const Vector::value_type a1[] = { 1, 2, 3, 4 };
const Vector::value_type a2[] = { 1, 2, 4, 3 };
// Set up two vectors.
const Vector v1 (a1, a1 + sizeof a1 / sizeof *a1);
const Vector v2 (a2, a2 + sizeof a2 / sizeof *a2);
// Check for equality.
bool same = std::equal (v1.begin (), v1.end (), v2.begin ());
same = same && std::equal (v1.begin (), v1.end (), v2.begin (),
std::equal_to<Vector::value_type>());
// Output the sequences.
std::cout << "{ ";
std::copy (v1.begin (), v1.end (), os_iter (std::cout, " "));
std::cout << "} == { ";
std::copy (v2.begin (), v2.end (), os_iter (std::cout, " "));
// Output the result.
std::cout << "} --> " << std::boolalpha << same << std::endl;
return 0;
}

View File

@@ -0,0 +1,66 @@
/**************************************************************************
*
* eqlrange.cpp - Example program for finding the valid range for insertion
* of a value in a container.
*
* $Id: equal_range.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#include <examples.h>
int main ()
{
// Typedef for convenience.
typedef std::vector<int, std::allocator<int> > Vector;
Vector::value_type arr[] = { 0, 1, 2, 2, 3, 4, 2, 2, 2, 6, 7 };
// Set up a vector.
Vector v1 (arr, arr + sizeof arr / sizeof *arr);
// Try equal_range variants.
std::pair<Vector::iterator, Vector::iterator> p1 =
std::equal_range (v1.begin (), v1.end (), 3);
std::pair<Vector::iterator, Vector::iterator> p2 =
std::equal_range (v1.begin (), v1.end (), 2,
std::less<Vector::value_type> ());
// Output results.
std::cout << "\nThe equal range for 3 is: "
<< "(" << *p1.first << " , "
<< *p1.second << ")" << std::endl;
std::cout << "\nThe equal range for 2 is: "
<< "(" << *p2.first << " , "
<< *p2.second << ")" << std::endl;
return 0;
}

View File

@@ -0,0 +1,72 @@
/**************************************************************************
*
* failure.cpp - Example program demonstrating ios::failure.
*
* $Id: failure.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream>
#include <stdexcept>
#include <examples.h>
#ifndef _RWSTD_NO_EXCEPTIONS
int main ()
{
try {
// Enable exceptions in cin.
std::cin.exceptions (std::ios::eofbit);
// Clear all bits and set eofbit.
std::cin.clear (std::ios::eofbit);
}
catch (const std::ios::failure &e) {
std::cout << "Caught an exception: " << e.what () << std::endl;
}
catch (const std::exception &e) {
std::cout << "Caught an exception: " << e.what () << std::endl;
return 1; // Indicate failure.
}
catch (...) {
std::cout << "Caught an unknown exception" << std::endl;
return 1; // Indicate failure.
}
return 0;
}
#else
int main ()
{
std::cout << "Exceptions not supported." << std::endl;
return 0;
}
#endif // _RWSTD_NO_EXCEPTIONS

View File

@@ -0,0 +1,89 @@
/***************************************************************************
*
* filebuf.cpp - basic_filebuf example
*
* $Id: filebuf.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <fstream> // for filebuf, ifstream, istream
#include <iostream> // for cout, endl
#include <cstdio> // for tmpnam(), remove()
#include <examples.h>
int main ()
{
#ifndef _RWSTD_NO_EXT_FILEBUF
// use an extension of this implementation: NULL file name argument
// creates a temporary file; closing the file stream object or its
// associated filebuf removes the temporary file
const char* const fname = 0;
#else // if defined (_RWSTD_NO_EXT_FILEBUF)
char fnamebuf [L_tmpnam];
// create a temporary filename
const char* const fname = std::tmpnam (fnamebuf);
if (!fname)
return 1;
#endif // _RWSTD_NO_EXT_FILEBUF
// create a filebuf object for reading and writing of a temporary file
std::filebuf outbuf;
outbuf.open (fname, std::ios::in | std::ios::out | std::ios::trunc);
// set the internal character buffer size
// buffer will be allocated internally and deallocated in object dtor
outbuf.pubsetbuf (0, 256);
// associate the filebuf object with an iostream object
std::iostream out (&outbuf);
// open this source code file and output it to the temporary file
out << std::ifstream (__FILE__).rdbuf ();
// seek to the beginning of the stream
out.seekp (0);
// output the contents of the `out' object to standard output
std::cout << out.rdbuf ();
// close the filebuf object before removing the underlying file
outbuf.close ();
if (fname) {
// remove the temporary file if it has a name
// otherwise, if fname is NULL, the temporary file will
// have already been automatically removed by the call
// to close() above
std::remove (fname);
}
return 0;
}

View File

@@ -0,0 +1,79 @@
/**************************************************************************
*
* fill.cpp - Example program for initializing a range with a given value.
*
* $Id: fill.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for copy(), fill()
#include <iostream> // for cout
#include <iterator> // for ostream_iterator
#include <vector> // for vector
#include <examples.h>
int main ()
{
// Typedef for convenience.
typedef std::vector<int, std::allocator<int> > Vector;
const Vector::value_type arr[] = { 1, 2, 3, 4 };
// Set up two vectors.
Vector v1 (arr, arr + sizeof arr / sizeof *arr);
Vector v2 (v1.begin (), v1.end ());
// Set up one empty vector.
Vector v3;
// Fill all of v1 with 9.
std::fill (v1.begin (), v1.end (), 9);
// Fill first 3 of v2 with 7.
std::fill_n (v2.begin (), 3, 7);
// Use insert iterator to fill v3 with 5 11's.
std::fill_n (std::back_inserter (v3), 5, 11);
// Copy all three to cout.
std::ostream_iterator<Vector::value_type,
char,
std::char_traits<char> > out (std::cout, " ");
std::copy (v1.begin (), v1.end (), out);
std::cout << std::endl;
std::copy (v2.begin (), v2.end (), out);
std::cout << std::endl;
std::copy (v3.begin (), v3.end (), out);
std::cout << std::endl;
// Fill cout with 3 5's.
std::fill_n (out, 3, 5);
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,70 @@
/**************************************************************************
*
* find.cpp - Example program for finding an occurence of value in a
* sequence.
*
* $Id: find.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <vector> // for vector
#include <algorithm> // for adjacent_find, find
#include <functional> // for bind1st, equal_to
#include <iostream> // for cout, endl
#include <examples.h>
int main ()
{
// Typedef for convenience.
typedef std::vector<int, std::allocator<int> > Vector;
const Vector::value_type arr[] = { 0, 1, 2, 2, 3, 4, 2, 2, 6, 7 };
// Set up a vector.
const Vector v1 (arr, arr + sizeof arr / sizeof *arr);
// Try find.
Vector::const_iterator it = std::find (v1.begin (), v1.end (), 3);
std::cout << *it << ' ';
// Try find_if.
it = std::find_if (v1.begin (), v1.end (),
std::bind1st (std::equal_to<Vector::value_type>(), 3));
std::cout << *it << ' ';
// Try both adjacent_find variants.
it = std::adjacent_find (v1.begin (), v1.end ());
std::cout << *it << ' ';
it = std::adjacent_find (v1.begin (), v1.end (),
std::equal_to<Vector::value_type>());
std::cout << *it << std::endl;
return 0;
}

View File

@@ -0,0 +1,93 @@
/**************************************************************************
*
* find_end.cpp - Example program for finding a subsequence.
*
* $Id: find_end.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for find_first_of(), find_end()
#include <functional> // for equal_to
#include <iostream> // for cout, endl
#include <iterator> // for ostream_iterator
#include <list> // for list
#include <examples.h>
int main ()
{
typedef std::list<int, std::allocator<int> > List;
const List::value_type a1[] = { 0, 1, 6, 5, 3, 2, 2, 6, 5, 7 };
const List::value_type a2[] = { 6, 5, 0, 0 };
// Set up two sequences.
const List l1 (a1, a1 + sizeof a1 / sizeof *a1);
const List l2 (a2, a2 + 2);
// Try both find_first_of variants.
List::const_iterator it1 =
std::find_first_of (l1.begin (), l1.end (), l2.begin (), l2.end ());
List::const_iterator it2 =
std::find_first_of (l1.begin (), l1.end (), l2.begin (), l2.end (),
std::equal_to<List::value_type>());
// Try both find_end variants.
List::const_iterator it3 =
std::find_end (l1.begin (), l1.end (), l2.begin (), l2.end ());
List::const_iterator it4 =
std::find_end (l1.begin (), l1.end (), l2.begin (), l2.end (),
std::equal_to<List::value_type>());
// Output results of find_first_of.
// Iterator now points to the first element that matches
// one of a set of values.
std::ostream_iterator<List::value_type,
char,
std::char_traits<char> > out (std::cout, " " );
if (it3 == it4 && it1 == it2) {
std::cout << "For the sequences { ";
std::copy (l1.begin (), l1.end (), out);
std::cout << "} and { ";
std::copy (l2.begin (), l2.end (), out);
std::cout << "} \nboth versions of find_first_of point to: "
<< *it1 << std::endl;
// Output results of find_end.
// Iterator now points to the first element of the last
// find subsequence.
std::cout << "both versions of find_end point to: "
<< *it3 << std::endl;
}
return 0;
}

View File

@@ -0,0 +1,74 @@
/**************************************************************************
*
* find_f_o.cpp - Example program for finding a subsequence.
*
* $Id: find_first_of.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for find_first_of()
#include <functional> // for equal_to
#include <iostream> // for cout, endl
#include <iterator> // for ostream_iterator
#include <vector> // for vector
#include <examples.h>
int main ()
{
// Typedef for convenience.
typedef std::vector<int, std::allocator<int> > Vector;
const Vector::value_type a1[] = { 0, 1, 2, 2, 3, 4, 2, 2, 6, 7 };
const Vector::value_type a2[] = { 6, 4 };
// Set up two vectors.
const Vector v1 (a1, a1 + sizeof a1 / sizeof *a1);
const Vector v2 (a2, a2 + sizeof a2 / sizeof *a2);
// Try both find_first_of variants.
Vector::const_iterator it1 =
std::find_first_of (v1.begin (), v1.end (), v2.begin (), v2.end ());
Vector::const_iterator it2 =
std::find_first_of (v1.begin (), v1.end (), v2.begin (), v2.end (),
std::equal_to<Vector::value_type>());
// Create an output stream iterator.
std::ostream_iterator<Vector::value_type,
char,
std::char_traits<char> > out (std::cout, " " );
// Output results.
std::cout << "For the vectors { ";
std::copy (v1.begin (), v1.end (), out);
std:: cout << "} and { ";
std::copy (v2.begin (), v2.end (), out);
std::cout << "}\nboth versions of find_first_of point to: "
<< *it1 << std::endl;
// Return 0 on success, non-0 on failure.
return !(*it1 == *it2);
}

View File

@@ -0,0 +1,123 @@
/**************************************************************************
*
* fmtflags_manip.cpp - Example program demonstrating the implementation
* of a user-defined manipulator for convenient and exception-safe setting
* and restoring of stream formatting flags.
*
* $Id: fmtflags_manip.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
**************************************************************************/
#include <ios> // for hex
#include <iomanip> // for __rw_smanip
#include <iostream> // for cout
#include <examples.h>
// implementation class of the fmtflags manipulator that saves
// a stream object's fmtflags state before optionally setting
// their new value and restores the saved state when destroyed
// uses ios::basefield as an invalid flags value (it's a valid
// mask but not a valid flags value)
class fmtflags_manip
{
std::ios_base *strm_;
std::ios_base::fmtflags saved_;
public:
fmtflags_manip (): strm_ (), saved_ (std::ios_base::basefield) { }
void operator() (std::ios_base &strm,
std::ios_base::fmtflags flags) const {
const_cast<fmtflags_manip*>(this)->strm_ = &strm;
const_cast<fmtflags_manip*>(this)->saved_ = strm.flags ();
if (flags != std::ios_base::basefield)
strm.flags (flags);
}
~fmtflags_manip () {
if (strm_)
strm_->flags (saved_);
}
};
// saveflags manipulator to temporarily set formatting flags and
// restore their initial value at the end of an insertion statement
inline std::__rw_smanip<fmtflags_manip, std::ios_base::fmtflags>
saveflags (std::ios_base::fmtflags flags = std::ios_base::basefield)
{
typedef std::__rw_smanip<fmtflags_manip, std::ios_base::fmtflags> Manip;
return Manip (flags);
}
// dummy class whose insertion operator always throws
struct BadClass { };
inline std::ostream&
operator<< (std::ostream &strm, BadClass&)
{
// inserter that always throws an exception
return throw "exception", strm;
}
int main ()
{
try {
BadClass throw_exception;
// use the saveflags manipulator to set the formatting flags
// for the rest of this insertion statement only and have it
// restore the previous flags at the end of the statement
// or on exception
std::cout << saveflags (std::cout.hex)
<< std::showbase
<< "hex 1234: " << 0x1234 << '\n'
<< "hex 2345: " << 0x2345 << '\n'
<< throw_exception // inserting will throw
<< "never output!"; // this string is never output
}
catch (...) {
// show that default formatting flags (decimal output and no
// showbase) have been restored during stack unwinding above
std::cout << "dec 3456: " << 3456 << '\n'
<< std::oct
<< "oct 4567: " << 04567 << '\n';
}
// use ordinary manipulators to set hex output with base prefix
std::cout << std::hex << std::showbase
<< "hex 5678: " << 0x5678 << '\n'
<< "hex 6789: " << 0x6789 << '\n';
// the same flags set above are still in effect
std::cout << "hex 789a: " << 0x789a << '\n';
return 0;
}

View File

@@ -0,0 +1,69 @@
/**************************************************************************
*
* for_each.cpp - Example program for applying a function to each element
* in a range.
*
* $Id: for_each.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for for_each
#include <functional> // for less, unary_function
#include <iostream> // for cout, endl
#include <set> // for set
#include <examples.h>
// Function object that outputs its argument times x.
template <class Arg>
class out_times_x: public std::unary_function<Arg, void>
{
Arg multiplier;
public:
out_times_x (const Arg &x) : multiplier (x) { }
void operator() (const Arg &x) const {
std::cout << x * multiplier << " " << std::endl;
}
};
int main ()
{
// Typedef for convenience.
typedef std::set<int, std::less<int>, std::allocator<int> > sequence;
sequence::value_type arr [] = { 1, 2, 3, 4, 5 };
// Populate a sequence from arr.
sequence s (arr, arr + sizeof arr / sizeof *arr);
// Construct a function object.
out_times_x<sequence::value_type> f2 (2);
// Apply function object's operator() to each element.
std::for_each (s.begin (), s.end (), f2);
return 0;
}

View File

@@ -0,0 +1,86 @@
/***************************************************************************
*
* fstream.cpp - fstream example.
*
* $Id: fstream.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <fstream> // for fstream
#include <iostream> // for cout
#include <string> // for string
#include <examples.h>
int main ( )
{
// create a bi-directional fstream object
std::fstream inout ("test_fstream.out",
std::ios::in | std::ios::out | std::ios::trunc);
// write out three lines to the file
inout << "Dieses ist die Geschichte eines Mannes.\n"
<< "C'est l'histoire d'un homme.\n"
<< "This is the story of a man.\n";
std::string line;
// seek to the beginning of the file
inout.seekg (0);
// extract the first line
std::getline (inout, line);
// output the first line to standard output
std::cout << "\nDeutsch:\n" << line << '\n';
// obtain current position in file
const std::fstream::pos_type pos = inout.tellg ();
// extract the second line
std::getline (inout, line);
// output the second line to standard output
std::cout << "Francais:\n" << line << '\n';
// extract the third line
std::getline (inout, line);
// output the third line to standard output
std::cout << "English:\n" << line << '\n';
// move the put sequence before the second line
inout.seekp (pos);
// replace the second and third lines
inout << "This is the story of a man.\n"
<< "C'est l'histoire d'un homme.";
// seek to the beginning of the file
inout.seekg (0);
// output the entire contents of the fstream object's buffer to stdout
std::cout << "\n" << inout.rdbuf () << '\n';
return 0;
}

View File

@@ -0,0 +1,89 @@
/**************************************************************************
*
* funct_ob.cpp - Example program for function objects.
*
* $Id: funct_ob.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
// Create a new function object from unary_function.
template <class Arg, class Result>
struct factorial: public std::unary_function<Arg, Result>
{
Result operator() (const Arg &arg) {
Result a = 1;
for (Arg i = 2; i <= arg; i++)
a *= i;
return a;
}
};
int main ()
{
// Typedefs for convenience.
typedef std::deque<int, std::allocator<int> > Deque;
typedef std::vector<long, std::allocator<long> > Vector;
// Initialize a deque with an array of integers.
Deque::value_type arr[] = { 1, 2, 3, 4, 5, 6, 7 };
Deque d (arr, arr + sizeof arr / sizeof *arr);
// Create an empty vector to store the factorials.
Vector v;
// Transform the numbers in the deque to their factorials
// and store in the vector.
std::transform (d.begin (), d.end (),
std::back_inserter (v),
factorial<Deque::value_type, Vector::value_type>());
// Create an iterator to output deque elements.
std::ostream_iterator<Deque::value_type,
char,
std::char_traits<char> > outd (std::cout, " ");
// Print the results.
std::cout << "The following numbers: \n ";
std::copy (d.begin (), d.end (), outd);
// Create an iterator to output vector elements.
std::ostream_iterator<Vector::value_type,
char,
std::char_traits<char> > outv (std::cout, " ");
std::cout << std::endl;
std::cout << "\nHave the factorials: \n ";
std::copy (v.begin (), v.end (), outv);
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,96 @@
/**************************************************************************
*
* generate.cpp - Example program for initializing a container with values
* produced by a value-generator class.
*
* $Id: generate.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for generate, generate_n
#include <iostream> // for cout, endl
#include <iterator> // for ostream_iterator
#include <vector> // for vector
#include <examples.h>
// Value generator simply doubles the current value and returns it.
template <class T>
class generator
{
T val_;
public:
generator (const T &val) : val_ (val) { }
T operator() () {
return val_ += val_;
}
};
int main ()
{
// Typedef for convenience.
typedef std::vector<short, std::allocator<short> > Vector;
Vector::value_type arr[4] = { 1, 2, 3, 4 };
// Set up two vectors.
Vector v1 (arr, arr + sizeof arr / sizeof *arr);
Vector v2 = v1;
// Set up one empty vector.
Vector v3;
// Create a generator function object.
generator<Vector::value_type> gen (1);
// Generate values for all of v1.
std::generate (v1.begin (), v1.end (), gen);
// Generate values for first 3 of v2.
std::generate_n (v2.begin (), 3, gen);
// Use back_insert_iterator to generate 5 values for v3.
std::generate_n (std::back_inserter (v3), 5, gen);
// Copy all three to cout.
std::ostream_iterator<Vector::value_type,
char,
std::char_traits<char> > out (std::cout, " ");
std::copy (v1.begin (), v1.end (), out);
std::cout << std::endl;
std::copy (v2.begin (), v2.end (), out);
std::cout << std::endl;
std::copy (v3.begin (), v3.end (), out);
std::cout << std::endl;
// Generate 3 values into cout.
std::generate_n (out, 3, gen);
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,91 @@
/**************************************************************************
*
* gslice.cpp - Generalized slice example program.
*
* $Id: gslice.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // fo cout, endl
#include <valarray.h> // Includes valarray and provides stream inserter.
#include <examples.h> // Generic header for all examples.
typedef std::valarray<int> valarray_t;
typedef std::valarray<std::size_t> selector_t;
int main(void) {
// Create a valarray of ints.
valarray_t::value_type ibuf[27] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26 };
valarray_t vi(ibuf, 27);
// Create length and stride valarray
selector_t::value_type length_buf[3] = {3,3,3};
selector_t::value_type stride_buf[3] = {9,3,1};
selector_t length_val(length_buf, 3);
selector_t stride_val(stride_buf, 3);
// Print out the valarray<int>.
std::cout << "original valarray vi\n\n" << vi << "\n\n";
// Print out all three dimensions (the entire valarray).
std::cout << "vi[gslice(0,{3,3,3},{9,3,1})]\n\n"
<< vi[std::gslice(0,length_val,stride_val)] << "\n\n";
// Print a two dimensional slice out of the middle.
selector_t::value_type length_buf2[] = {3, 3};
selector_t::value_type stride_buf2[] = {3, 1};
selector_t length_val2(length_buf2, 2);
selector_t stride_val2(stride_buf2, 2);
std::cout << "vi[gslice(9,{3,3},{3,1})]\n\n"
<< vi[std::gslice(9,length_val2,stride_val2)] << "\n\n";
// Print another two dimensional slice out of the middle but
// orthoganal to one we just did.
stride_val2[0] = 9;
stride_val2[1] = 1;
std::cout << "vi[gslice(3,{3,3},{9,1})]\n\n"
<< vi[std::gslice(3,length_val2,stride_val2)] << "\n\n";
// Print out the last plane in the middle -- orthoganal to both
// of the previous ones.
stride_val2[0] = 3;
stride_val2[1] = 9;
std::cout << "vi[gslice(1,{3,3},{3,9})]\n\n"
<< vi[std::gslice(1,length_val2,stride_val2)] << "\n\n";
// Now how about a diagonal slice, upper left front to lower right
// back.
stride_val2[0] = 3;
stride_val2[1] = 10;
std::cout << "vi[gslice(0,{3,3},{3,10})]\n\n"
<< vi[std::gslice(0,length_val2,stride_val2)] << "\n\n";
return 0;
}

View File

@@ -0,0 +1,72 @@
/**************************************************************************
*
* gslice_array.cpp -- Generalized array slice examples
*
* $Id: gslice_array.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout, endl
#include <valarray.h> // Includes valarray and provides stream inserter.
#include <examples.h> // Generic header for all examples.
typedef std::valarray<int> valarray_t;
int main(void) {
valarray_t::value_type
ibuf[22] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 };
// Create a valarray of ints
valarray_t vi (ibuf,(sizeof ibuf / sizeof *ibuf));
// Print out the valarray
std::cout << "original valarray vi:\n\n" << vi << "\n\n";
// Get a two dimensional diagonal slice out of the middle
std::size_t length_ary[] = {4, 2};
std::size_t stride_ary[] = {4, 6};
std::valarray<std::size_t> length_val(length_ary, 2);
std::valarray<std::size_t> stride_val(stride_ary, 2);
// Print out the slices starting at positions 0 and 2 and respectively.
std::cout << "vi[gslice(0,[4,2],[4,6])]\n\n"
<< vi[std::gslice(0,length_val,stride_val)] << "\n\n";
std::cout << "vi[gslice(2,[4,2],[4,6])]\n\n"
<< vi[std::gslice(2,length_val,stride_val)] << "\n\n";
// Multiply the first slice by the second.
vi[std::gslice(0,length_val,stride_val)]
*= static_cast<valarray_t > (vi[std::gslice(2,length_val,stride_val)]);
std::cout << "vi[gslice(0,[4,2],[4,8])] *= vi[gslice(2,[4,2],[4,6])]\n\n"
<< vi << std::endl;
return 0;
}

View File

@@ -0,0 +1,63 @@
/**************************************************************************
*
* hasfacet.cpp - Example program of the hasfacet function template.
*
* $Id: has_facet.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <ios> // for boolalpha
#include <iostream> // for cout
#include <locale> // for ctype, has_facet(), locale
#include <examples.h>
// dummy facet, must derive from locale::facet
// and define a static member of type locale::id
struct MyFacet: public std::locale::facet
{
static std::locale::id id;
};
std::locale::id MyFacet::id;
int main ()
{
// create a copy of the locale imbued in cout
const std::locale loc (std::cout.getloc ());
std::cout << std::boolalpha
<< "std::has_facet<std::ctype<char> >(std::cout.getloc ()) = ";
// see if ctype<char> is installed in the locale (expect true)
std::cout << std::has_facet<std::ctype<char> >(loc) << '\n';
std::cout << "std::has_facet<MyFacet>(std::cout.getloc ()) = ";
// see if MyFacet is installed in the locale (expect false)
std::cout << std::has_facet<MyFacet>(loc) << '\n';
return 0;
}

View File

@@ -0,0 +1,97 @@
/**************************************************************************
*
* heap_ops.cpp - Example program for heap operations.
*
* $Id: heap_ops.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for copy, make_heap, pop_heap, push_heap
#include <functional> // for less
#include <iostream> // for cout
#include <iterator> // for ostream_iterator
#include <vector> // for vector
#include <examples.h>
template <class charT, class Traits, class T, class Allocator>
void print_vector (std::basic_ostream<charT, Traits> &strm,
const std::vector<T, Allocator> &v)
{
std::copy (v.begin (), v.end (),
std::ostream_iterator<T, charT, Traits> (strm, " "));
strm << std::endl;
}
int main ()
{
typedef std::vector<int, std::allocator<int> > Vector;
const Vector::value_type d1[] = { 1, 2, 3, 4 };
const Vector::value_type d2[] = { 1, 3, 2, 4 };
// Set up two vectors.
Vector v1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
Vector v2 (d2 + 0, d2 + sizeof d2 / sizeof *d2);
// Make heaps.
std::make_heap (v1.begin (), v1.end ());
std::make_heap (v2.begin (), v2.end (), std::less<int>());
// v1 = (4, x, y, z) and v2 = (4, x, y, z)
// Note that x, y and z represent the remaining values in the
// container (other than 4). The definition of the heap and heap
// operations does not require any particular ordering
// of these values.
// Copy both vectors to cout.
print_vector (std::cout, v1);
print_vector (std::cout, v2);
// Now let's pop.
std::pop_heap (v1.begin (), v1.end ());
std::pop_heap (v2.begin (), v2.end (), std::less<int>());
print_vector (std::cout, v1);
print_vector (std::cout, v2);
// And push.
std::push_heap (v1.begin (), v1.end ());
std::push_heap (v2.begin (), v2.end (), std::less<int>());
print_vector (std::cout, v1);
print_vector (std::cout, v2);
// Now sort those heaps.
std::sort_heap (v1.begin (), v1.end ());
std::sort_heap (v2.begin (), v2.end (), std::less<int>());
print_vector (std::cout, v1);
print_vector (std::cout, v2);
return 0;
}

View File

@@ -0,0 +1,143 @@
/***************************************************************************
*
* ifstream.cpp - basic_ifstream example.
*
* $Id: ifstream.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <fstream> // for fstream
#include <iomanip> // for setw
#include <ios> // for dec, hex, oct, right, showbase
#include <iostream> // for cerr, cout
#include <locale> // for isspace()
#include <examples.h>
int main ( )
{
const char ntbs[] = "Le minot passait la piece a frotter.";
try {
// create a read/write file-stream object
std::ifstream in ("ifstream.results.out",
std::ios::in | std::ios::out | std::ios::trunc);
if (!in.is_open ())
throw (std::ios::failure ("open error"));
// associate the ostream object's buffer with the ifstream object
std::ostream out (in.rdbuf ());
// output ntbs in out
out << ntbs << '\n';
// seek to the beginning of the file
in.seekg (0);
char c;
// output each space-separated word on a separate line
while (in.get (c)) {
if ((std::isspace)(c, in.getloc ()))
std::cout << '\n';
else
std::cout << c;
}
std::cout << '\n';
// clear stream state, failbit | eofbit set by last call to get ()
in.clear ();
// move back to the beginning of the file
in.seekg (0);
char buf [40];
// guard against buffer overflow
in.width (sizeof buf);
// set right justification
std::cout << std::right;
// does the same thing as the previous code
// output each word on a separate line in a field of width 10
while (in >> buf) {
std::cout.width (10);
std::cout << buf << '\n';
}
std::cout << '\n';
// clear the stream state
in.clear ();
// output the base info before each integer
out << std::showbase;
std::ostream::pos_type pos= out.tellp ();
const long x = 10;
// output x in hex with a field with of 10
out << std::hex << std::setw (10) << x << '\n';
// output x in oct with a field with of 10
out << std::oct << std::setw (10) << x << '\n';
// output x in dec with a field with of 10
out << std::dec << std::setw (10) << x << '\n';
// move back to the beginning of the file
in.seekg (0);
// output the entire file
std::cout << in.rdbuf () << '\n';
// clear the stream state
in.clear ();
// seek the input sequence to pos
in.seekg (pos);
long a = 0;
long b = 0;
long d = 0;
in.unsetf (std::ios::basefield);
// read the previous outputted integer
in >> a >> b >> d;
// output 3 times 10
std::cout << a << '\n' << b << '\n' << d << '\n';
}
catch (std::ios::failure e) {
std::cerr << e.what () << '\n';
}
return 0;
}

View File

@@ -0,0 +1 @@
100

View File

@@ -0,0 +1 @@
1 1 2 3 5 8 13 21 45 66 111 177 eof

View File

@@ -0,0 +1 @@
amanaplanacanalpanama

View File

@@ -0,0 +1 @@
Mon Jun 4 08:25:14 2007

View File

@@ -0,0 +1,74 @@
/**************************************************************************
*
* includes.cpp - Example program of basic set operation for sorted
* sequences.
*
* $Id: includes.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for includes
#include <iostream> // for cout, endl
#include <iterator> // for ostream_iterator
#include <set> // for set
#include <examples.h>
int main ()
{
// Typedefs for convenience.
typedef std::set<int, std::less<int>, std::allocator<int> > Set;
typedef std::ostream_iterator<int, char, std::char_traits<char> > os_iter;
int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int a2[] = { 2, 4, 6, 8, 10, 12 };
int a3[] = { 3, 5, 7, 8 };
// Initialize three sets.
Set all (a1, a1 + sizeof a1 / sizeof *a1);
Set even (a2, a2 + sizeof a2 / sizeof *a2);
Set other (a3, a3 + sizeof a3 / sizeof *a3);
// Demonstrate includes.
std::cout << "The set: ";
std::copy (all.begin (), all.end (), os_iter (std::cout, " "));
bool answer = std::includes (all.begin (), all.end (),
other.begin (), other.end ());
std::cout << std::endl << (answer ? "INCLUDES " : "DOES NOT INCLUDE ");
std::copy (other.begin (), other.end (), os_iter (std::cout, " "));
answer = std::includes (all.begin (), all.end (),
even.begin (), even.end ());
std::cout << ", and" << std::endl
<< (answer ? "INCLUDES" : "DOES NOT INCLUDE ");
std::copy (even.begin (), even.end (), os_iter (std::cout, " "));
std::cout << std::endl << std::endl;
return 0;
}

View File

@@ -0,0 +1,60 @@
/**************************************************************************
*
* indirect_array.cpp -- Indirect array examples
*
* $Id: indirect_array.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout, endl
#include <valarray.h> // Includes valarray and provides stream inserter.
#include <examples.h> // Generic header for all examples.
typedef std::valarray<int> valarray_t;
typedef std::valarray<std::size_t> selector_t;
int main(void) {
// Create a valarray of integers.
valarray_t::value_type vbuf[10] = {0,1,2,3,4,5,6,7,8,9};
valarray_t vi(vbuf, (sizeof vbuf / sizeof *vbuf));
// Create a valarray of indices for a selector.
selector_t::value_type sbuf[6] = {0,2,3,4,7,8};
selector_t selector(sbuf, (sizeof sbuf / sizeof *sbuf));
// Print out the valarray<int>.
std::cout << "original valarray vi\n\n" << vi << "\n\n";
// Print out the selective array.
std::cout << "vi[0,2,3,4,7,8]\n\n" << vi[selector] << "\n\n";
// Double the selected values.
vi[selector] += vi[selector];
// Print out the modified valarray.
std::cout << "vi[0,2,3,4,7,8] += vi[0,2,3,4,7,8]\n\n" << vi << std::endl;
return 0;
}

View File

@@ -0,0 +1,75 @@
/**************************************************************************
*
* inr_prod.cpp - Example program computes the inner product A X B of two
* ranges A and B.
*
* $Id: inner_product.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for copy()
#include <functional> // for plus, minus
#include <iostream> // for cout, endl
#include <iterator> // for ostream_iterator
#include <list> // for list
#include <numeric> // for inner_product()
#include <vector> // for vector
#include <examples.h>
int main ()
{
typedef std::list<int, std::allocator<int> > List;
typedef std::vector<int, std::allocator<int> > Vector;
typedef std::ostream_iterator<int, char, std::char_traits<char> > os_iter;
// Initialize a list and a vector using arrays of ints.
int a1[] = { 6, -3, -2 };
int a2[] = { -2, -3, -2 };
List l (a1, a1 + sizeof a1 / sizeof *a1);
Vector v (a2, a2 + sizeof a2 / sizeof *a2);
// Calculate the inner product of the two sets of values.
List::value_type prod = std::inner_product (l.begin (), l.end (),
v.begin (), 0);
// Calculate a wacky inner product using the same values.
List::value_type wacky =
std::inner_product (l.begin (), l.end (), v.begin (), 0,
std::plus<List::value_type>(),
std::minus<List::value_type>());
// Print the output.
std::cout << "For the sets of numbers: { ";
std::copy (v.begin (), v.end (), os_iter (std::cout, " "));
std::cout << "} and { ";
std::copy (l.begin (), l.end (), os_iter (std::cout, " "));
std::cout << "}\nThe inner product is: " << prod
<< "\nThe wacky result is: " << wacky
<< std::endl;
return 0;
}

View File

@@ -0,0 +1,80 @@
/**************************************************************************
*
* ins_itr.cpp - Example program of insert iterator.
*
* $Id: insert_iterator.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for copy
#include <iostream> // for cout, endl
#include <iterator> // for ostream_iterator, xxx_inserter
#include <deque> // for deque
#include <examples.h>
int main ()
{
// Typedefs for convenience.
typedef std::deque<int, std::allocator<int> > Deque;
typedef std::ostream_iterator<int, char, std::char_traits<char> > os_iter;
// Initialize a deque using an array.
Deque::value_type arr[] = { 3, 4, 7, 8 };
Deque d (arr, arr + sizeof arr / sizeof *arr);
// Output the original deque.
std::cout << "Start with a deque: \n ";
std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
// Insert into the middle.
std::insert_iterator<Deque> ins (d, d.begin () + 2);
*ins = 5;
*ins = 6;
// Output the new deque.
std::cout << "\n\nUse an insert_iterator: \n ";
std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
// A deque of four 1s.
Deque d2 (4, 1);
// Insert d2 at front of d.
std::copy (d2.begin (), d2.end (), std::front_inserter (d));
// Output the new deque.
std::cout << "\n\nUse a front_inserter: \n ";
std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
// Insert d2 at back of d.
std::copy (d2.begin (), d2.end (), std::back_inserter (d));
// Output the new deque.
std::cout << "\n\nUse a back_inserter: \n ";
std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,214 @@
/**************************************************************************
*
* insert_wchar.cpp
*
* Example program demonstrating an implementation of an inserter
* operator overloaded for arrays of wchar_t that performs codeset
* conversion from wchar_t to mutlibyte characters.
*
* $Id: insert_wchar.cpp 590060 2007-10-30 13:12:23Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
**************************************************************************/
#include <cassert> // for assert()
#include <cwchar> // for mbstate_t, size_t
#include <ios> // for hex
#include <iostream> // for cout
#include <locale> // for codecvt, isalnum(), locale
#include <ostream> // for basic_ostream
#include <sstream> // for ostringstream
// inserts a wide character string into a stream buffer performing
// codeset conversion if necessary
template <class charT, class Traits>
void
streambuf_insert (std::basic_ostream<charT, Traits> &strm,
const wchar_t *s)
{
typedef typename Traits::state_type StateT;
typedef std::codecvt<wchar_t, charT, StateT> Codecvt;
const Codecvt &cvt = std::use_facet<Codecvt>(strm.getloc ());
const std::size_t slen = std::char_traits<wchar_t>::length (s);
// perform codeset conversion in chunks to avoid dynamic
// memory allocation
const std::size_t xbufsize = 32;
charT xbuf [xbufsize];
charT* xbuf_end = xbuf + xbufsize;
charT* to_next = 0;
const wchar_t* from_next = 0;
const wchar_t* const end = s + slen;
StateT state = StateT ();
for (const wchar_t* base = s; from_next != end; base = from_next) {
const std::codecvt_base::result res =
cvt.out (state, base, end, from_next,
xbuf, xbuf_end, to_next);
std::streamsize nbytes = to_next - xbuf;
switch (res) {
case Codecvt::error:
// write out the sequence successfully converted up
// to the point of the error in the internal sequence
// and fail
strm.rdbuf ()->sputn (xbuf, nbytes);
strm.setstate (strm.badbit);
case Codecvt::noconv:
// write the entire sequence
if (nbytes != strm.rdbuf ()->sputn (xbuf, nbytes)) {
strm.setstate (strm.badbit);
return;
}
from_next = end; // effectively break
break;
default:
assert (cvt.ok == res || cvt.partial == res);
// partial conversion will result if there isn't enough
// space in the conversion buffer to hold the converted
// sequence, but we're O.K. since we'll be passing any
// remaining unconverted characters (starting at
// from_next) in the next iteration
nbytes = to_next - xbuf;
if (nbytes != strm.rdbuf ()->sputn (xbuf, nbytes)) {
strm.setstate (strm.badbit);
return;
}
}
}
}
// stream insertion operator overloaded for arrays of wchar_t characters
template <class charT, class Traits>
std::basic_ostream<charT, Traits>&
operator<< (std::basic_ostream<charT, Traits> &strm,
const wchar_t *s)
{
const typename std::basic_ostream<charT, Traits>::sentry opfx (strm);
if (opfx) {
try {
// try to insert character array into stream buffer
streambuf_insert (strm, s);
}
catch (...) {
bool threw;
try {
// set badbit on exception without throwing ios::failure
strm.setstate (strm.badbit);
threw = false;
}
catch (std::ios_base::failure&) {
// make a note of the exception thrown from setstate()...
threw = true;
}
if (threw) {
// ...and rethrow the original exception
throw;
}
}
}
return strm;
}
// examples of wide character strings
static const wchar_t* const wcs [] = {
L"a", L"abc",
// Greek letter Alpha:
L"\x0391", // "\xce\x91"
// Greek letters Alpha Beta:
L"\x0391\x0392", // "\xce\x91\xce\x91\xce\x92"
// Greek letters Alpha Beta Gamma:
L"\x0391\x0392\x0393", // "\xce\x91\xce\x92\xce\x93"
// Tibetan digit zero:
L"\x0f20", // "\xe0\xbc\xa0"
// Tibetan digits one, zero:
L"\x0f21\x0f20", // "\xe0\xbc\xa1\xe0\xbc\xa0"
// Tibetan digits two, one, zero:
L"\x0f22\x0f21\x0f20" // "\xe0\xbc\xa2\xe0\xbc\xa1\xe0\xbc\xa0"
};
int main ()
{
typedef std::codecvt_byname<wchar_t, char, std::mbstate_t> Codecvt;
// create a UCS/UTF-8 codecvt facet and install it in a locale
const std::locale utf (std::cout.getloc (), new Codecvt ("UTF-8@UCS"));
for (std::size_t i = 0; i != sizeof wcs / sizeof *wcs; ++i) {
std::ostringstream strm;
// imbue the UTF-8/UCS capable locale in a stringstream
strm.imbue (utf);
// insert each wide character string into the narrow stream
// object relying on the inserter to convert each wide string
// into the corresponding multibyte character string
strm << wcs [i];
// write out the wide character string in Unicode notation
std::cout << "UCS-2: " << std::hex;
for (const wchar_t *pwc = wcs [i]; *pwc != L'\0'; ++pwc)
std::cout << "U+" << unsigned (*pwc) << ' ';
const std::string str = strm.str ();
std::cout << " ==> UTF-8: \"";
typedef unsigned char UChar;
// write out the the multibyte character sequence using
// ordinary aphanumeric symbols or hex escape sequences
// where necessary
for (const char *pc = str.c_str (); *pc != '\0'; ++pc) {
// parenthesize isalnum to prevent macro expension
// in case the function happens to be (illegally)
// shadowed by a macro
if ((std::isalnum)(*pc, std::cout.getloc ()))
std::cout << *pc;
else
std::cout << "\\x" << int (UChar (*pc));
}
std::cout << "\"\n";
}
}

View File

@@ -0,0 +1,46 @@
/**************************************************************************
*
* isalnum.cpp - Example program of isalnum convience function.
*
* $Id: isalnum.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout, endl
#include <locale> // for isalnum
#include <examples.h>
int main ()
{
std::cout << "Alphanumeric ASCII characters: ";
// iterate
for (char c = '\0'; c != '\177'; ++c)
if ((std::isalnum)(c, std::cout.getloc ()))
std::cout << c << ' ';
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,150 @@
/***************************************************************************
*
* istream1.cpp - istream example
*
* $Id: istream1.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <fstream> // for basic_ofstream
#include <iostream> // for basic_istream, cout
#include <cstdio> // for tmpnam () and remove ()
#include <examples.h>
int main ()
{
#ifndef _RWSTD_STRICT_ANSI
// use an extension of this implementation: NULL file name argument
// creates a temporary file; closing the file stream object or its
// associated filebuf removes the temporary file
const char* const fname = 0;
#else // if defined (_RWSTD_STRICT_ANSI)
char fnamebuf [L_tmpnam];
// create a temporary filename
const char* const fname = std::tmpnam (fnamebuf);
if (!fname)
return 1;
#endif // _RWSTD_STRICT_ANSI
// typedefs for convenience
typedef std::basic_istream<char, std::char_traits<char> > Input;
typedef std::basic_ofstream<Input::char_type, Input::traits_type> Output;
Input::char_type s [200];
// open a file for read and write operations
Output out (fname, std::ios::in | std::ios::out | std::ios::trunc);
// tie the istream object to the ofstream filebuf
Input in (out.rdbuf ());
double x = 3.14159;
int i = 3;
// output to the file
out << "He lifted his head and pondered.\n"
<< x << '\n' << i << '\n';
// seek to the beginning of the file
in.seekg (0);
// read from the file using formatted functions
in.getline (s, sizeof s);
in >> x >> i;
// seek to the beginning of the file
in.seekg (0, std::ios::beg);
// output the all file to the standard output
std::cout << in.rdbuf ();
// seek to the beginning of the file
in.seekg (0);
// read the first line in the file
// "He lifted his head and pondered."
in.getline (s, sizeof s / 2);
std::cout << s << '\n';
// read the second line in the file
in.getline (s, sizeof s / 2);
std::cout << s << '\n';
// seek to the beginning of the file
in.seekg (0);
// read the first line in the file
// "He lifted his head and pondered."
in.get (s, sizeof s / 2);
// remove the newline character
in.ignore ();
std::cout << s << '\n';
// read the second line in the file
// 3.14159
in.get (s, sizeof s / 2);
std::cout << s << '\n';
// remove the newline character
in.ignore ();
// store the current file position
const Input::pos_type position = in.tellg ();
out << "\nreplace the int\n";
// move back to the previous saved position
in.seekg (position);
// for convenience
const Input::int_type eof = Input::traits_type::eof ();
// output the remainder of the file
// this is equivalent to "std::cout << in.rdbuf ();"
while (!Input::traits_type::eq_int_type (in.peek (), eof))
std::cout << Input::traits_type::to_char_type (in.get ());
std::cout << "\n\n\n";
if (fname) {
// close the stream before removing the file
out.close ();
// remove the temporary file (must have a name)
std::remove (fname);
}
return 0;
}

View File

@@ -0,0 +1,94 @@
/***************************************************************************
*
* istreambuf_iterator.cpp - istreambuf_iterator example
*
* $Id: istreambuf_iterator.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <fstream> // for ofstream, istreambuf_iterator
#include <iostream> // for cout
#include <cstdio> // for tmpnam () and remove ()
#include <examples.h>
int main ( )
{
#ifndef _RWSTD_STRICT_ANSI
// use an extension of this implementation: NULL file name argument
// creates a temporary file; closing the file stream object or its
// associated filebuf removes the temporary file
const char* const fname = 0;
#else // if defined (_RWSTD_STRICT_ANSI)
char fnamebuf [L_tmpnam];
// create a temporary filename
const char* const fname = std::tmpnam (fnamebuf);
if (!fname)
return 1;
#endif // _RWSTD_STRICT_ANSI
// open the file is_iter.out for reading and writing
std::ofstream out (fname, std::ios::out | std::ios::in | std::ios::trunc);
// output the example sentence into the file
out << "Ceci est un simple exemple pour demontrer le\n"
"fonctionnement de istreambuf_iterator.";
// seek to the beginning of the file
out.seekp (0);
// construct an istreambuf_iterator pointing to
// the ofstream object underlying streambuffer
std::istreambuf_iterator<char, std::char_traits<char> > iter (out.rdbuf ());
// construct an end of stream iterator
const std::istreambuf_iterator<char, std::char_traits<char> > end;
std::cout << '\n';
// output the content of the file
while (!iter.equal (end)) {
// use both operator++ and operator*
std::cout << *iter++;
}
std::cout << '\n';
if (fname) {
// close the stream before removing the file
out.close ();
// remove the temporary file (must have a name)
std::remove (fname);
}
return 0;
}

View File

@@ -0,0 +1,142 @@
/***************************************************************************
*
* istringstream.cpp - basic_istringstream example
*
* $Id: istringstream.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iomanip> // for setw
#include <ios> // for dec, hex, oct, right, showbase
#include <iostream> // for endl, wcout, wostream
#include <locale> // for isspace()
#include <sstream> // for strstream, wistringstream
#include <string> // for wstring
#include <examples.h>
int main ( )
{
try {
// create a read/write stream object
std::wistringstream in (std::ios::in | std::ios::out);
// associate the ostream object's buffer with the ifstream object
std::wostream out (in.rdbuf ());
// enable exceptions in both streams
in.exceptions (std::ios::badbit);
out.exceptions (std::ios::failbit | std::ios::badbit);
// write string into out
out << L"Il avait l'air heureux." << std::endl;
// seek to the beginning of the stream
in.seekg (0);
wchar_t c;
// output each space-separated word on a separate line
while (in.get (c)) {
if ((std::isspace)(c, in.getloc ()))
std::wcout << std::endl;
else
std::wcout.put(c);
}
std::wcout << std::endl;
// clear stream state, failbit | eofbit set by last call to get ()
in.clear ();
// move back to the beginning of the stream
in.seekg (0);
wchar_t buf [40];
// guard against buffer overflow
in.width (sizeof buf);
// set right justification
std::wcout << std::right;
// does the same thing as the previous code
// output each word on a separate line in a field of width 10
while (in >> buf) {
std::wcout.width (10);
std::wcout << buf << std::endl;
}
std::wcout << std::endl;
// clear flags, last in >> buf set fail bit
// because of a newline at end of string
in.clear ();
// output the base info before each integer
out << std::showbase;
std::wostream::pos_type pos= out.tellp ();
const long l = 10;
// output l in hex with a field with of 10
out << std::hex << std::setw (10) << l << std::endl;
// output l in oct with a field with of 10
out << std::oct << std::setw (10) << l << std::endl;
// output l in dec with a field with of 10
out << std::dec << std::setw (10) << l << std::endl;
// move back to the beginning of the stream
in.seekg (0);
// output the entire stream
std::wcout << in.rdbuf () << std::endl;
// clear the flags
in.clear ();
// seek the input sequence to pos
in.seekg (pos);
in.unsetf (std::ios::basefield);
int a, b, d;
// read the previous outputted integer
in >> a >> b >> d;
// output 3 times 10
std::wcout << a << std::endl << b << std::endl << d << std::endl;
}
catch (std::ios::failure &e) {
std::wcerr << e.what () << std::endl;
}
return 0;
}

View File

@@ -0,0 +1,63 @@
/***************************************************************************
*
* istrstream.cpp - istrstream example.
*
* $Id: istrstream.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout
#include <strstream> // for strstream
#include <examples.h>
int main ()
{
// create two constant istrstream objects and initialize their
// underlying strstreambufs with string literals; the objects
// do not create copies of the literals but use them directly
// for efficiency
std::istrstream a ("Ce n'est pas l'homme qui prend la mer, ");
std::istrstream b ("c'est la mer qui prend l'homme.");
// create a dynamic strstream object
std::strstream out;
// output the contents of the streams into out
out << a.rdbuf () << b.str () << '\n';
// output the contents of out to standard output
std::cout << out.rdbuf () << '\n';
// output the contents of the stream objects to standard output
std::cout << a.str () << '\n' << b.rdbuf () << '\n';
// the dtors of a and b will not release any storage
// since the objects did not allocate any
// the dtor of out will release allocated storage since
// neither freeze() nor str() was called on the object
return 0;
}

View File

@@ -0,0 +1,60 @@
/**************************************************************************
*
* lex_comp.cpp - Example program compares to ranges lexicographically.
*
* $Id: lex_compare.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for lexicographical_compare
#include <functional> // for less
#include <ios> // for boolalpha
#include <iostream> // for cout, endl
#include <vector> // for vector
#include <examples.h>
int main()
{
typedef std::vector<int, std::allocator<int> > Vector;
const Vector::value_type d1[] = { 1, 3, 5, 32, 64 };
const Vector::value_type d2[] = { 1, 3, 2, 43, 56 };
// Create vectors.
const Vector v1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
const Vector v2 (d2 + 0, d2 + sizeof d1 / sizeof *d2);
// Is v1 less than v2 (I think not).
bool b1 = std::lexicographical_compare (v1.begin (), v1.end (),
v2.begin (), v2.end ());
// Is v2 less than v1 (yup, sure is).
bool b2 = std::lexicographical_compare (v2.begin (), v2.end (),
v1.begin (), v1.end (),
std::less<int>());
std::cout << std::boolalpha << b1
<< " " << b2 << std::endl;
return 0;
}

View File

@@ -0,0 +1,121 @@
/**************************************************************************
*
* limits.cpp - Example program of numeric limits class used for
* representing information about scalar types.
*
* $Id: limits.cpp 495402 2007-01-11 22:07:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1998-2006 Rogue Wave Software.
*
**************************************************************************/
#include <limits> // for numeric_limits
#include <iostream> // for cout
#include <examples.h>
std::ostream& operator<< (std::ostream &strm, std::float_denorm_style style)
{
const char* name = "(unknown)";
switch (style) {
case std::denorm_absent: name = "std::denorm_absent"; break;
case std::denorm_indeterminate: name = "std::denorm_indeterminate"; break;
case std::denorm_present: name = "std::denorm_present"; break;
}
return strm << name;
}
template <class T>
void print_limits (std::ostream &strm, const char *tname, T)
{
#define PRINT_MEMBER(type, member) \
strm << " static " << type << " " #member " = " \
<< std::numeric_limits<T>::member << ";\n"
strm << "struct std::numeric_limits<" << tname << "> {\n";
PRINT_MEMBER ("const bool", is_specialized);
PRINT_MEMBER (tname, min ());
PRINT_MEMBER (tname, max ());
PRINT_MEMBER ("const int", digits);
PRINT_MEMBER ("const int", digits10);
PRINT_MEMBER ("const bool", is_signed);
PRINT_MEMBER ("const bool", is_integer);
PRINT_MEMBER ("const bool", is_exact);
PRINT_MEMBER ("const int", radix);
PRINT_MEMBER (tname, epsilon ());
PRINT_MEMBER ("int", round_error ());
PRINT_MEMBER ("const int", min_exponent);
PRINT_MEMBER ("const int", min_exponent10);
PRINT_MEMBER ("const int", max_exponent);
PRINT_MEMBER ("const int", max_exponent10);
PRINT_MEMBER ("const bool", has_infinity);
PRINT_MEMBER ("const bool", has_quiet_NaN);
PRINT_MEMBER ("const bool", has_signaling_NaN);
PRINT_MEMBER ("const std::float_denorm_style", has_denorm);
PRINT_MEMBER ("const bool", has_denorm_loss);
PRINT_MEMBER (tname, infinity ());
PRINT_MEMBER (tname, quiet_NaN ());
PRINT_MEMBER (tname, signaling_NaN ());
PRINT_MEMBER (tname, denorm_min ());
PRINT_MEMBER ("const bool", is_iec559);
PRINT_MEMBER ("const bool", is_bounded);
PRINT_MEMBER ("const bool", is_modulo);
PRINT_MEMBER ("const bool", traps);
PRINT_MEMBER ("const bool", tinyness_before);
PRINT_MEMBER ("const int", round_style);
strm << "};\n";
}
int main ()
{
#define PRINT_LIMITS(T) print_limits (std::cout, #T, T ())
// print bool values as "false" and "true"
std::cout.setf (std::cout.boolalpha);
// print the numeric limits for an integer type
PRINT_LIMITS (int);
std::cout << '\n';
// print the numeric limits for a floating point type
PRINT_LIMITS (double);
return 0;
}

View File

@@ -0,0 +1,111 @@
/**************************************************************************
*
* list.cpp - Example program of list class.
*
* $Id: list.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for find
#include <list> // for list
#include <string> // for string
#include <iostream> // for cout
#include <examples.h>
typedef std::list<std::string, std::allocator<std::string> > StringList;
// stream out a list of strings
std::ostream& operator<< (std::ostream &out, const StringList &l)
{
// create a sentry object to guard the stream
const std::ostream::sentry guard (out);
if (guard) {
// the guard succeeded in preparing the stream for output
for (StringList::const_iterator i = l.begin (); i != l.end (); ++i) {
// insert the string into the stream object's buffer
const std::streamsize n =
out.rdbuf ()->sputn ((*i).data (), (*i).size ());
// insert the space character into the buffer
if ( std::streamsize ((*i).size ()) != n
|| std::ostream::traits_type::to_int_type (' ')
!= out.rdbuf ()->sputc (' ')) {
// set badbit if either operation failed
out.setstate (std::ios::badbit);
break;
}
}
}
return out;
}
int main ()
{
// Create a list of critters.
StringList critters;
// Insert a few critters.
critters.insert (critters.begin (), "antelope");
critters.insert (critters.begin (), "bear");
critters.insert (critters.begin (), "cat");
// Print out the list.
std::cout << critters << '\n';
// Change cat to cougar.
*std::find (critters.begin (),critters.end (), "cat") = "cougar";
std::cout << critters << '\n';
// Put a zebra at the beginning, an ocelot ahead of antelope,
// and a rat at the end.
critters.push_front ("zebra");
critters.insert (std::find (critters.begin (), critters.end (),
"antelope"), "ocelot");
critters.push_back ("rat");
std::cout << critters << '\n';
// Sort the list (Use list's sort function since the
// generic algorithm requires a random access iterator
// and list only provides bidirectional)
critters.sort ();
std::cout << critters << '\n';
// Now let's erase half of the critters.
StringList::size_type half = critters.size () / 2;
for (StringList::size_type i = 0; i != half; ++i)
critters.erase (critters.begin ());
std::cout << critters << '\n';
return 0;
}

View File

@@ -0,0 +1,84 @@
/**************************************************************************
*
* locale.cpp - Example program for the locale class.
*
* $Id: locale.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <rw/_defs.h>
#if defined (_AIX) && defined (__IBMCPP__) && !defined (_RWSTD_NO_IMPLICIT_INCLUSION)
# define _RWSTD_NO_IMPLICIT_INCLUSION
#endif
#include <algorithm> // for copy, sort
#include <iostream> // for cout
#include <iterator> // for ostream_iterator
#include <string> // for string
#include <vector> // for vector
#include <codecvte.h>
#include <examples.h>
int main ()
{
std::locale loc; // Default locale
// Construct new locale using default locale plus
// user defined codecvt facet
// This facet converts from ISO Latin
// Alphabet No. 1 (ISO 8859-1) to
// U.S. ASCII code page 437
// This facet replaces the default for
// codecvt<char,char,mbstate_t>
std::locale my_loc (loc, new ex_codecvt);
// imbue modified locale onto cout
std::locale old = std::cout.imbue (my_loc);
std::cout << "A \x93 jolly time was had by all" << std::endl;
std::cout.imbue (old);
std::cout << "A jolly time was had by all" << std::endl;
// Create a vector of strings
std::vector<std::string, std::allocator<std::string> > v;
v.insert (v.begin(), "antelope");
v.insert (v.begin(), "bison");
v.insert (v.begin(), "elk");
typedef std::ostream_iterator<std::string, char, std::char_traits<char> >
Iter;
std::copy (v.begin (), v.end (), Iter (std::cout, " "));
std::cout << std::endl;
// Sort the strings using the locale as a comparitor
std::sort (v.begin (), v.end (), loc);
std::copy (v.begin (), v.end (), Iter (std::cout," "));
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,87 @@
/**************************************************************************
*
* map.cpp - Example program of map.
*
* $Id: map.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout
#include <map> // for map
#include <string> // for string
#include <utility> // for pair
#include <examples.h>
typedef std::map<std::string, int, std::less<std::string>,
std::allocator<std::pair<const std::string, int> > >
months_type;
// Print out a map.
inline std::ostream&
operator<< (std::ostream &out, const months_type &m)
{
for (months_type::const_iterator it = m.begin (); it != m.end (); ++it)
std::cout << (*it).first << " has " << (*it).second << " days\n";
return out;
}
int main ()
{
// Create a map of months and the number of days in the month.
months_type months;
typedef months_type::value_type value_type;
// Put the months in the multimap.
months.insert (value_type (std::string ("January"), 31));
months.insert (value_type (std::string ("Febuary"), 28));
months.insert (value_type (std::string ("Febuary"), 29));
months.insert (value_type (std::string ("March"), 31));
months.insert (value_type (std::string ("April"), 30));
months.insert (value_type (std::string ("May"), 31));
months.insert (value_type (std::string ("June"), 30));
months.insert (value_type (std::string ("July"), 31));
months.insert (value_type (std::string ("August"), 31));
months.insert (value_type (std::string ("September"), 30));
months.insert (value_type (std::string ("October"), 31));
months.insert (value_type (std::string ("November"), 30));
months.insert (value_type (std::string ("December"), 31));
// Print out the months. Second Febuary is not present.
std::cout << months << std::endl;
// Find the Number of days in June.
months_type::iterator p = months.find (std::string ("June"));
// Print out the number of days in June.
if (p != months.end ())
std::cout << std::endl << (*p).first << " has "
<< (*p).second << " days\n";
return 0;
}

View File

@@ -0,0 +1,60 @@
/**************************************************************************
*
* mask_array.cpp -- Mask array examples
*
* $Id: mask_array.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout, endl
#include <valarray.h> // Includes valarray and provides stream inserter.
#include <examples.h> // Generic header for all examples.
typedef std::valarray<int> valarray_t;
typedef std::valarray<bool> maskarray_t;
int main(void) {
// Create a valarray of ints.
valarray_t::value_type ibuf[10] = {0,1,2,3,4,5,6,7,8,9};
valarray_t vi(ibuf, 10);
// Create a valarray of bools for a mask.
maskarray_t::value_type mbuf[10] = {1,0,1,1,1,0,0,1,1,0};
maskarray_t mask(mbuf,10);
// Print out the valarray<int>.
std::cout << "original valarray<int> vi\n\n" << vi << "\n\n";
// Print out the valarray<bool>.
std::cout << "original valarray<bool> mask\n\n" << mask << "\n\n";
// Print a mask array.
std::cout << "vi[mask]\n\n" << vi[mask] << "\n\n";
// Double the values of the masked array and print out.
vi[mask] += static_cast<valarray_t> (vi[mask]);
std::cout << "vi[mask] += vi[mask]\n\n" << vi << std::endl;
return 0;
}

View File

@@ -0,0 +1,61 @@
/**************************************************************************
*
* max.cpp - Example program for finding maximum of a pair of values.
*
* $Id: max.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for max, min
#include <functional> // for greater
#include <iostream> // for cout
#include <examples.h>
int main ()
{
double d1 = 10.0, d2 = 20.0;
// Find minimum.
double val1 = std::min (d1, d2);
// The greater comparator returns the greater of the two values.
double val2 = std::min (d1, d2, std::greater<double>());
// Find minimum.
double val3 = std::max (d1, d2);
// The less comparator returns the smaller of the two values.
// Note that, like every comparison in the library, max is
// defined in terms of the < operator, so using less here
// is the same as using the max algorithm with a default
// comparator.
double val4 = std::max(d1, d2, std::less<double>());
std::cout << val1 << " " << val2 << " "
<< val3 << " " << val4 << std::endl;
return 0;
}

View File

@@ -0,0 +1,64 @@
/**************************************************************************
*
* max_elem.cpp - Example program for finding maximum value in a range.
*
* $Id: max_elem.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for max_element
#include <functional> // for less
#include <iostream> // for cout, endl
#include <vector> // for vector
int main ()
{
typedef std::vector<int, std::allocator<int> > Vector;
typedef Vector::iterator Iterator;
const Vector::value_type d1[] = { 1, 3, 5, 32, 64 };
// Set up vector.
Vector v1(d1 + 0, d1 + sizeof d1 / sizeof *d1);
// Find the largest element in the vector.
Iterator it1 = std::max_element (v1.begin (), v1.end ());
// Find the largest element in the range from
// the beginning of the vector to the 2nd to last.
Iterator it2 = std::max_element (v1.begin (), v1.end () - 1,
std::less<int>());
// Find the smallest element.
Iterator it3 = std::min_element (v1.begin (), v1.end ());
// Find the smallest value in the range from
// the beginning of the vector plus 1 to the end.
Iterator it4 = std::min_element (v1.begin () + 1, v1.end (),
std::less<int>());
std::cout << *it1 << " " << *it2 << " "
<< *it3 << " " << *it4 << std::endl;
return 0;
}

View File

@@ -0,0 +1,223 @@
/**************************************************************************
*
* mbsrtowcs.cpp
*
* Example program to demonstrate an implementation of the C Standard
* Library function mbsrtowcs() in terms of the C++ Standard Library
* codecvt facet.
*
* $Id: mbsrtowcs.cpp 590060 2007-10-30 13:12:23Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
**************************************************************************/
#include <cassert> // for assert()
#include <cerrno> // for EILSEQ, errno
#include <cstring> // for strlen()
#include <cwchar> // for mbstate_t
#include <ios> // for hex
#include <iostream> // for cout
#include <locale> // for codecvt, locale
#include <examples.h>
// my_mbsrtowcs() behaves
std::size_t
my_mbsrtowcs (std::mbstate_t *pstate,
wchar_t *dst,
const char *src,
std::size_t size)
{
const std::locale global;
typedef std::codecvt<wchar_t, char, std::mbstate_t> CodeCvt;
// retrieve the codecvt facet from the global locale
const CodeCvt &cvt = std::use_facet<CodeCvt>(global);
// use a small local buffer when dst is null and ignore size
wchar_t buf [4];
if (0 == dst) {
dst = buf;
size = sizeof buf / sizeof *buf;
}
// set up pointers into the source sequence
const char* from = src;
const char* const from_end = from + std::strlen (from);
const char* from_next = from;
// set up pointers into the destination sequence
wchar_t* to = dst;
wchar_t* const to_end = to + size;
wchar_t* to_next;
// number of non-NUL wide characters stored in destination buffer
std::size_t nconv = 0;
// use a local state when pstate is null (i.e., emulate mbstowcs)
std::mbstate_t state = std::mbstate_t ();
if (0 == pstate)
pstate = &state;
for ( ; from_next != from_end && to != to_end;
from = from_next, to = dst == buf ? dst : to_next) {
// convert a (sub)sequence of the source buffer into
// the destination buffer
const std::codecvt_base::result res =
cvt.in (*pstate,
from, from_end, from_next,
to, to_end, to_next);
// verify the consistency of the xxx_next pointers
assert (from <= from_next && from_next <= from_end);
assert (to <= to_next && to_next <= to_end);
// process conversion result
switch (res) {
case std::codecvt_base::error:
// conversion error
errno = EILSEQ;
return std::size_t (-1);
case std::codecvt_base::noconv:
// only codecvt<T, T> (i.e., facets where intern_type and
// extern_type are identical) is allowed to return noconv
// treat this case as an error even though it indicates
// a bad (incorrectly implemented) codecvt facet
return std::size_t (-1);
case std::codecvt_base::partial:
// partial conversion (incomplete character or not enough
// room in destination buffer to convert the entire source
// sequence)
if (dst != buf || std::size_t (to_next - to) < size) {
errno = EILSEQ;
return std::size_t (-1);
}
nconv += to_next - to;
break;
case std::codecvt_base::ok:
// complete conversion of an initial subsequence (but not
// necessarily all) of the source buffer
nconv += to_next - to;
if (dst == buf && from_next == from_end)
return nconv;
break;
}
}
return nconv;
}
int main ()
{
static const char* const mbs [] = {
"a", "abc",
// <U0391>: Greek letter Alpha
"\xce\x91",
// <U0391><U0392>: Greek letters Alpha Beta
"\xce\x91\xce\x92",
// <U0391><U0392><U0393>: Greek letters Alpha Beta Gamma
"\xce\x91\xce\x92\xce\x93",
// <U0966>: Devangari digit 0
"\xe0\xa5\xa6",
// <U0967><U0966>: Devangari digits 10
"\xe0\xa5\xa7\xe0\xa5\xa6",
// <U0968><U0967><U0966>: Devangari digits 210
"\xe0\xa5\xa8\xe0\xa5\xa7\xe0\xa5\xa6"
};
typedef std::codecvt_byname<wchar_t, char, std::mbstate_t> CodeCvt;
// create a UCS/UTF-8 codecvt facet and install it in a locale
const std::locale utf (std::locale (""), new CodeCvt ("UTF-8@UCS"));
// set the global locale to use the UCS/UTF-8 codecvt facet
std::locale::global (utf);
// iterate over examples of UTF-8 sequences and output the wide
// character sequence each converts to
for (std::size_t i = 0; i != sizeof mbs / sizeof *mbs; ++i) {
wchar_t *dst = 0;
// initialize state to the initial shift state
std::mbstate_t state = std::mbstate_t ();
// obtain the length of the wide character sequence
// corresponding to the multibyte source sequence,
// not including the terminating NUL
const std::size_t length =
my_mbsrtowcs (&state, 0, mbs [i], std::size_t (-1));
if (std::size_t (-1) == length) {
std::cerr << "Error computing length of destination sequence.\n";
continue;
}
// allocate a wide character buffer large enough to hold
// the converted sequence including the terminating NUL
dst = new wchar_t [length + 1];
// reset state to the initial shift state
state = std::mbstate_t ();
// convert the narrow character source sequence into
// the wide character buffer
const std::size_t nconv =
my_mbsrtowcs (&state, dst, mbs [i], length + 1);
if (length != nconv) {
std::cerr << "Error converting source sequence.\n";
continue;
}
// NUL-terminate the converted string
dst [nconv] = L'\0';
// write out the wide and the narrow sequences
std::cout << "UCS-2 (" << std::dec << length << "): " << std::hex;
for (const wchar_t *pwc = dst; *pwc != L'\0'; ++pwc)
std::cout << "U+" << unsigned (*pwc) << ' ';
std::cout << " ==> UTF-8: ";
typedef unsigned char UChar;
for (const char *pc = mbs [i]; *pc; ++pc)
std::cout << "\\x" << int (UChar (*pc));
std::cout << "\"\n";
delete[] dst;
}
return 0;
}

View File

@@ -0,0 +1,156 @@
/**************************************************************************
*
* memfunc.cpp - Example program for mem_fun and other member function
* pointer wrappers.
*
* $Id: memfunc.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#if defined (__IBMCPP__)
# include <rw/_config.h>
// disable implicit inclusion to work around
// a limitation in IBM VisualAge 5 (PR #26959)
# ifndef _RWSTD_NO_IMPLICIT_INCLUSION
# define _RWSTD_NO_IMPLICIT_INCLUSION
# endif // _RWSTD_NO_IMPLICIT_INCLUSION
#endif // __IBMCPP__
#include <algorithm> // for for_each, stable_sort
#include <cstddef> // for size_t
#include <functional> // for greater
#include <iomanip> // for setprecision, setw
#include <ios> // for fixed, left, right
#include <iostream> // for cout
#include <vector> // for vector
#include <examples.h>
// Very large city class
class Megapolis
{
public:
Megapolis (const char *s = "", double n = 0)
:population(n), cityName(s) { }
// The following function cannot return void due to limitations in
// some current C++ implementations.
virtual std::size_t byPopulation () const {
std::cout << std::setw (12) << std::left << cityName
<< std::setw (12) << std::left << "Megapolis"
<< std::setw (5) << std::right
<< std::fixed << std::setprecision (1)
<< population << '\n';
return 0;
}
bool operator< ( const Megapolis* ptr_ ) {
return population > ptr_->population;
}
double population;
protected:
const char* cityName;
};
// City and surrounding area class
struct Metropolis: public Megapolis
{
Metropolis (const char *s = "", double n = 0)
: Megapolis (s, n){ }
virtual std::size_t byPopulation () const {
std::cout << std::setw (12) << std::left << cityName
<< std::setw (12) << std::left << "Metropolis"
<< std::setw (5) << std::right
<< std::fixed << std::setprecision (1)
<< population << '\n';
return 0;
}
};
namespace std {
// specialize the standard function object for Megapolis*
template<>
struct greater<Megapolis*>
{
typedef const Megapolis* first_argument_type;
typedef const Megapolis* second_argument_type;
typedef bool result_type;
result_type
operator() (first_argument_type x, second_argument_type y) const {
return y->population < x->population;
}
};
} // namespace std
int main ()
{
// Create a vector of very large cities
std::vector<Megapolis*, std::allocator<Megapolis*> > cities;
cities.push_back (new Megapolis ("Calcutta", 12));
cities.push_back (new Megapolis ("Tokyo", 26.8));
cities.push_back (new Megapolis ("Delhi", 10));
cities.push_back (new Megapolis ("Bombay", 15));
cities.push_back (new Metropolis ("Cairo", 12));
cities.push_back (new Metropolis ("New York", 7.5));
cities.push_back (new Metropolis ("Los Angeles", 3.7));
cities.push_back (new Metropolis ("Jakarta", 8.2));
// Now use mem_fun to pass byPopulation member function
// of Megapolis to the for_each function.
std::cout << "City Type Population (millions)\n";
std::cout << "----------- ----------- ---------------------\n";
std::for_each (cities.begin(), cities.end(),
std::mem_fun (&Megapolis::byPopulation));
std::cout << "\nAfter sorting...\n\n";
// Sort
std::stable_sort (cities.begin (), cities.end (),
std::greater<Megapolis*>());
std::for_each (cities.begin (), cities.end (),
std::mem_fun (&Megapolis::byPopulation));
// Free allocated memory
while (!cities.empty ()) {
delete cities.back ();
cities.pop_back ();
}
return 0;
}

View File

@@ -0,0 +1,62 @@
/**************************************************************************
*
* memfunref.cpp - Example program for mem_fun and other member function
* reference wrappers.
*
* $Id: memfunref.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for for_each
#include <functional> // for mem_fun_ref
#include <iostream> // for cout, endl
#include <examples.h>
#include <memfunref.h>
typedef sublist<int> list_type;
int main ()
{
list_type::value_type a1[] = { 2, 1, 5, 6, 4 };
list_type::value_type a2[] = { 11, 4, 67, 3, 14 };
list_type s1 (a1, a1 + sizeof a1 / sizeof *a1);
list_type s2 (a2, a2 + sizeof a2 / sizeof *a2);
// Build a list of sublists
typedef std::list<list_type, std::allocator<list_type> > list_list;
list_list l;
l.insert (l.begin (), s1);
l.insert (l.begin (), s2);
// Sort each sublist in the list
std::for_each (l.begin (), l.end (), std::mem_fun_ref (&list_type::sort));
// Display the contents of list
std::for_each (l.begin (), l.end (), std::mem_fun_ref (&list_type::disp));
return 0;
}

View File

@@ -0,0 +1,104 @@
/**************************************************************************
*
* merge.cpp - Example program demonstrating the merge algorithms.
*
* $Id: merge.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // advance, copy, inplace_merge, merge
#include <functional> // less
#include <iostream> // cout
#include <iterator> // back_inserter, ostream_iterator
#include <vector> // vector
#include <examples.h>
int main ()
{
typedef std::vector<int, std::allocator<int> > Vector;
const Vector::value_type d1[] = { 1, 2, 3, 4};
const Vector::value_type d2[] = { 11, 13, 15, 17, 12, 14, 16, 18};
// Set up two vectors.
Vector v1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
Vector v2 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
// Set up four destination vectors.
Vector v3 (d2 + 0, d2 + sizeof d2 / sizeof *d2);
Vector v4 (v3);
Vector v5 (v3);
Vector v6 (v3);
// Set up one empty vector.
Vector v7;
// Merge v1 with v2.
std::merge (v1.begin (), v1.end (),
v2.begin (), v2.end (), v3.begin ());
// Now use comparator.
std::merge (v1.begin (), v1.end (),
v2.begin (), v2.end (), v4.begin (), std::less<int>());
// Merge v5 in place.
Vector::iterator mid = v5.begin ();
std::advance (mid, 4); // equivalent to mid += 4 but more generic
std::inplace_merge (v5.begin (), mid, v5.end ());
// Now use a comparator on v6.
mid = v6.begin ();
std::advance (mid, 4); // equivalent to mid += 4 but more generic
std::inplace_merge (v6.begin (), mid, v6.end (), std::less<int>());
// Merge v1 and v2 to empty vector using insert iterator.
std::merge (v1.begin (), v1.end (),
v2.begin (), v2.end (), std::back_inserter (v7));
// Copy all to cout.
std::ostream_iterator<int, char, std::char_traits<char> >
out (std::cout, " ");
std::copy (v1.begin (), v1.end (), out);
std::cout << '\n';
std::copy (v2.begin (), v2.end (), out);
std::cout << '\n';
std::copy (v3.begin (), v3.end (), out);
std::cout << '\n';
std::copy (v4.begin (), v4.end (), out);
std::cout << '\n';
std::copy (v5.begin (), v5.end (), out);
std::cout << '\n';
std::copy (v6.begin (), v6.end (), out);
std::cout << '\n';
std::copy (v7.begin (), v7.end (), out);
std::cout << '\n';
// Merge v1 and v2 to cout.
std::merge (v1.begin (), v1.end (), v2.begin (), v2.end (), out);
std::cout << '\n';
return 0;
}

View File

@@ -0,0 +1,69 @@
/**************************************************************************
*
* messages.cpp - Example program for the messages facet.
*
* $Id: messages.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <string> // for string
#include <locale> // for locale, use_facet, messages
#include <iostream> // for cerr, cout, endl
#include <rwstdmessages.h>
#include <examples.h>
#ifndef _WIN32
# define CAT_NAME "./rwstdmessages.cat"
#else
# define CAT_NAME "rwstdmessages.dll"
#endif
int main ()
{
std::locale loc;
// Get a reference to the messages<char> facet
const std::messages<char>& msgs =
std::use_facet <std::messages<char> >(loc);
// Open a catalog and try to grab
// both some valid messages, and an invalid message
const std::string def = "Message Not Found";
std::messages<char>::catalog cat = msgs.open (CAT_NAME, loc);
if (cat != std::messages<char>::catalog (-1)) {
std::cout << msgs.get (cat, 1, _RW_MSG_HELLO, def) << '\n'
<< msgs.get (cat, 1, _RW_MSG_GOODBYE, def) << '\n'
<< msgs.get (cat, 1, _RW_MSG_NOGOOD, def) << '\n'
<< msgs.get (cat, 2, _RW_MSG_TREES, def) << '\n';
msgs.close (cat);
}
else
std::cerr << "Unable to open message catalog" << std::endl;
return 0;
}

View File

@@ -0,0 +1,68 @@
/**************************************************************************
*
* mismatch.cpp - Example program of comparing elements from two sequences
* and returning the first two elements that don't
* match each other.
*
* $Id: mismatch.cpp 648752 2008-04-16 17:01:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // mismatch
#include <functional> // less_equal
#include <iostream> // cout, endl
#include <utility> // pair
#include <vector> // vector
#include <examples.h>
int main ()
{
typedef std::vector<int, std::allocator<int> > Vector;
typedef Vector::const_iterator Iter;
const Vector::value_type d1[] = { 1, 2, 3, 4 };
const Vector::value_type d2[] = { 1, 3, 2, 4 };
// Set up two vectors.
const Vector vi1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
const Vector vi2 (d2 + 0, d2 + sizeof d2 / sizeof *d2);
// p1 will contain two iterators that point to the first pair of
// elements that are different between the two vectors.
std::pair<Iter, Iter> p1 =
std::mismatch (vi1.begin (), vi1.end (), vi2.begin ());
// Find the first two elements such that an element in the
// first vector is greater than the element in the second vector.
std::pair<Iter, Iter> p2 =
std::mismatch (vi1.begin (), vi1.end (),
vi2.begin (), std::less_equal<int>());
// Output results.
std::cout << *p1.first << ", " << *p1.second << std::endl;
std::cout << *p2.first << ", " << *p2.second << std::endl;
return 0;
}

View File

@@ -0,0 +1,96 @@
/**************************************************************************
*
* money_get.cpp - Example program for the money_get facet.
*
* $Id: money_get.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1998-2006 Rogue Wave Software.
*
**************************************************************************/
#include <locale> // for locale, money_get, use_facet
#include <sstream> // for istringstream
#include <iostream> // for cout, endl
#include <iterator> // for istreambuf_iterator
#include <examples.h>
// hardcode the name of the English US locale for known systems
#if defined (__FreeBSD__) || defined (__osf__)
// FreeBSD and Tru64 UNIX
const char en_US[] = "en_US.ISO8859-1";
#elif defined (__hpux)
// HP-UX
const char en_US[] = "en_US.iso88591";
// Windows
#elif defined (_WIN32)
const char en_US[] = "English";
#else
// AIX, IRIX, Linux, Solaris
const char en_US[] = "en_US";
#endif
int main (int argc, char *argv[])
{
// Get the monetary string and locale from the argument vector.
const char* const buffer = 1 < argc ? argv [1] : "$1,234.6789";
const char* const locname = 2 < argc ? argv [2] : en_US;
const bool intl = 3 < argc;
std::string smon;
long double fmon = 0.0;
std::ios_base::iostate state = std::ios_base::goodbit;
// Retrieve the money_get facet from the named locale.
const std::locale loc (locname);
typedef std::istreambuf_iterator<char> Iter;
typedef std::money_get<char, Iter> MoneyGet;
const MoneyGet &mgf = std::use_facet<MoneyGet>(loc);
{
// Build an istringstream object from the buffer
// and imbue the locale in it.
std::istringstream ins (buffer);
ins.imbue (loc);
// Get a string representation of the monetary value.
mgf.get (ins, Iter (), intl, ins, state, smon);
}
{
std::istringstream ins (buffer);
ins.imbue (loc);
// Get a floating point representation of the monetary value.
mgf.get (ins, Iter (), intl, ins, state, fmon);
}
// Output the original sequence and its string and floating point
// representations.
std::cout << buffer << " --> \"" << smon << "\" --> " << fmon << '\n';
// Return 0 on success, non-zero on failure.
return !(std::ios_base::eofbit == state);
}

View File

@@ -0,0 +1,186 @@
/**************************************************************************
*
* money_manip.cpp - Example program demonstrating the implementation
* of user-defined manipulators for the convenient
* extraction and insertion of monetary values
*
* $Id: money_manip.cpp 636370 2008-03-12 15:32:56Z faridz $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
**************************************************************************/
#include <iomanip> // for __rw_smanip
#include <iostream> // for cout
#include <locale> // for money_get, money_put
#include <examples.h>
// implementation class of the extraction manipulator for monetary values
template <class moneyT>
struct money_get_manip
{
moneyT *pval_;
explicit money_get_manip (moneyT *pval)
: pval_ (pval) { }
template <class charT, class Traits>
void
operator() (std::basic_ios<charT, Traits> &strm, bool intl) const {
typedef std::istreambuf_iterator<charT> Iter;
typedef std::money_get<charT, Iter> MoneyGet;
std::ios_base::iostate err = std::ios_base::goodbit;
const MoneyGet &mg = std::use_facet<MoneyGet>(strm.getloc ());
// extract the monetary value from the stream
mg.get (Iter (strm.rdbuf ()), Iter (), intl, strm, err, *pval_);
if (std::ios_base::goodbit != err)
strm.setstate (err);
}
};
// implementation class of the insertion manipulator for monetary values
template <class moneyT>
struct money_put_manip
{
const moneyT &val_;
explicit money_put_manip (const moneyT &val)
: val_ (val) { }
template <class charT, class Traits>
void
operator() (std::basic_ios<charT, Traits> &strm, bool intl) const {
typedef std::ostreambuf_iterator<charT> Iter;
typedef std::money_put<charT, Iter> MoneyPut;
const MoneyPut &tp = std::use_facet<MoneyPut>(strm.getloc ());
// insert the monetary value into the stream
const Iter end =
tp.put (Iter (strm.rdbuf ()), intl, strm, strm.fill (), val_);
if (end.failed ())
strm.setstate (std::ios_base::badbit);
}
};
// manipulator for the extraction of monetary long double values
inline std::__rw_smanip<money_get_manip<long double>, bool>
get_money (long double *pval, bool intl = false)
{
// return an object of the manipulator implementation type
// that will store the function arguments until its function
// call operator is invoked by the extraction operator for
// std::__rw_smanip
typedef money_get_manip<long double> GetMoney;
typedef std::__rw_smanip<GetMoney, bool> Manip;
return Manip (GetMoney (pval), intl);
}
// manipulator for the extraction of monetary strings
template <class charT>
inline std::__rw_smanip<money_get_manip<std::basic_string<charT> >, bool>
get_money (std::basic_string<charT> *pval, bool intl = false)
{
// return an object of the manipulator implementation type
// that will store the function arguments until its function
// call operator is invoked by the extraction operator for
// std::__rw_smanip
typedef std::basic_string<charT> String;
typedef money_get_manip<String> GetMoney;
typedef std::__rw_smanip<GetMoney, bool> Manip;
return Manip (GetMoney (pval), intl);
}
// manipulator for the insertion of monetary long double values
inline std::__rw_smanip<money_put_manip<long double>, bool>
put_money (const long double &val, bool intl = false)
{
// return an object of the manipulator implementation type
// that will store the function arguments until its function
// call operator is invoked by the insertion operator for
// std::__rw_smanip
typedef money_put_manip<long double> PutMoney;
typedef std::__rw_smanip<PutMoney, bool> Manip;
return Manip (PutMoney (val), intl);
}
// manipulator for the insertion of monetary strings
template <class charT>
inline std::__rw_smanip<money_put_manip<std::basic_string<charT> >, bool>
put_money (const std::basic_string<charT> &val, bool intl = false)
{
// return an object of the manipulator implementation type
// that will store the function arguments until its function
// call operator is invoked by the insertion operator for
// std::__rw_smanip
typedef std::basic_string<charT> String;
typedef money_put_manip<String> PutMoney;
typedef std::__rw_smanip<PutMoney, bool> Manip;
return Manip (PutMoney (val), intl);
}
int main (int argc, char *argv[])
{
const std::string valstr = argc < 2 ? "0" : argv [1];
// set cin and cout's locale to the one specified in the environment
std::cin.imbue (std::locale (""));
std::cout.imbue (std::cin.getloc ());
// output the monetary value specified by the command line argument,
// including the currency symbol, in both the local and international
// formats
std::cout.setf (std::cout.showbase);
std::cout << put_money (valstr) << '\n';
std::cout << put_money (valstr, true) << '\n';
long double val = 0.0L;
// input a long double monetary value
std::cin >> get_money (&val);
// if the input was successful, output the extracted monetary value
// otherwise, output an error message
if (std::cin.good ()) {
std::cout << put_money (val) << '\n';
std::cout << put_money (val, true) << '\n';
}
else {
std::cerr << "get_money (long double*) failed\n";
return 1;
}
return 0;
}

View File

@@ -0,0 +1,102 @@
/**************************************************************************
*
* moneypunct.cpp - Example program for the moneypunct facet.
*
* $Id: moneypunct.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <climits> // for CHAR_MAX
#include <clocale> // for lconv, localeconv()
#include <iostream> // for cout, endl
#include <locale> // for locale
#include <examples.h>
int main ()
{
// Get a moneypunct facet from the classic "C" locale
const std::moneypunct<char, false> &mp =
std::use_facet<std::moneypunct<char, false> >(std::locale::classic ());
const std::lconv* const lc = std::localeconv ();
// Expected output:
// Punctuator C++ C99
// Decimal point = '.' "."
// Thousands separator = ',' ""
// Currency symbol = "" ""
// Negative Sign = "" ""
// Positive Sign = "" ""
// Digits after decimal = 0 CHAR_MAX
std::cout << "Punctuator C++ C99\n";
std::cout << "Decimal point = '"
<< mp.decimal_point () << "' \""
<< lc->decimal_point << '"';
std::cout << "\nThousands separator = '"
<< mp.thousands_sep () << "' \""
<< lc->thousands_sep << '"';
std::cout << "\nCurrency symbol = \""
<< mp.curr_symbol () << "\" \""
<< lc->currency_symbol << '"';
std::cout << "\nNegative Sign = \""
<< mp.negative_sign () << "\" \""
<< lc->negative_sign << '"';
std::cout << "\nPositive Sign = \""
<< mp.positive_sign () << "\" \""
<< lc->positive_sign << '"';
std::cout << "\nDigits after decimal = "
<< mp.frac_digits () << " ";
// since the value of CHAR_MAX depends on the signedness of char,
// if lc->frac_digits equals CHAR_MAX print the string "CHAR_MAX"
// to maintain the same output across all architectures, otherwise
// print the actual value
if (CHAR_MAX == lc->frac_digits)
std::cout << "CHAR_MAX";
else {
// this block should not be entered in a conforming environment
// since, according to 7.11, p2 of C99, frac_digits is required
// to be equal to CHAR_MAX in the "C" locale
// cast lconv::frac_digits from char to int to print out
// the numeric value of the member
std::cout << int (lc->frac_digits)
<< " (libc bug: expected CHAR_MAX, i.e., "
<< unsigned ((unsigned char)CHAR_MAX)
<< ")";
}
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,64 @@
/**************************************************************************
*
* moneyput.cpp - Example program for the money_put facet.
*
* $Id: moneyput.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <locale> // for locale, money_put, use_facet
#include <iostream> // for cout, endl
#include <iterator> // for ostreambuf_iterator
#include <string> // for string
#include <examples.h>
int main ()
{
typedef std::ostreambuf_iterator<char, std::char_traits<char> > Iter;
const std::string buffer ("10002");
const long double ldval = 10002;
// Construct a ostreambuf_iterator on cout
Iter begin (std::cout);
const std::locale loc;
// Get a money put facet
const std::money_put<char, Iter> &mp =
std::use_facet<std::money_put<char, Iter> >(loc);
// Put out the string representation of the monetary value
std::cout << buffer << " --> ";
mp.put (begin, false, std::cout, ' ', buffer);
// Put out the long double representation of the monetary value
std::cout << std::endl << ldval << " --> ";
mp.put (begin, false, std::cout, ' ', ldval);
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,88 @@
/**************************************************************************
*
* multimap.cpp - Example program for multimap class.
*
* $Id: multimap.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout, ostream
#include <map> // for multimap
#include <string> // for string
#include <examples.h>
typedef std::multimap<int, std::string, std::less<int>,
std::allocator<std::pair<const int, std::string> > >
months_type;
// Print out a multimap.
inline std::ostream&
operator<< (std::ostream &out, const months_type &m)
{
for (months_type::const_iterator it = m.begin (); it != m.end (); ++it)
std::cout << (*it).second << " has " << (*it).first << " days\n";
return out;
}
int main ()
{
// Create a multimap of months and the number of days in the month.
months_type months;
typedef months_type::value_type value_type;
// Put the months in the multimap with the number of days
// as the not necessarily unique key.
months.insert (value_type (31, std::string ("January")));
months.insert (value_type (28, std::string ("Febuary")));
months.insert (value_type (31, std::string ("March")));
months.insert (value_type (30, std::string ("April")));
months.insert (value_type (31, std::string ("May")));
months.insert (value_type (30, std::string ("June")));
months.insert (value_type (31, std::string ("July")));
months.insert (value_type (31, std::string ("August")));
months.insert (value_type (30, std::string ("September")));
months.insert (value_type (31, std::string ("October")));
months.insert (value_type (30, std::string ("November")));
months.insert (value_type (31, std::string ("December")));
// Print out the months.
std::cout << "All months of the year\n" << months << std::endl;
// Find all months with 30 days.
std::pair<months_type::iterator, months_type::iterator> p =
months.equal_range (30);
// Print out the 30 day months.
std::cout << "\nMonths with 30 days\n";
for ( ; p.first != p.second; ++p.first)
std::cout << (*p.first).second << '\n';
return 0;
}

View File

@@ -0,0 +1,99 @@
/**************************************************************************
*
* multiset.cpp - Example program for multiset class.
*
* $Id: multiset.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for set_intersection(), set_union()
#include <iostream> // for cout, endl, ostream
#include <iterator> // for ostream_iterator
#include <sstream> // for ostringstream
#include <set> // for set
#include <examples.h>
typedef std::multiset<int, std::less<int>, std::allocator<int> > IntSet;
std::ostream& operator<< (std::ostream& out, const IntSet& s)
{
std::ostringstream strm;
typedef std::ostream_iterator<int, char, std::char_traits<char> >
OstreamIter;
std::copy (s.begin (), s.end (), OstreamIter (strm, " "));
const std::ostream::sentry guard (out);
if (guard) {
const std::string data = strm.str ();
if ( std::streamsize (data.size ())
!= out.rdbuf ()->sputn (data.data (), data.size ()))
out.setstate (std::ios::badbit);
}
return out;
}
int main ()
{
// Create a multiset of integers.
IntSet si;
for (IntSet::value_type j = 0; j < 2; j++) {
for (IntSet::value_type i = 0; i < 10; ++i)
// Insert values with a hint.
si.insert (si.begin (), i);
}
// Print out the multiset.
std::cout << si << std::endl;
// Make another multiset and an empty multiset.
IntSet si2, result;
for (IntSet::value_type i = 0; i < 10; i++)
si2.insert (i + 5);
std::cout << si2 << std::endl;
// Try a couple of set algorithms.
std::set_union (si.begin (), si.end (), si2.begin (), si2.end (),
std::inserter (result, result.begin ()));
std::cout << "Union:\n" << result << std::endl;
result.erase (result.begin (), result.end ());
std::set_intersection (si.begin (), si.end (), si2.begin (), si2.end (),
std::inserter (result, result.begin ()));
std::cout << "Intersection:\n" << result << std::endl;
return 0;
}

View File

@@ -0,0 +1,100 @@
/**************************************************************************
*
* mutex.cpp - Example program for mutex.
*
* This program illustraes how to mutexes are used to synchronize access
* to resources in a multi-threaded program.
*
* NOTE: this program exposes implementation details of the Rogue Wave(R)
* C++ Standard library which are subject to change without notice
* from one version of the library to another. Portable code should
* avoid relying on any implementation details.
*
* $Id: mutex.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout
#include <rw/_mutex.h> // for __rw::__rw_mutex
#include <examples.h>
#ifdef _RWSTD_REENTRANT
// An integer shared amongst multiple threads.
int shared;
// A mutex used to synchronize updates to shared.
__rw::__rw_mutex shared_mutex;
// Increment shared by one. Uses a mutex directly.
void increment_shared ()
{
shared_mutex._C_acquire(); // Lock the mutex.
++shared;
shared_mutex._C_release(); // Unlock the mutex.
}
// Decrement shared by one. Uses a mutex guard.
void decrement_shared ()
{
_RWSTD_MT_GUARD (shared_mutex); // Acquire the lock on shared_mutex.
--shared;
// The lock on shared is released when destructor is called on guard.
}
int exchange_shared ()
{
int new_value = shared;
new_value *= new_value;
// atomically assign new_value to shared and return shared's old value
int old_value = _RWSTD_ATOMIC_SWAP (shared, new_value, shared_mutex);
return old_value;
}
#endif // _RWSTD_REENTRANT
int main()
{
#ifdef _RWSTD_REENTRANT
increment_shared();
decrement_shared();
std::cout << "Multi Threading enabled" << std::endl;
#else // if !defined (_RWSTD_REENTRANT)
std::cout << "Multi Threading not enabled" << std::endl;
#endif // _RWSTD_REENTRANT
return 0;
}

View File

@@ -0,0 +1,69 @@
/**************************************************************************
*
* negator.cpp - Example program for reversing the sense of predicate
* function objects by using function adaptors and function
* objects.
*
* $Id: negator.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <functional> // for unary_function
#include <ios> // for boolalpha
#include <iostream> // for cout, endl
#include <examples.h>
// Create a new predicate from unary_function.
template <class Arg>
struct is_odd : public std::unary_function<Arg, bool>
{
bool operator() (const Arg &arg1) const {
return arg1 % 2 != 0;
}
};
int main ()
{
std::less<int> less_func;
// Use not2 on less.
std::cout << std::boolalpha
<< less_func (1, 4) << '\n'
<< less_func (4, 1) << '\n'
<< std::not2 (std::less<int>())(1, 4) << '\n'
<< std::not2 (std::less<int>())(4, 1) << '\n';
// Create an instance of our predicate.
is_odd<int> odd;
// Use not1 on our user defined predicate.
std::cout << odd (1) << '\n'
<< odd (4) << '\n'
<< std::not1 (odd)(1) << '\n'
<< std::not1 (odd)(4) << std::endl;
return 0;
}

View File

@@ -0,0 +1,84 @@
/**************************************************************************
*
* nthelem.cpp - Example program for rearranging a collection based on nth
* element.
*
* $Id: nthelem.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <algorithm> // for swap
#include <vector> // for vector
#include <iostream> // for cout, endl
#include <examples.h>
template <class RandomAccessIterator>
void quik_sort (RandomAccessIterator beg, RandomAccessIterator end)
{
const long dist = long (end - beg);
// Stop condition for recursion.
if (dist > 2) {
// Use nth_element to do all the work for quik_sort.
std::nth_element (beg, beg + (dist / 2), end);
// Recursive calls to each remaining unsorted portion.
quik_sort (beg, beg + (dist / 2 - 1));
quik_sort (beg + (dist / 2 + 1), end);
}
else if (2 == dist && *(end - 1) < *beg)
std::swap (beg, end);
}
int main ()
{
typedef std::vector<int, std::allocator<int> > Vector;
// Initialize a vector using an array of integers.
const Vector::value_type arr[] = { 37, 12, 2, -5, 14, 1, 0, -1, 14, 32 };
Vector v (arr, arr + sizeof arr / sizeof *arr);
// Print the initial vector.
std::cout << "The unsorted values are: \n ";
for (Vector::iterator i = v.begin (); i != v.end (); ++i)
std::cout << *i << ", ";
std::cout << "\n\n";
// Use the new sort algorithm.
quik_sort (v.begin (), v.end ());
// Output the sorted vector.
std::cout << "The sorted values are: \n ";
for (Vector::iterator j = v.begin (); j != v.end (); ++j)
std::cout << *j << ", ";
std::cout << "\n\n";
return 0;
}

View File

@@ -0,0 +1,88 @@
/**************************************************************************
*
* numget.cpp - Example program for the numget facet.
*
* $Id: num_get.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout, endl
#include <sstream> // for istringstream
#include <string> // for string
int main ()
{
typedef std::istreambuf_iterator<char, std::char_traits<char> > Iter;
std::ios::iostate state;
Iter end;
bool bval = false;
long lval = 0L;
long double ldval = 0.0;
const std::locale loc;
// Get a num_get facet
const std::num_get<char, Iter> &ng =
std::use_facet<std::num_get<char, Iter> >(loc);
#ifndef _RWSTD_NO_BOOL
{
// Build an istringstream from the buffer and construct
// beginning and ending iterators on it.
std::istringstream ins ("true");
Iter begin (ins);
// Set stream flags to recognize boolalpha input.
ins.setf (std::ios::boolalpha);
// Get the boolean value from the stream.
ng.get (begin, end, ins, state, bval);
}
#endif
std::cout << bval << std::endl;
{
// Get the date
std::istringstream ins ("2422235");
Iter begin (ins);
ng.get (begin, end, ins, state, lval);
}
std::cout << lval << std::endl;
{
// Get the weekday
std::istringstream ins ("32324342.98908");
Iter begin (ins);
ng.get (begin, end, ins, state, ldval);
}
std::cout.setf (std::ios::fixed, std::ios::floatfield);
std::cout << ldval << std::endl;
return 0;
}

View File

@@ -0,0 +1,110 @@
/**************************************************************************
*
* num_put.cpp - Example program for the num_put facet.
*
* $Id: num_put.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cout, endl
#include <iterator> // for ostreambuf_iterator
#include <locale> // for time_put
#include <examples.h>
// custom numeric punctuation facet
struct Punct: std::numpunct<char>
{
char do_decimal_point () const {
return ';';
}
string_type do_truename () const {
return "numeric formatting";
}
char do_thousands_sep () const {
return ':';
}
std::string do_grouping () const {
return "\1\2\3";
}
};
int main ()
{
// construct a custom punctuation facet
std::numpunct<char> *punct = new Punct;
// construct a locale containing the custom facet
const std::locale loc (std::cout.getloc (), punct);
// imbue combined locale in cout
std::cout.imbue (loc);
// retrieve the standard numeric formatting facet
const std::num_put<char> &nput =
std::use_facet<std::num_put<char> >(std::cout.getloc ());
// obtain a stream buffer iterator operating on cout's buffer
std::ostreambuf_iterator<char> it = std::cout.rdbuf ();
// write out bool values as strings
std::cout.setf (std::ios::boolalpha);
*nput.put (it, std::cout, ' ', true) = '\n';
// set the field width and justification for the next operation
std::cout.width (18);
std::cout.setf (std::ios::fixed | std::ios::right);
const double Pi = 3.14159265358979323846;
// truncate Pi to an integer and write it out, padded on the left with periods
*nput.put (it, std::cout, '.', long (Pi)) = '\n';
// write out a number in hex notation using capital letters
std::cerr.width (13);
std::cerr.setf (std::ios::right | std::ios::uppercase);
std::cerr.setf (std::ios::hex, std::ios::basefield);
*nput.put (it, std::cerr, ' ', 3735928559UL) = '\n';
std::cout.width (18);
std::cout.setf (std::ios::internal, std::ios::adjustfield);
std::cout.setf (std::ios::showpos);
// write Pi out padded after the sign with spaces
*nput.put (it, std::cout, ' ', Pi) = '\n';
// write Pi out again, multiplied by a large number (to see grouping)
*nput.put (it, std::cout, ' ', Pi * 1000000) = '\n';
// and again, in scientific notation
std::cout.precision (11);
std::cout.setf (std::ios::scientific, std::ios::floatfield);
*nput.put (it, std::cout, ' ', Pi * 1000000) = '\n';
return 0;
}

View File

@@ -0,0 +1,104 @@
/**************************************************************************
*
* numpunct.cpp - Example program for the numpunct facet.
* $Id: numpunct.cpp 495453 2007-01-12 00:56:15Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <iostream> // for cerr, cout
#include <locale> // for locale, numpunct, use_facet
#include <stdexcept> // for runtime_error
#include <examples.h>
static std::locale
make_german_locale ()
{
static const char* const
locale_names [] = {
"de_DE.ISO8859-1", // AIX, Solaris, Tru64
"de_DE.iso88591", // HP-UX, Linux
"de_DE.88591",
"De_DE.88591", // Reliant
"de_DE",
"de", // Linux, Solaris
"German",
"german", // Linux
"deutsch", // Linux
"german_germany.1252", // Windows
0 // (sentinel)
};
std::locale german;
// iterate over the know locale names above until a valid one
// is found (i.e., one that doesn't cause locale to throw)
for (const char* const *names = locale_names; ; ) {
try {
german = std::locale (names [0]);
break;
}
catch (std::runtime_error&) {
// continue trying until the next name is null
if (0 == *++names)
throw;
}
catch (...) {
throw;
}
}
return german;
}
int main ()
{
typedef std::numpunct<char> Numpunct;
try {
// try to contruct a german locale
const std::locale german (make_german_locale ());
// obtain a numpunct facet for the german locale
const Numpunct &np = std::use_facet<Numpunct>(german);
std::cout << "German locale"
<< "\nDecimal point = " << np.decimal_point ()
<< "\nThousands separator = " << np.thousands_sep ()
<< "\nTrue name = " << np.truename ()
<< "\nFalse name = " << np.falsename()
<< '\n';
}
catch (std::runtime_error& e) {
// a runtime_error will be thrown if the locale cannot be constructed
std::cerr << "Caught runtime_error:\n";
std::cerr << e.what () << '\n';
}
catch (...) {
std::cerr << "Caught an unknown exception\n";
}
return 0;
}

View File

@@ -0,0 +1,184 @@
/***************************************************************************
*
* ostream1.cpp - ostream example
*
* $Id: ostream.cpp 429755 2006-08-08 17:57:38Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1998-2006 Rogue Wave Software.
*
**************************************************************************/
#include <climits> // for limits macrors
#include <iomanip> // for setfill, setprecision, setw
#include <ios> // for dec, hex, oct, showbase, showpos, etc.
#include <iostream> // for cout
#include <sstream> // for istringstream
#include <examples.h>
int main ()
{
// create a read/write stringbuf object on tiny char
// and attach it to an istringstream object
std::istringstream in (std::ios::in | std::ios::out);
// tie the ostream object to the istringstream object
std::ostream out (in.rdbuf ());
// output integer values in decimal, octal and hexadecimal notation
// assume 16-bit short, 32-bit int, and 64-bit long or long long
const short i16_min = SHRT_MIN;
const short i16_max = SHRT_MAX;
const unsigned short u16_max = USHRT_MAX;
const int i32_min = INT_MIN;
const int i32_max = INT_MAX;
const unsigned int u32_max = UINT_MAX;
#if INT_MAX < LONG_MAX
// sizeof (int) < sizeof (long) or there is no type wider than long
typedef long Int64;
typedef unsigned long UInt64;
#elif LONG_MAX < LLONG_MAX
// sizeof (long) < sizeof (long long) and sizeof (long) == sizeof (int)
typedef long long Int64;
typedef unsigned long long UInt64;
#else
// no 64-bit wide integer found
# define NO_INT64
#endif // INT_MAX < LONG_MAX < LLONG_MAX
#ifndef NO_INT64
// assuming a 2's complement representation, compute LLONG_MIN,
// LLONG_MAX, and ULLONG_MAX (the C99 macros are not part of
// C++ '98 or C++ '03 and may not be #defined)
const Int64 i64_min = (UInt64 (-1) >> 1) + 1;
const Int64 i64_max = UInt64 (-1) >> 1;
const UInt64 u64_max = UInt64 (-1);
#endif // NO_INT64
// output the '0' and "0x" prefix to indicate values in octal
// and hexadecimal notation
out << std::showbase;
// output the leading plus sign for positive signed integers
// (but not for unsigned integers)
out << std::showpos;
out << "int16_t min = "
<< std::dec << std::setw (6) << i16_min << ' '
<< std::oct << std::setw (7) << i16_min << ' '
<< std::hex << i16_min << '\n';
out << "int16_t max = "
<< std::dec << std::setw (6) << i16_max << ' '
<< std::oct << std::setw (7) << i16_max << ' '
<< std::hex << i16_max << '\n';
out << "uint16_t max = "
<< std::dec << std::setw (6) << u16_max << ' '
<< std::oct << std::setw (7) << u16_max << ' '
<< std::hex << u16_max << '\n';
out << "int32_t min = "
<< std::dec << std::setw (11) << i32_min << ' '
<< std::oct << std::setw (12) << i32_min << ' '
<< std::hex << i32_min << '\n';
out << "int32_t max = "
<< std::dec << std::setw (11) << i32_max << ' '
<< std::oct << std::setw (12) << i32_max << ' '
<< std::hex << i32_max << '\n';
out << "uint32_t max = "
<< std::dec << std::setw (11) << u32_max << ' '
<< std::oct << std::setw (12) << u32_max << ' '
<< std::hex << u32_max << '\n';
#ifndef NO_INT64
out << "int64_t min = "
<< std::dec << std::setw (20) << i64_min << ' '
<< std::oct << std::setw (23) << i64_min << ' '
<< std::hex << i64_min << '\n';
out << "int64_t max = "
<< std::dec << std::setw (20) << i64_max << ' '
<< std::oct << std::setw (23) << i64_max << ' '
<< std::hex << i64_max << '\n';
out << "uint64_t max = "
<< std::dec << std::setw (20) << u64_max << ' '
<< std::oct << std::setw (23) << u64_max << ' '
<< std::hex << u64_max << '\n';
#else // if defined (NO_INT64)
// hardcoded for platforms with no 64-bit type
out << "int64_t min = -9223372036854775808 "
<< "01000000000000000000000 "
<< "0x8000000000000000\n";
out << "int64_t max = +9223372036854775807 "
<< "0777777777777777777777 "
<< "0x7fffffffffffffff\n";
out << "uint64_t max = 18446744073709551615 "
<< "01777777777777777777777 "
<< "0xffffffffffffffff\n";
#endif // NO_INT64
const char sep[] = "----------------------------------------";
// output a leading portion of sep
out.write (sep, 10).put ('\n');
// set the field width to 10, the the padding character to '*'
// and and output the floating point pi constant with 2 digits
// after the decimal point
const double pi = 3.14159123;
out << std::noshowpos // do not output the leading plus sign
<< std::setprecision (3) // two digits after the decimal point
<< std::setfill ('*') // padded to width with the asterisk
<< std::setw (10) // field width
<< pi
<< '\n';
// output the entire contents of the buffer to standard output
std::cout << in.rdbuf ();
return 0;
}

View File

@@ -0,0 +1,101 @@
/***************************************************************************
*
* ostreambuf_iterator.cpp - ostreambuf_iterator example
*
* $Id: ostreambuf_iterator.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <fstream> // for filebuf
#include <iostream> // for cout
#include <iterator> // for streambuf_iterator's
#include <cstdio> // for L_tmpnam, tmpnam and remove
#include <examples.h>
int main ( )
{
#ifndef _RWSTD_STRICT_ANSI
// use an extension of this implementation: NULL file name argument
// creates a temporary file; closing the file stream object or its
// associated filebuf removes the temporary file
const char* const fname = 0;
#else // if defined (_RWSTD_STRICT_ANSI)
char fnamebuf [L_tmpnam];
// create a temporary filename
const char* const fname = std::tmpnam (fnamebuf);
if (!fname)
return 1;
#endif // _RWSTD_STRICT_ANSI
// create a filebuf object
std::filebuf buf;
// open the file and link it to the filebuf object
buf.open (fname, std::ios::in | std::ios::out | std::ios::trunc);
// create an ostreambuf_iterator and link it to
// the filebuf object
std::ostreambuf_iterator<char, std::char_traits<char> > out_iter (&buf);
const char charset[] =
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
// output characters into the file using the ostreambuf_iterator
for (unsigned i = 0; charset [i]; ++i)
out_iter = charset [i];
// seek to the beginning of the file
buf.pubseekpos(0);
// create an istreambuf_iterator and link it to the filebuf object
std::istreambuf_iterator<char, std::char_traits<char> > in_iter (&buf);
// construct an end of stream iterator
std::istreambuf_iterator<char, std::char_traits<char> > end;
std::cout << '\n';
// output the contents of the file
while (!in_iter.equal (end))
std::cout << *in_iter++;
std::cout << '\n';
if (fname) {
// close the stream before removing the file
buf.close ();
// remove the temporary file (must have a name)
std::remove (fname);
}
return 0;
}

View File

@@ -0,0 +1,3 @@
For the series: 1 2 3 4 5 6 7 8 9 10 where N = 10
The sum = (N * N + N) / 2 = 55
The product = N! = 3628800

View File

@@ -0,0 +1,8 @@
For the vector:
1 1 2 3 5 8 13 21 34 55
The differences between adjacent elements are:
1 0 1 1 2 3 5 8 13 21
The products of adjacent elements are:
1 1 2 6 15 40 104 273 714 1870

View File

@@ -0,0 +1,6 @@
For the list: 3 4 5 6 7 8
When the iterator is initialized to l.begin (),
it points to 3
After advance (itr, 4), the iterator points to 7

View File

@@ -0,0 +1,4 @@
X::X (12345)
b destroyed
12345
X::~X [12345]

View File

@@ -0,0 +1,3 @@
Container contents: 0 1 2 2 2 2 3 4 6 7
The number 3 was found
The number 11 was NOT found

View File

@@ -0,0 +1 @@
3 3 3

View File

@@ -0,0 +1,4 @@
4....:....3....:....2....:....1....:....0
b = 5: 0000000000000000000000000000000000000101
b = ~b: 1111111111111111111111111111111111111010
b <<= 38: 1000000000000000000000000000000000000000

View File

@@ -0,0 +1,14 @@
Before:
<EFBFBD> <20> <20> a <20> <20> <20> <20> <20>
.................
.................
After in:
<EFBFBD> <20> <20> a <20> <20> <20> <20> <20>
.................
<EFBFBD> I I a <20> U <20> <20> o
After out:
<EFBFBD> I I <20> <20> U <20> <20> <20>
.................
<EFBFBD> I I a <20> U <20> <20> o

View File

@@ -0,0 +1,6 @@
String 1 : blue
String 2 : blues
Strings "blue" and "blue" should compare equal : equal
Strings "blue" and "blues" should compare NOT equal : not equal
Normalized hash value for "blue" : 431029
Normalized hash value for "blues" : 6896579

View File

@@ -0,0 +1 @@
a = (0.000001,-0.000287), b = (58.219883,69.735392)

View File

@@ -0,0 +1,4 @@
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

View File

@@ -0,0 +1,4 @@
sequence: { 1 2 3 4 5 5 7 8 9 10 }
number of fives: 2
number of sixes: 0
number of values less than 8: 7

View File

@@ -0,0 +1,4 @@
1
0
P
BLUES POWER

View File

@@ -0,0 +1,5 @@
ace of spades
king of spades
queen of spades
jack of spades
ten of spades

View File

@@ -0,0 +1,4 @@
For the vector: 3 4 5 6 7 8
When the iterator is initialized to point to 6
The distance between the beginning and 6 is 3

View File

@@ -0,0 +1 @@
{ 1 2 3 4 } == { 1 2 4 3 } --> false

View File

@@ -0,0 +1,4 @@
The equal range for 3 is: (3 , 4)
The equal range for 2 is: (2 , 3)

View File

@@ -0,0 +1 @@
Caught an exception: std::cin: stream object has set ios::eofbit

View File

@@ -0,0 +1,89 @@
/***************************************************************************
*
* filebuf.cpp - basic_filebuf example
*
* $Id: filebuf.cpp 550991 2007-06-26 23:58:07Z sebor $
*
***************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Copyright 1994-2006 Rogue Wave Software.
*
**************************************************************************/
#include <fstream> // for filebuf, ifstream, istream
#include <iostream> // for cout, endl
#include <cstdio> // for tmpnam(), remove()
#include <examples.h>
int main ()
{
#ifndef _RWSTD_NO_EXT_FILEBUF
// use an extension of this implementation: NULL file name argument
// creates a temporary file; closing the file stream object or its
// associated filebuf removes the temporary file
const char* const fname = 0;
#else // if defined (_RWSTD_NO_EXT_FILEBUF)
char fnamebuf [L_tmpnam];
// create a temporary filename
const char* const fname = std::tmpnam (fnamebuf);
if (!fname)
return 1;
#endif // _RWSTD_NO_EXT_FILEBUF
// create a filebuf object for reading and writing of a temporary file
std::filebuf outbuf;
outbuf.open (fname, std::ios::in | std::ios::out | std::ios::trunc);
// set the internal character buffer size
// buffer will be allocated internally and deallocated in object dtor
outbuf.pubsetbuf (0, 256);
// associate the filebuf object with an iostream object
std::iostream out (&outbuf);
// open this source code file and output it to the temporary file
out << std::ifstream (__FILE__).rdbuf ();
// seek to the beginning of the stream
out.seekp (0);
// output the contents of the `out' object to standard output
std::cout << out.rdbuf ();
// close the filebuf object before removing the underlying file
outbuf.close ();
if (fname) {
// remove the temporary file if it has a name
// otherwise, if fname is NULL, the temporary file will
// have already been automatically removed by the call
// to close() above
std::remove (fname);
}
return 0;
}

View File

@@ -0,0 +1,4 @@
9 9 9 9
7 7 7 4
11 11 11 11 11
5 5 5

View File

@@ -0,0 +1 @@
3 3 2 2

View File

@@ -0,0 +1,3 @@
For the sequences { 0 1 6 5 3 2 2 6 5 7 } and { 6 5 }
both versions of find_first_of point to: 6
both versions of find_end point to: 6

View File

@@ -0,0 +1,2 @@
For the vectors { 0 1 2 2 3 4 2 2 6 7 } and { 6 4 }
both versions of find_first_of point to: 4

View File

@@ -0,0 +1,7 @@
hex 1234: 0x1234
hex 2345: 0x2345
dec 3456: 3456
oct 4567: 4567
hex 5678: 0x5678
hex 6789: 0x6789
hex 789a: 0x789a

Some files were not shown because too many files have changed in this diff Show More