description | title | ms.date | ms.topic | f1_keywords | helpviewer_keywords | ms.assetid | |||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: adapter (STL/CLR) |
adapter (STL/CLR) |
06/15/2018 |
reference |
|
|
71ce7e51-42b6-4f70-9595-303791a97677 |
The STL/CLR header <cliext/adapter>
specifies two class templates (collection_adapter
and range_adapter
), and the function template make_collection
.
#include <cliext/adapter>
Header: <cliext/adapter>
Namespace: cliext
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. |
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.
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>>;
Coll
The type of the wrapped collection.
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. |
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. |
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.
Designates the wrapped BCL interface.
Coll^ base();
The member function returns the stored BCL interface handle.
// 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
Designates the beginning of the controlled sequence.
iterator begin();
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.
// 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
Constructs an adapter object.
collection_adapter();
collection_adapter(collection_adapter<Coll>% right);
collection_adapter(collection_adapter<Coll>^ right);
collection_adapter(Coll^ collection);
collection
BCL handle to wrap.
right
Object to copy.
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
.
// 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
The types of a signed distance between two elements.
typedef int difference_type;
The type describes a signed element count.
// 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
Designates the end of the controlled sequence.
iterator end();
The member function returns an input iterator that points just beyond the end of the controlled sequence.
// 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
The type of an iterator for the controlled sequence.
typedef T1 iterator;
The type describes an object of unspecified type T1
that can serve as an input iterator for the controlled sequence.
// 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
The type of a dictionary key.
typedef Key key_type;
The type is a synonym for the template parameter Key
, in a specialization for IDictionary
or IDictionary<Value>
; otherwise it isn't defined.
// 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]
The type of a dictionary value.
typedef Value mapped_type;
The type is a synonym for the template parameter Value
, in a specialization for IDictionary
or IDictionary<Value>
; otherwise it isn't defined.
// 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]
Replaces the stored BCL handle.
collection_adapter<Coll>% operator=(collection_adapter<Coll>% right);
right
Adapter to copy.
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
.
// 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
The type of a reference to an element.
typedef value_type% reference;
The type describes a reference to an element.
// 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
Counts the number of elements.
size_type size();
The member function returns the length of the controlled sequence. It isn't defined in a specialization for IEnumerable
or IEnumerable<Value>
.
// 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
The type of a signed distance between two elements.
typedef int size_type;
The type describes a non-negative element count.
// 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
Swaps the contents of two containers.
void swap(collection_adapter<Coll>% right);
right
Container to swap contents with.
The member function swaps the stored BCL handles between *this
and right
.
// 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
The type of an element.
typedef Value value_type;
The type is a synonym for the template parameter Value
, if present in the specialization; otherwise it's a synonym for System::Object^
.
// 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 a range_adapter
from an iterator pair.
template<typename Iter>
range_adapter<Iter> make_collection(Iter first, Iter last);
Iter
The type of the wrapped iterators.
first
First iterator to wrap.
last
Second iterator to wrap.
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.
// 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 |
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.
template<typename Iter>
ref class range_adapter
: public
System::Collections::IEnumerable,
System::Collections::ICollection,
System::Collections::Generic::IEnumerable<Value>,
System::Collections::Generic::ICollection<Value>
{ ..... };
Iter
The type associated with the wrapped iterators.
Member function | Description |
---|---|
range_adapter::range_adapter |
Constructs an adapter object. |
Operator | Description |
---|---|
range_adapter::operator= |
Replaces the stored iterator pair. |
Interface | Description |
---|---|
xref:System.Collections.IEnumerable | Iterates through elements in the collection. |
xref:System.Collections.ICollection | Maintains a group of elements. |
xref:System.Collections.Generic.IEnumerable%601 | Iterates through typed elements in the collection. |
xref:System.Collections.Generic.ICollection%601 | Maintains a group of typed elements. |
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.
Replaces the stored iterator pair.
range_adapter<Iter>% operator=(range_adapter<Iter>% right);
right
Adapter to copy.
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
.
// 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
Constructs an adapter object.
range_adapter();
range_adapter(range_adapter<Iter>% right);
range_adapter(range_adapter<Iter>^ right);
range_adapter(Iter first, Iter last);
first
First iterator to wrap.
last
Second iterator to wrap.
right
Object to copy.
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
.
// 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