<cliext/adapter> (STL/CLR)

STL/CLR 标头 <cliext/adapter> 指定两个类模板(collection_adapterrange_adapter)以及函数模板 make_collection

语法

#include <cliext/adapter>

要求

标头:<cliext/adapter>

命名空间:cliext

声明

说明
collection_adapter 将基类库 (BCL) 集合包装为范围。
range_adapter 将范围包装为 BCL 集合。
函数 说明
make_collection 使用迭代器对创建范围适配器。

成员

collection_adapter

包装 .NET 集合以用作 STL/CLR 容器。 collection_adapter 是描述简单 STL/CLR 容器对象的模板类。 它包装基类库 (BCL) 接口,并返回用于操作受控序列的迭代器对。

语法

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
已包装集合的类型。

专用化

专业 说明
IEnumerable 对元素进行排序。
ICollection 维护元素组。
IList 维护有序的元素组。
IDictionary 维护一组 {键, 值} 对。
IEnumerable<Value> 对类型化元素进行排序。
ICollection<Value> 维护类型化元素组。
IList<Value> 维护有序的类型化元素组。
IDictionary<Value> 维护一组类型化 {键, 值} 对。

成员

类型定义 说明
collection_adapter::difference_type 两个元素间的带符号距离的类型。
collection_adapter::iterator 受控序列的迭代器的类型。
collection_adapter::key_type 字典键的类型。
collection_adapter::mapped_type 字典值的类型。
collection_adapter::reference 元素的引用的类型。
collection_adapter::size_type 两个元素间的带符号距离的类型。
collection_adapter::value_type 元素的类型。
成员函数 说明
collection_adapter::base 指定包装的 BCL 接口。
collection_adapter::begin 指定受控序列的开头。
collection_adapter::collection_adapter 构造适配器对象。
collection_adapter::end 指定受控序列的末尾。
collection_adapter::size 对元素数进行计数。
collection_adapter::swap 交换两个容器的内容。
运算符 说明
collection_adapter::operator= 替换存储的 BCL 句柄。

备注

可使用此模板类将 BCL 容器作为 STL/CLR 容器进行操作。 collection_adapter 存储 BCL 接口的句柄,该接口又控制一系列元素。 collection_adapter 对象 X 按顺序返回一对用于访问元素的输入迭代器 X.begin()X.end()。 某些专用化还允许编写 X.size() 以确定受控序列的长度。

collection_adapter::base

指定包装的 BCL 接口。

语法

Coll^ base();

备注

该成员函数返回存储的 BCL 接口句柄。

示例

// 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

指定受控序列的开头。

语法

iterator begin();

备注

该成员函数返回一个输入迭代器,指定受控序列的第一个元素,或刚超出空序列末尾的位置。

示例

// 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

构造适配器对象。

语法

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

参数

collection
要包装的 BCL 句柄。

right
要复制的对象。

备注

构造函数:

collection_adapter();

使用 nullptr 初始化存储的句柄。

构造函数:

collection_adapter(collection_adapter<Coll>% right);

使用 right.base() 初始化存储的句柄。

构造函数:

collection_adapter(collection_adapter<Coll>^ right);

使用 right->base() 初始化存储的句柄。

构造函数:

collection_adapter(Coll^ collection);

使用 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

collection_adapter::difference_type

两个元素间的带符号距离的类型。

语法

typedef int difference_type;

备注

该类型描述有符号元素计数。

示例

// 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

指定受控序列的末尾。

语法

iterator end();

备注

该成员函数返回一个输入迭代器,指向刚超出受控序列末尾的位置。

示例

// 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

受控序列的迭代器的类型。

语法

typedef T1 iterator;

备注

该类型描述未指定类型 T1 的对象,该对象可充当受控序列的输入迭代器。

示例

// 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

字典键的类型。

语法

typedef Key key_type;

备注

该类型是模板参数 Key 的同义词,用于 IDictionaryIDictionary<Value> 的专门化;否则未定义。

示例

// 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

字典值的类型。

语法

typedef Value mapped_type;

备注

该类型是模板参数 Value 的同义词,用于 IDictionaryIDictionary<Value> 的专门化;否则未定义。

示例

// 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=

替换存储的 BCL 句柄。

语法

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

参数

right
要复制的适配器。

备注

成员运算符将 right 复制到对象,然后返回 *this。 可使用它在 right 中将存储的 BCL 句柄替换为存储的 BCL 句柄的副本。

示例

// 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

元素的引用的类型。

语法

typedef value_type% reference;

备注

该类型描述了对元素的引用。

示例

// 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

对元素数进行计数。

语法

size_type size();

备注

成员函数将返回受控序列的长度。 它不是在 IEnumerableIEnumerable<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

collection_adapter::size_type

两个元素间的带符号距离的类型。

语法

typedef int size_type;

备注

该类型描述非负元素计数。

示例

// 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

交换两个容器的内容。

语法

void swap(collection_adapter<Coll>% right);

参数

right
要与其交换内容的容器。

注解

该成员函数在 *thisright 之间交换存储的 BCL 句柄。

示例

// 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

元素的类型。

语法

typedef Value value_type;

备注

该类型是模板参数 Value 的同义词(如果存在于专门化中);否则它是 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_collection (STL/CLR)

从迭代器对生成一个 range_adapter

语法

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

参数

Iter
已包装迭代器的类型。

first
要包装的第一个迭代器。

last
要包装的第二个迭代器。

注解

函数模板返回 gcnew range_adapter<Iter>(first, last)。 可使用它从一对迭代器构造一个 range_adapter<Iter> 对象。

示例

// 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)

一个模板类,可包装一对迭代器,用于实现多个基类库 (BCL) 接口。 可使用 range_adapter 来操作 STL/CLR 范围,如同操作 BCL 集合。

语法

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
与包装的迭代器关联的类型。

成员

成员函数 说明
range_adapter::range_adapter 构造适配器对象。
操作员 说明
range_adapter::operator= 替换存储的迭代器对。

接口

接口 说明
IEnumerable 循环访问集合中的元素。
ICollection 维护元素组。
IEnumerable<T> 循环访问集合中的类型化元素。
ICollection<T> 维护类型化元素组。

备注

range_adapter 存储了一对迭代器,后者又界定了一个元素序列。 该对象实现四个 BCL 接口,使你可以按顺序循环访问元素。 可使用此模板类操作 STL/CLR 范围,如同操作 BCL 容器。

range_adapter::operator=

替换存储的迭代器对。

语法

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

参数

right
要复制的适配器。

备注

成员运算符将 right 复制到对象,然后返回 *this。 可使用它在 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

range_adapter::range_adapter

构造适配器对象。

语法

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

参数

first
要包装的第一个迭代器。

last
要包装的第二个迭代器。

right
要复制的对象。

注解

构造函数:

range_adapter();

使用默认构造的迭代器初始化存储的迭代器对。

构造函数:

range_adapter(range_adapter<Iter>% right);

通过复制存储在 right 中的对来初始化存储的迭代器对。

构造函数:

range_adapter(range_adapter<Iter>^ right);

通过复制存储在 *right 中的对来初始化存储的迭代器对。

构造函数:

range_adapter(Iter^ first, last);

使用 firstlast 初始化存储的迭代器对。

示例

// 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