Muokkaa

Jaa


<cliext/adapter> (STL/CLR)

The STL/CLR header <cliext/adapter> specifies two class templates (collection_adapter and range_adapter), and the function template make_collection.

Syntax

#include <cliext/adapter>

Requirements

Header: <cliext/adapter>

Namespace: cliext

Declarations

Class Description
collection_adapter Wraps the Base Class Library (BCL) collection as a range.
range_adapter Wraps the range as a BCL collection.
Function Description
make_collection Creates a range adapter using an iterator pair.

Members

collection_adapter

Wraps a .NET collection for use as an STL/CLR container. A collection_adapter is a template class that describes a simple STL/CLR container object. It wraps a Base Class Library (BCL) interface, and returns an iterator pair that you use to manipulate the controlled sequence.

Syntax

template<typename Coll>
    ref class collection_adapter;

template<>
    ref class collection_adapter<
        System::Collections::ICollection>;
template<>
    ref class collection_adapter<
        System::Collections::IEnumerable>;
template<>
    ref class collection_adapter<
        System::Collections::IList>;
template<>
    ref class collection_adapter<
        System::Collections::IDictionary>;
template<typename Value>
    ref class collection_adapter<
        System::Collections::Generic::ICollection<Value>>;
template<typename Value>
    ref class collection_adapter<
        System::Collections::Generic::IEnumerable<Value>>;
template<typename Value>
    ref class collection_adapter<
        System::Collections::Generic::IList<Value>>;
template<typename Key,
    typename Value>
    ref class collection_adapter<
        System::Collections::Generic::IDictionary<Key, Value>>;

Parameters

Coll
The type of the wrapped collection.

Specializations

Specialization Description
IEnumerable Sequences through elements.
ICollection Maintains a group of elements.
IList Maintains an ordered group of elements.
IDictionary Maintain a set of {key, value} pairs.
IEnumerable<Value> Sequences through typed elements.
ICollection<Value> Maintains a group of typed elements.
IList<Value> Maintains an ordered group of typed elements.
IDictionary<Value> Maintains a set of typed {key, value} pairs.

Members

Type definition Description
collection_adapter::difference_type The type of a signed distance between two elements.
collection_adapter::iterator The type of an iterator for the controlled sequence.
collection_adapter::key_type The type of a dictionary key.
collection_adapter::mapped_type The type of a dictionary value.
collection_adapter::reference The type of a reference to an element.
collection_adapter::size_type The type of a signed distance between two elements.
collection_adapter::value_type The type of an element.
Member function Description
collection_adapter::base Designates the wrapped BCL interface.
collection_adapter::begin Designates the beginning of the controlled sequence.
collection_adapter::collection_adapter Constructs an adapter object.
collection_adapter::end Designates the end of the controlled sequence.
collection_adapter::size Counts the number of elements.
collection_adapter::swap Swaps the contents of two containers.
Operator Description
collection_adapter::operator= Replaces the stored BCL handle.

Remarks

You use this template class to manipulate a BCL container as a STL/CLR container. The collection_adapter stores a handle to a BCL interface, which in turn controls a sequence of elements. A collection_adapter object X returns a pair of input iterators X.begin() and X.end() that you use to visit the elements, in order. Some of the specializations also let you write X.size() to determine the length of the controlled sequence.

collection_adapter::base

Designates the wrapped BCL interface.

Syntax

Coll^ base();

Remarks

The member function returns the stored BCL interface handle.

Example

// cliext_collection_adapter_base.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
    {
    cliext::deque<wchar_t> d6x(6, L'x');
    Mycoll c1(%d6x);

    // display initial contents "x x x x x x "
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    System::Console::WriteLine("base() same = {0}", c1.base() == %c1);
    return (0);
    }
x x x x x x
base() same = True

collection_adapter::begin

Designates the beginning of the controlled sequence.

Syntax

iterator begin();

Remarks

The member function returns an input iterator that designates the first element of the controlled sequence, or just beyond the end of an empty sequence.

Example

// cliext_collection_adapter_begin.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
{
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');
    Mycoll c1(%d1);

    // display initial contents "a b c "
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect first two items
    Mycoll::iterator it = c1.begin();
    System::Console::WriteLine("*begin() = {0}", *it);
    System::Console::WriteLine("*++begin() = {0}", *++it);
    return (0);
}
a b c
*begin() = a
*++begin() = b

collection_adapter::collection_adapter

Constructs an adapter object.

Syntax

collection_adapter();
collection_adapter(collection_adapter<Coll>% right);
collection_adapter(collection_adapter<Coll>^ right);
collection_adapter(Coll^ collection);

Parameters

collection
BCL handle to wrap.

right
Object to copy.

Remarks

The constructor:

collection_adapter();

initializes the stored handle with nullptr.

The constructor:

collection_adapter(collection_adapter<Coll>% right);

initializes the stored handle with right.base().

The constructor:

collection_adapter(collection_adapter<Coll>^ right);

initializes the stored handle with right->base().

The constructor:

collection_adapter(Coll^ collection);

initializes the stored handle with collection.

Example

// cliext_collection_adapter_construct.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
{
    cliext::deque<wchar_t> d6x(6, L'x');

    // construct an empty container
    Mycoll c1;
    System::Console::WriteLine("base() null = {0}", c1.base() == nullptr);

    // construct with a handle
    Mycoll c2(%d6x);
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct by copying another container
    Mycoll c3(c2);
    for each (wchar_t elem in c3)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct by copying a container handle
    Mycoll c4(%c3);
    for each (wchar_t elem in c4)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    return (0);
}
base() null = True
x x x x x x
x x x x x x
x x x x x x

collection_adapter::difference_type

The types of a signed distance between two elements.

Syntax

typedef int difference_type;

Remarks

The type describes a signed element count.

Example

// cliext_collection_adapter_difference_type.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
{
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');
    Mycoll c1(%d1);

    // display initial contents "a b c "
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // compute positive difference
    Mycoll::difference_type diff = 0;
    Mycoll::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        ++diff;
    System::Console::WriteLine("end()-begin() = {0}", diff);
    return (0);
}
a b c
end()-begin() = 3

collection_adapter::end

Designates the end of the controlled sequence.

Syntax

iterator end();

Remarks

The member function returns an input iterator that points just beyond the end of the controlled sequence.

Example

// cliext_collection_adapter_end.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
{
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');
    Mycoll c1(%d1);

    // display initial contents "a b c "
    Mycoll::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        System::Console::Write("{0} ", *it);
    System::Console::WriteLine();
    return (0);
}
a b c

collection_adapter::iterator

The type of an iterator for the controlled sequence.

Syntax

typedef T1 iterator;

Remarks

The type describes an object of unspecified type T1 that can serve as an input iterator for the controlled sequence.

Example

// cliext_collection_adapter_iterator.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
{
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');
    Mycoll c1(%d1);

    // display initial contents "a b c "
    Mycoll::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        System::Console::Write("{0} ", *it);
    System::Console::WriteLine();
    return (0);
}
a b c

collection_adapter::key_type

The type of a dictionary key.

Syntax

typedef Key key_type;

Remarks

The type is a synonym for the template parameter Key, in a specialization for IDictionary or IDictionary<Value>; otherwise it isn't defined.

Example

// cliext_collection_adapter_key_type.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/map>

typedef cliext::map<wchar_t, int> Mymap;
typedef cliext::collection_adapter<
    System::Collections::Generic::IDictionary<wchar_t, int>> Mycoll;
typedef System::Collections::Generic::KeyValuePair<wchar_t,int> Mypair;
int main()
{
    Mymap d1;
    d1.insert(Mymap::make_value(L'a', 1));
    d1.insert(Mymap::make_value(L'b', 2));
    d1.insert(Mymap::make_value(L'c', 3));
    Mycoll c1(%d1);

    // display contents "[a 1] [b 2] [c 3] "
    for each (Mypair elem in c1)
    {
        Mycoll::key_type key = elem.Key;
        Mycoll::mapped_type value = elem.Value;
        System::Console::Write("[{0} {1}] ", key, value);
    }
    System::Console::WriteLine();
    return (0);
}
[a 1] [b 2] [c 3]

collection_adapter::mapped_type

The type of a dictionary value.

Syntax

typedef Value mapped_type;

Remarks

The type is a synonym for the template parameter Value, in a specialization for IDictionary or IDictionary<Value>; otherwise it isn't defined.

Example

// cliext_collection_adapter_mapped_type.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/map>

typedef cliext::map<wchar_t, int> Mymap;
typedef cliext::collection_adapter<
    System::Collections::Generic::IDictionary<wchar_t, int>> Mycoll;
typedef System::Collections::Generic::KeyValuePair<wchar_t,int> Mypair;
int main()
{
    Mymap d1;
    d1.insert(Mymap::make_value(L'a', 1));
    d1.insert(Mymap::make_value(L'b', 2));
    d1.insert(Mymap::make_value(L'c', 3));
    Mycoll c1(%d1);

    // display contents "[a 1] [b 2] [c 3] "
    for each (Mypair elem in c1)
    {
        Mycoll::key_type key = elem.Key;
        Mycoll::mapped_type value = elem.Value;
        System::Console::Write("[{0} {1}] ", key, value);
    }
    System::Console::WriteLine();
    return (0);
}
[a 1] [b 2] [c 3]

collection_adapter::operator=

Replaces the stored BCL handle.

Syntax

collection_adapter<Coll>% operator=(collection_adapter<Coll>% right);

Parameters

right
Adapter to copy.

Remarks

The member operator copies right to the object, then returns *this. You use it to replace the stored BCL handle with a copy of the stored BCL handle in right.

Example

// cliext_collection_adapter_operator_as.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
{
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');
    Mycoll c1(%d1);

    // display initial contents "a b c "
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign to a new container
    Mycoll c2;
    c2 = c1;
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
}
a b c
a b c

collection_adapter::reference

The type of a reference to an element.

Syntax

typedef value_type% reference;

Remarks

The type describes a reference to an element.

Example

// cliext_collection_adapter_reference.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
    {
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');
    Mycoll c1(%d1);

    // display initial contents "a b c "
    Mycoll::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
    {   // get a reference to an element
        Mycoll::reference ref = *it;
        System::Console::Write("{0} ", ref);
    }
    System::Console::WriteLine();
    return (0);
}
a b c

collection_adapter::size

Counts the number of elements.

Syntax

size_type size();

Remarks

The member function returns the length of the controlled sequence. It isn't defined in a specialization for IEnumerable or IEnumerable<Value>.

Example

// cliext_collection_adapter_size.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
{
    cliext::deque<wchar_t> d6x(6, L'x');
    Mycoll c1(%d6x);

    // display initial contents "x x x x x x "
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    System::Console::WriteLine("size() = {0}", c1.size());
    return (0);
}
x x x x x x
size() = 6

collection_adapter::size_type

The type of a signed distance between two elements.

Syntax

typedef int size_type;

Remarks

The type describes a non-negative element count.

Example

// cliext_collection_adapter_size_type.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
{
    cliext::deque<wchar_t> d6x(6, L'x');
    Mycoll c1(%d6x);

    // display initial contents "x x x x x x"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    Mycoll::size_type size = c1.size();
    System::Console::WriteLine("size() = {0}", size);
    return (0);
}
x x x x x x
size() = 6

collection_adapter::swap

Swaps the contents of two containers.

Syntax

void swap(collection_adapter<Coll>% right);

Parameters

right
Container to swap contents with.

Remarks

The member function swaps the stored BCL handles between *this and right.

Example

// cliext_collection_adapter_swap.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
{
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');
    Mycoll c1(%d1);

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct another container with repetition of values
    cliext::deque<wchar_t> d2(5, L'x');
    Mycoll c2(%d2);
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // swap and redisplay
    c1.swap(c2);
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
}
a b c
x x x x x
x x x x x
a b c

collection_adapter::value_type

The type of an element.

Syntax

typedef Value value_type;

Remarks

The type is a synonym for the template parameter Value, if present in the specialization; otherwise it's a synonym for System::Object^.

Example

// cliext_collection_adapter_value_type.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
{
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');
    Mycoll c1(%d1);

    // display contents "a b c" using value_type
    for (Mycoll::iterator it = c1.begin();
        it != c1.end(); ++it)
    {   // store element in value_type object
        Mycoll::value_type val = *it;

        System::Console::Write("{0} ", val);
    }
    System::Console::WriteLine();
    return (0);
}
a b c

make_collection (STL/CLR)

Make a range_adapter from an iterator pair.

Syntax

template<typename Iter>
    range_adapter<Iter> make_collection(Iter first, Iter last);

Parameters

Iter
The type of the wrapped iterators.

first
First iterator to wrap.

last
Second iterator to wrap.

Remarks

The function template returns gcnew range_adapter<Iter>(first, last). You use it to construct a range_adapter<Iter> object from a pair of iterators.

Example

// cliext_make_collection.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::deque<wchar_t> Mycont;
typedef cliext::range_adapter<Mycont::iterator> Myrange;
int main()
{
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in d1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    System::Collections::ICollection^ p1 =
        cliext::make_collection(d1.begin(), d1.end());
    System::Console::WriteLine("Count = {0}", p1->Count);
    System::Console::WriteLine("IsSynchronized = {0}",
        p1->IsSynchronized);
    System::Console::WriteLine("SyncRoot not nullptr = {0}",
        p1->SyncRoot != nullptr);

    // copy the sequence
    cli::array<System::Object^>^ a1 = gcnew cli::array<System::Object^>(5);

    a1[0] = L'|';
    p1->CopyTo(a1, 1);
    a1[4] = L'|';
    for each (wchar_t elem in a1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    return (0);
}
a b c
Count = 3
IsSynchronized = False
SyncRoot not nullptr = True
| a b c |

range_adapter (STL/CLR)

A template class that wraps a pair of iterators that are used to implement several Base Class Library (BCL) interfaces. You use the range_adapter to manipulate an STL/CLR range as if it was a BCL collection.

Syntax

template<typename Iter>
    ref class range_adapter
        :   public
        System::Collections::IEnumerable,
        System::Collections::ICollection,
        System::Collections::Generic::IEnumerable<Value>,
        System::Collections::Generic::ICollection<Value>
    { ..... };

Parameters

Iter
The type associated with the wrapped iterators.

Members

Member function Description
range_adapter::range_adapter Constructs an adapter object.
Operator Description
range_adapter::operator= Replaces the stored iterator pair.

Interfaces

Interface Description
IEnumerable Iterates through elements in the collection.
ICollection Maintains a group of elements.
IEnumerable<T> Iterates through typed elements in the collection.
ICollection<T> Maintains a group of typed elements.

Remarks

The range_adapter stores a pair of iterators, which in turn delimit a sequence of elements. The object implements four BCL interfaces that let you iterate through the elements, in order. You use this template class to manipulate STL/CLR ranges much like BCL containers.

range_adapter::operator=

Replaces the stored iterator pair.

Syntax

range_adapter<Iter>% operator=(range_adapter<Iter>% right);

Parameters

right
Adapter to copy.

Remarks

The member operator copies right to the object, then returns *this. You use it to replace the stored iterator pair with a copy of the stored iterator pair in right.

Example

// cliext_range_adapter_operator_as.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::deque<wchar_t> Mycont;
typedef cliext::range_adapter<Mycont::iterator> Myrange;
int main()
{
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');
    Myrange c1(d1.begin(), d1.end());

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign to a new container
    Myrange c2;
    c2 = c1;
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
}
a b c
a b c

range_adapter::range_adapter

Constructs an adapter object.

Syntax

range_adapter();
range_adapter(range_adapter<Iter>% right);
range_adapter(range_adapter<Iter>^ right);
range_adapter(Iter first, Iter last);

Parameters

first
First iterator to wrap.

last
Second iterator to wrap.

right
Object to copy.

Remarks

The constructor:

range_adapter();

initializes the stored iterator pair with default constructed iterators.

The constructor:

range_adapter(range_adapter<Iter>% right);

initializes the stored iterator pair by copying the pair stored in right.

The constructor:

range_adapter(range_adapter<Iter>^ right);

initializes the stored iterator pair by copying the pair stored in *right.

The constructor:

range_adapter(Iter^ first, last);

initializes the stored iterator pair with first and last.

Example

// cliext_range_adapter_construct.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::deque<wchar_t> Mycont;
typedef cliext::range_adapter<Mycont::iterator> Myrange;
int main()
{
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');

    // construct an empty adapter
    Myrange c1;

    // construct with an iterator pair
    Myrange c2(d1.begin(), d1.end());
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct by copying another adapter
    Myrange c3(c2);
    for each (wchar_t elem in c3)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct by copying an adapter handle
    Myrange c4(%c3);
    for each (wchar_t elem in c4)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    return (0);
}
a b c
a b c
a b c