map (STL/CLR)

此模板类描述了一个对象,该对象控制一个具有双向访问权限且长度可变的元素序列。 使用容器 map 将元素序列作为(几乎)平衡有序的节点树进行管理,每个节点存储一个元素。 元素由用于对序列进行排序的键,以及随附的映射值组成。

在下面的描述中,GValue 等同于:

Microsoft::VisualC::StlClr::GenericPair<GKey, GMapped>

其中:

GKeyKey 相同,除非后者是 ref 类型,在这种情况下它是 Key^

GMappedMapped 相同,除非后者是 ref 类型,在这种情况下它是 Mapped^

语法

template<typename Key,
    typename Mapped>
    ref class map
        :   public
        System::ICloneable,
        System::Collections::IEnumerable,
        System::Collections::ICollection,
        System::Collections::Generic::IEnumerable<GValue>,
        System::Collections::Generic::ICollection<GValue>,
        System::Collections::Generic::IList<GValue>,
        System::Collections::Generic::IDictionary<Gkey, GMapped>,
        Microsoft::VisualC::StlClr::ITree<Gkey, GValue>
    { ..... };

参数

Key
受控序列中元素的键组件的类型。

Mapped
受控序列中元素的其他组件的类型。

要求

标头:<cliext/map>

命名空间:cliext

声明

类型定义 说明
map::const_iterator 受控序列的常量迭代器的类型。
map::const_reference 元素的常量引用的类型。
map::const_reverse_iterator 受控序列的常量反向迭代器的类型。
map::difference_type 两个元素间的(可能带符号)距离的类型。
map::generic_container 容器的泛型接口的类型。
map::generic_iterator 容器的泛型接口的迭代器的类型。
map::generic_reverse_iterator 容器的泛型接口的反向迭代器的类型。
map::generic_value 容器的泛型接口的元素类型。
map::iterator 受控序列的迭代器的类型。
map::key_compare 两个键的排序委托。
map::key_type 排序键的类型。
map::mapped_type 与每个键关联的映射值的类型。
map::reference 元素的引用的类型。
map::reverse_iterator 受控序列的反向迭代器的类型。
map::size_type 两个元素间的(非负)距离的类型。
map::value_compare 两个元素值的排序委托。
map::value_type 元素的类型。
成员函数 说明
map::begin 指定受控序列的开头。
map::clear 删除所有元素。
map::count 对与指定键匹配的元素进行计数。
map::empty 测试元素是否存在。
map::end 指定受控序列的末尾。
map::equal_range 查找与指定键匹配的范围。
map::erase 移除指定位置处的元素。
map::find 查找与指定键匹配的元素。
map::insert 添加元素。
map::key_comp 复制两个键的排序委托。
map::lower_bound 查找与指定键匹配的范围的开头。
map::make_value 构造值对象。
map::map 构造容器对象。
map::rbegin 指定反向受控序列的开头。
map::rend 指定反向受控序列的末尾。
map::size 对元素数进行计数。
map::swap 交换两个容器的内容。
map::to_array 将受控序列复制到新数组。
map::upper_bound 查找与指定键匹配的范围的末尾。
map::value_comp 复制两个元素值的排序委托。
运算符 说明
map::operator= 替换受控序列。
map::operator[] 将键映射到其关联的映射值。
operator!=(映射) 确定 map 对象是否不等于另一个 map 对象。
operator<(映射) 确定 map 对象是否小于另一个 map 对象。
operator<=(映射) 确定 map 对象是否小于或等于另一个 map 对象。
operator==(映射) 确定 map 对象是否等于另一个 map 对象。
operator>(映射) 确定 map 对象是否大于另一个 map 对象。
operator>=(映射) 确定 map 对象是否大于或等于另一个 map 对象。

接口

接口 说明
ICloneable 复制对象。
IEnumerable 对元素进行排序。
ICollection 维护元素组。
IEnumerable<T> 对类型化元素进行排序。
ICollection<T> 维护类型化元素组。
IDictionary<TKey,TValue> 维护 {键, 值} 对的组。
ITree<Key, Value> 维护泛型容器。

备注

对象为它作为单个节点控制的序列分配和释放存储。 它将元素插入到(几乎)平衡的树中,通过更改节点之间的链接来保持有序,而不是通过将一个节点的内容复制到另一个节点。 这意味着你可以自由插入和移除元素,不会干扰到其余的元素。

对象通过调用类型为 map::key_compare 的存储委托对象对其控制的序列进行排序。 构造 map 时,可以指定存储的委托对象;如果未指定委托对象,则默认值是比较 operator<(key_type, key_type)。 需要通过调用成员函数 map::key_comp 来访问此存储对象。

此类委托对象必须对 map::key_type 类型的键实施严格的弱排序。 这意味着,对于任意两个键 XY

key_comp()(X, Y) 在每次调用时返回相同的布尔结果。

如果 key_comp()(X, Y) 为 true,则 key_comp()(Y, X) 必须为 false。

如果 key_comp()(X, Y) 为 true,则 X 被认为在 Y 之前排序。

如果 !key_comp()(X, Y) && !key_comp()(Y, X) 为 true,则 XY 被认为具有等效顺序。

对于受控序列中 Y 之前的任何元素 Xkey_comp()(Y, X) 为 false。 (对于默认委托对象,键值永远不会减少。)与模板类 map 不同,模板类 map 的对象不需要所有元素的键都是唯一的。 (两个或两个以上的键可以具有等效的顺序。)

每个元素都包含一个单独的键和一个映射的值。 序列以允许在对数时间中查找、插入和删除任意元素的方式表示。 也就是说,运算数与序列中元素数的对数成正比。 此外,插入元素不会使迭代器失效,移除元素仅会使指向已移除元素的迭代器失效。

map 支持双向迭代器,这意味着你可以通过指定受控序列中某个元素的迭代器来单步执行相邻元素。 有一个特殊的头节点对应于 end() 返回的迭代器。 可以递减此迭代器,以到达受控序列中的最后一个元素(如果存在)。 可以递增 map 迭代器以到达头节点,然后它与 end() 相等。 但你不能取消引用 end() 返回的迭代器。

不能在给定其数值位置的情况下直接引用 map 元素。 这需要一个随机访问迭代器。

map 迭代器将句柄存储到其关联的 map 节点,该节点又将句柄存储到其关联的容器。 只能将迭代器与其关联的容器对象一起使用。 只要关联的 map 节点与某些映射关联,map 迭代器就保持有效。 此外,有效的迭代器可取消引用。 只要所指定的元素值不等于 end(),就可以使用该迭代器对其进行访问或更改。

擦除或移除元素会为其存储的值调用析构函数。 销毁容器会擦除所有元素。 因此,元素类型为 ref 类的容器可确保没有元素的生存期长于容器。 但是,句柄的容器不会销毁其元素。

成员

map::begin

指定受控序列的开头。

语法

iterator begin();

备注

该成员函数返回一个双向迭代器,指定受控序列的第一个元素,或刚超出空序列末尾的位置。 用于获取一个迭代器,该迭代器指定受控序列的 current 开头,但如果受控序列的长度发生更改,则该迭代器的状态也会发生更改。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect first two items
    Mymap::iterator it = c1.begin();
    System::Console::WriteLine("*begin() = [{0} {1}]",
        it->first, it->second);
    ++it;
    System::Console::WriteLine("*++begin() = [{0} {1}]",
        it->first, it->second);
    return (0);
    }
[a 1] [b 2] [c 3]
*begin() = [a 1]
*++begin() = [b 2]

map::clear

删除所有元素。

语法

void clear();

备注

该成员函数有效调用 erase(begin(), end())。 用于确保受控序列为空。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // clear the container and reinspect
    c1.clear();
    System::Console::WriteLine("size() = {0}", c1.size());

    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));

    // display contents " [a 1] [b 2]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    c1.clear();
    System::Console::WriteLine("size() = {0}", c1.size());
    return (0);
    }
[a 1] [b 2] [c 3]
size() = 0
[a 1] [b 2]
size() = 0

map::const_iterator

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

语法

typedef T2 const_iterator;

备注

该类型描述了可充当受控序列的常量双向迭代器的未指定类型 T2 的对象。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    Mymap::const_iterator cit = c1.begin();
    for (; cit != c1.end(); ++cit)
        System::Console::Write("[{0} {1}] ", cit->first, cit->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

map::const_reference

元素的常量引用的类型。

语法

typedef value_type% const_reference;

备注

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

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    Mymap::const_iterator cit = c1.begin();
    for (; cit != c1.end(); ++cit)
        {   // get a const reference to an element
        Mymap::const_reference cref = *cit;
        System::Console::Write("[{0} {1}] ", cref->first, cref->second);
        }
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

map::const_reverse_iterator

受控序列的常量反向迭代器的类型。

语法

typedef T4 const_reverse_iterator;

备注

该类型描述了可充当受控序列的常量反向迭代器的未指定类型 T4 的对象。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]" reversed
    Mymap::const_reverse_iterator crit = c1.rbegin();
    for (; crit != c1.rend(); ++crit)
        System::Console::Write("[{0} {1}] ", crit->first, crit->second);
    System::Console::WriteLine();
    return (0);
    }
[c 3] [b 2] [a 1]

map::count

查找与指定键匹配的元素数。

语法

size_type count(key_type key);

参数

key
要搜索的键值。

注解

该成员函数返回受控序列中与 key 具有等效顺序的元素的数量。 用于确定受控序列中当前与指定键匹配的元素数。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("count(L'A') = {0}", c1.count(L'A'));
    System::Console::WriteLine("count(L'b') = {0}", c1.count(L'b'));
    System::Console::WriteLine("count(L'C') = {0}", c1.count(L'C'));
    return (0);
    }
[a 1] [b 2] [c 3]
count(L'A') = 0
count(L'b') = 1
count(L'C') = 0

map::difference_type

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

语法

typedef int difference_type;

备注

该类型描述了可能为负的元素计数。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

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

    // compute negative difference
    diff = 0;
    for (Mymap::iterator it = c1.end(); it != c1.begin(); --it)
        --diff;
    System::Console::WriteLine("begin()-end() = {0}", diff);
    return (0);
    }
[a 1] [b 2] [c 3]
end()-begin() = 3
begin()-end() = -3

map::empty

测试元素是否存在。

语法

bool empty();

备注

对于空受控序列,该成员函数返回 true。 它等效于 size() == 0。 用于测试 map 是否为空。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    System::Console::WriteLine("size() = {0}", c1.size());
    System::Console::WriteLine("empty() = {0}", c1.empty());

    // clear the container and reinspect
    c1.clear();
    System::Console::WriteLine("size() = {0}", c1.size());
    System::Console::WriteLine("empty() = {0}", c1.empty());
    return (0);
    }
[a 1] [b 2] [c 3]
size() = 3
empty() = False
size() = 0
empty() = True

map::end

指定受控序列的末尾。

语法

iterator end();

备注

该成员函数返回一个双向迭代器,指向刚超出受控序列末尾的位置。 用于获取一个迭代器,该迭代器指定受控序列的末尾;如果受控序列的长度改变,其状态不会改变。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect last two items
    Mymap::iterator it = c1.end();
    --it;
    --it;
    System::Console::WriteLine("*-- --end() = [{0} {1}]",
        it->first, it->second);
    ++it;
    System::Console::WriteLine("*--end() = [{0} {1}]",
        it->first, it->second);
    return (0);
    }

map::equal_range

查找与指定键匹配的范围。

语法

cliext::pair<iterator, iterator> equal_range(key_type key);

参数

key
要搜索的键值。

注解

成员函数返回一对迭代器 cliext::pair<iterator, iterator>(lower_bound(key), upper_bound(key))。 用于确定受控序列中当前与指定键匹配的元素范围。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
typedef Mymap::pair_iter_iter Pairii;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // display results of failed search
    Pairii pair1 = c1.equal_range(L'x');
    System::Console::WriteLine("equal_range(L'x') empty = {0}",
        pair1.first == pair1.second);

    // display results of successful search
    pair1 = c1.equal_range(L'b');
    for (; pair1.first != pair1.second; ++pair1.first)
        System::Console::Write("[{0} {1}] ",
            pair1.first->first, pair1.first->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
equal_range(L'x') empty = True
[b 2]

map::erase

移除指定位置处的元素。

语法

iterator erase(iterator where);
iterator erase(iterator first, iterator last);
bool erase(key_type key)

参数

first
要擦除的范围的开头。

key
要擦除的键值。

last
要擦除的范围的末尾。

where
要擦除的元素。

注解

第一个成员函数移除 where 所指向的受控序列的元素,并返回一个迭代器,该迭代器指定除移除的元素之外的第一个元素,如果不存在这样的元素,则指定 end()。 用于删除单个元素。

第二个成员函数移除范围 [first, last] 中的受控序列的元素,并返回一个迭代器,该迭代器指定除任何移除的元素之外的第一个元素,如果不存在这样的元素,则指定 end()。 用于移除零个或多个连续元素。

第三个成员函数移除受控序列中其键与 key 具有等效顺序的任何元素,并返回移除的元素数的计数。 用于移除与指定键匹配的所有元素并对其进行计数。

每个元素擦除所需的时间与受控序列中元素数的对数成正比。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    cliext::map<wchar_t, int> c1;
    c1.insert(cliext::map<wchar_t, int>::make_value(L'a', 1));
    c1.insert(cliext::map<wchar_t, int>::make_value(L'b', 2));
    c1.insert(cliext::map<wchar_t, int>::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (cliext::map<wchar_t, int>::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // erase an element and reinspect
    cliext::map<wchar_t, int>::iterator it =
        c1.erase(c1.begin());
    System::Console::WriteLine("erase(begin()) = [{0} {1}]",
        it->first, it->second);

    // add elements and display " b c d e"
    c1.insert(cliext::map<wchar_t, int>::make_value(L'd', 4));
    c1.insert(cliext::map<wchar_t, int>::make_value(L'e', 5));
    for each (cliext::map<wchar_t, int>::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // erase all but end
    it = c1.end();
    it = c1.erase(c1.begin(), --it);
    System::Console::WriteLine("erase(begin(), end()-1) = [{0} {1}]",
        it->first, it->second);
    System::Console::WriteLine("size() = {0}", c1.size());

    // erase end
    System::Console::WriteLine("erase(L'x') = {0}", c1.erase(L'x'));
    System::Console::WriteLine("erase(L'e') = {0}", c1.erase(L'e'));
    return (0);
    }
[a 1] [b 2] [c 3]
erase(begin()) = [b 2]
[b 2] [c 3] [d 4] [e 5]
erase(begin(), end()-1) = [e 5]
size() = 1
erase(L'x') = 0
erase(L'e') = 1

map::find

查找与指定键匹配的元素。

语法

iterator find(key_type key);

参数

key
要搜索的键值。

备注

如果受控序列中的至少一个元素具有与 key 等效的顺序,该成员函数将返回指定其中一个元素的迭代器;否则返回 end()。 用于定位受控序列中当前与指定键匹配的元素。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("find {0} = {1}",
        L'A', c1.find(L'A') != c1.end());

    Mymap::iterator it = c1.find(L'b');
    System::Console::WriteLine("find {0} = [{1} {2}]",
        L'b', it->first, it->second);

    System::Console::WriteLine("find {0} = {1}",
        L'C', c1.find(L'C') != c1.end());
    return (0);
    }
[a 1] [b 2] [c 3]
find A = False
find b = [b 2]
find C = False

map::generic_container

容器的泛型接口的类型。

语法

typedef Microsoft::VisualC::StlClr::
    ITree<GKey, GValue>
    generic_container;

备注

该类型描述此模板容器类的泛型接口。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct a generic container
    Mymap::generic_container^ gc1 = %c1;
    for each (Mymap::value_type elem in gc1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // modify generic and display original
    gc1->insert(Mymap::make_value(L'd', 4));
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // modify original and display generic
    c1.insert(Mymap::make_value(L'e', 5));
    for each (Mymap::value_type elem in gc1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3] [d 4]
[a 1] [b 2] [c 3] [d 4] [e 5]

map::generic_iterator

要与容器的泛型接口一起使用的迭代器的类型。

语法

typedef Microsoft::VisualC::StlClr::Generic::
    ContainerBidirectionalIterator<generic_value>
    generic_iterator;

备注

该类型描述可与此模板容器类的泛型接口一起使用的泛型迭代器。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct a generic container
    Mymap::generic_container^ gc1 = %c1;
    for each (Mymap::value_type elem in gc1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // get an element and display it
    Mymap::generic_iterator gcit = gc1->begin();
    Mymap::generic_value gcval = *gcit;
    System::Console::Write("[{0} {1}] ", gcval->first, gcval->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]
[a 1]

map::generic_reverse_iterator

要与容器的泛型接口一起使用的反向迭代器的类型。

语法

typedef Microsoft::VisualC::StlClr::Generic::
    ReverseRandomAccessIterator<generic_value>
    generic_reverse_iterator;

备注

该类型描述可与此模板容器类的泛型接口一起使用的泛型反向迭代器。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct a generic container
    Mymap::generic_container^ gc1 = %c1;
    for each (Mymap::value_type elem in gc1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // get an element and display it
    Mymap::generic_reverse_iterator gcit = gc1->rbegin();
    Mymap::generic_value gcval = *gcit;
    System::Console::WriteLine("[{0} {1}] ", gcval->first, gcval->second);
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]
[c 3]

map::generic_value

要与容器的泛型接口一起使用的元素的类型。

语法

typedef GValue generic_value;

备注

该类型描述一个 GValue 类型的对象,该对象描述可与此模板容器类的泛型接口一起使用的存储元素值。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct a generic container
    Mymap::generic_container^ gc1 = %c1;
    for each (Mymap::value_type elem in gc1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // get an element and display it
    Mymap::generic_iterator gcit = gc1->begin();
    Mymap::generic_value gcval = *gcit;
    System::Console::WriteLine("[{0} {1}] ", gcval->first, gcval->second);
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]
[a 1]

map::insert

添加元素。

语法

cliext::pair<iterator, bool> insert(value_type val);
iterator insert(iterator where, value_type val);
template<typename InIter>
    void insert(InIter first, InIter last);
void insert(System::Collections::Generic::IEnumerable<value_type>^ right);

参数

first
要插入的范围的开头。

last
要插入的范围的末尾。

right
要插入的枚举。

val
要插入的键值。

where
容器中要插入的位置(仅提示)。

注解

每个成员函数都插入了一个由其余操作数指定的序列。

第一个成员函数尝试插入一个值为 val 的元素,并返回一对 X 值。 如果 X.second 为 true,则 X.first 指定新插入的元素;否则 X.first 指定一个具有等效顺序的现有元素,不插入新元素。 可以用它来插入单个元素。

第二个成员函数插入一个值为 val 的元素,使用 where 作为提示(来提高性能),并返回一个指定新插入的元素的迭代器。 可以使用它插入单个元素,该元素可能位于已知元素旁边。

第三个成员函数插入序列 [first, last)。 可以用它来插入从另一个序列复制的零个或多个元素。

第四个成员函数插入由 right 指定的序列。 用于插入枚举器所描述的序列。

每个元素插入所需的时间与受控序列中元素数的对数成正比。 但是,如果系统提示在插入点旁边指定一个元素,则插入可以在摊销的恒定时间内发生。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
typedef Mymap::pair_iter_bool Pairib;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // insert a single value, unique and duplicate
// insert a single value, success and failure
    Pairib pair1 = c1.insert(Mymap::make_value(L'x', 24));
    System::Console::WriteLine("insert([L'x' 24]) = [[{0} {1}] {2}]",
        pair1.first->first, pair1.first->second, pair1.second);

    pair1 = c1.insert(Mymap::make_value(L'b', 2));
    System::Console::WriteLine("insert([L'b' 2]) = [[{0} {1}] {2}]",
        pair1.first->first, pair1.first->second, pair1.second);

    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // insert a single value with hint
    Mymap::iterator it =
        c1.insert(c1.begin(), Mymap::make_value(L'y', 25));
    System::Console::WriteLine("insert(begin(), [L'y' 25]) = [{0} {1}]",
        it->first, it->second);
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // insert an iterator range
    Mymap c2;
    it = c1.end();
    c2.insert(c1.begin(), --it);
    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // insert an enumeration
    Mymap c3;
    c3.insert(   // NOTE: cast is not needed
        (System::Collections::Generic::
            IEnumerable<Mymap::value_type>^)%c1);
    for each (Mymap::value_type elem in c3)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
insert([L'x' 24]) = [[x 24] True]
insert([L'b' 2]) = [[b 2] False]
[a 1] [b 2] [c 3] [x 24]
insert(begin(), [L'y' 25]) = [y 25]
[a 1] [b 2] [c 3] [x 24] [y 25]
[a 1] [b 2] [c 3] [x 24]
[a 1] [b 2] [c 3] [x 24] [y 25]

map::iterator

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

语法

typedef T1 iterator;

备注

该类型描述了可充当受控序列的双向迭代器的未指定类型 T1 的对象。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    Mymap::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        System::Console::Write("[{0} {1}] ", it->first, it->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

map::key_comp

复制两个键的排序委托。

语法

key_compare^key_comp();

备注

该成员函数返回用于对受控序列进行排序的排序委托。 用于对两个键进行比较。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    Mymap::key_compare^ kcomp = c1.key_comp();

    System::Console::WriteLine("compare(L'a', L'a') = {0}",
        kcomp(L'a', L'a'));
    System::Console::WriteLine("compare(L'a', L'b') = {0}",
        kcomp(L'a', L'b'));
    System::Console::WriteLine("compare(L'b', L'a') = {0}",
        kcomp(L'b', L'a'));
    System::Console::WriteLine();

    // test a different ordering rule
    Mymap c2 = cliext::greater<wchar_t>();
    kcomp = c2.key_comp();

    System::Console::WriteLine("compare(L'a', L'a') = {0}",
        kcomp(L'a', L'a'));
    System::Console::WriteLine("compare(L'a', L'b') = {0}",
        kcomp(L'a', L'b'));
    System::Console::WriteLine("compare(L'b', L'a') = {0}",
        kcomp(L'b', L'a'));
    return (0);
    }
compare(L'a', L'a') = False
compare(L'a', L'b') = True
compare(L'b', L'a') = False

compare(L'a', L'a') = False
compare(L'a', L'b') = False
compare(L'b', L'a') = True

map::key_compare

两个键的排序委托。

语法

Microsoft::VisualC::StlClr::BinaryDelegate<GKey, GKey, bool>
    key_compare;

备注

该类型是委托的同义词,用于确定其键参数的顺序。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    Mymap::key_compare^ kcomp = c1.key_comp();

    System::Console::WriteLine("compare(L'a', L'a') = {0}",
        kcomp(L'a', L'a'));
    System::Console::WriteLine("compare(L'a', L'b') = {0}",
        kcomp(L'a', L'b'));
    System::Console::WriteLine("compare(L'b', L'a') = {0}",
        kcomp(L'b', L'a'));
    System::Console::WriteLine();

    // test a different ordering rule
    Mymap c2 = cliext::greater<wchar_t>();
    kcomp = c2.key_comp();

    System::Console::WriteLine("compare(L'a', L'a') = {0}",
        kcomp(L'a', L'a'));
    System::Console::WriteLine("compare(L'a', L'b') = {0}",
        kcomp(L'a', L'b'));
    System::Console::WriteLine("compare(L'b', L'a') = {0}",
        kcomp(L'b', L'a'));
    return (0);
    }
compare(L'a', L'a') = False
compare(L'a', L'b') = True
compare(L'b', L'a') = False

compare(L'a', L'a') = False
compare(L'a', L'b') = False
compare(L'b', L'a') = True

map::key_type

排序键的类型。

语法

typedef Key key_type;

备注

该类型是模板参数 Key 的同义词。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]" using key_type
    for (Mymap::iterator it = c1.begin(); it != c1.end(); ++it)
        {   // store element in key_type object
        Mymap::key_type val = it->first;

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

map::lower_bound

查找与指定键匹配的范围的开头。

语法

iterator lower_bound(key_type key);

参数

key
要搜索的键值。

备注

该成员函数确定受控序列中与 key 具有等效顺序的第一个元素 X。 如果没有这样的元素,则返回 end();否则返回用于指定 X 的迭代器。 可以用它来定位受控序列中当前与指定键匹配的元素的序列的开头。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("lower_bound(L'x')==end() = {0}",
        c1.lower_bound(L'x') == c1.end());

    Mymap::iterator it = c1.lower_bound(L'a');
    System::Console::WriteLine("*lower_bound(L'a') = [{0} {1}]",
        it->first, it->second);
    it = c1.lower_bound(L'b');
    System::Console::WriteLine("*lower_bound(L'b') = [{0} {1}]",
        it->first, it->second);
    return (0);
    }
[a 1] [b 2] [c 3]
lower_bound(L'x')==end() = True
*lower_bound(L'a') = [a 1]
*lower_bound(L'b') = [b 2]

map::make_value

构造值对象。

语法

static value_type make_value(key_type key, mapped_type mapped);

参数

key
要使用的键值。

mapped
要搜索的映射值。

备注

该成员函数返回一个 value_type 对象,其键是 key,其映射值是 mapped。 可以用它来编写适合与多个其他成员函数一起使用的对象。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

map::map

构造容器对象。

语法

map();
explicit map(key_compare^ pred);
map(map<Key, Mapped>% right);
map(map<Key, Mapped>^ right);
template<typename InIter>
    mapmap(InIter first, InIter last);
template<typename InIter>
    map(InIter first, InIter last,
        key_compare^ pred);
map(System::Collections::Generic::IEnumerable<GValue>^ right);
map(System::Collections::Generic::IEnumerable<GValue>^ right,
    key_compare^ pred);

参数

first
要插入的范围的开头。

last
要插入的范围的末尾。

pred
受控序列的排序谓词。

right
要插入的对象或范围。

备注

构造函数:

map();

初始化没有元素、有默认排序谓词 key_compare() 的受控序列。 可以用它来指定一个具有默认排序谓词的空的初始受控序列。

构造函数:

explicit map(key_compare^ pred);

初始化没有元素、有排序谓词 pred 的受控序列。 用于指定一个具有指定排序谓词的空的初始受控序列。

构造函数:

map(map<Key, Mapped>% right);

初始化有序列 [right.begin(), right.end()) 和默认排序谓词的受控序列。 可以用它来指定一个初始受控序列,它是由 map 对象 right 控制的序列的副本,使用默认排序谓词。

构造函数:

map(map<Key, Mapped>^ right);

初始化有序列 [right->begin(), right->end()) 和默认排序谓词的受控序列。 可以用它来指定一个初始受控序列,它是由 map 对象 right 控制的序列的副本,使用默认排序谓词。

构造函数:

template<typename InIter> map(InIter first, InIter last);

初始化有序列 [first, last) 和默认排序谓词的受控序列。 可以用它来使受控序列成为另一个序列的副本,并使用默认排序谓词。

构造函数:

template<typename InIter> map(InIter first, InIter last, key_compare^ pred);

初始化有序列 [first, last] 和排序谓词 pred 的受控序列。 可以用它来使受控序列成为另一个序列的副本,并使用指定的排序谓词。

构造函数:

map(System::Collections::Generic::IEnumerable<Key>^ right);

初始化有枚举器 right 指定的序列和默认排序谓词的受控序列。 可以用它来使受控序列成为枚举器所描述的另一个序列的副本,并使用默认排序谓词。

构造函数:

map(System::Collections::Generic::IEnumerable<Key>^ right, key_compare^ pred);

初始化有枚举器 right 指定的序列和排序谓词 pred 的受控序列。 可以用它来使受控序列成为枚举器所描述的另一个序列的副本,并使用指定的排序谓词。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
// construct an empty container
    Mymap c1;
    System::Console::WriteLine("size() = {0}", c1.size());

    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an ordering rule
    Mymap c2 = cliext::greater_equal<wchar_t>();
    System::Console::WriteLine("size() = {0}", c2.size());

    c2.insert(c1.begin(), c1.end());
    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an iterator range
    Mymap c3(c1.begin(), c1.end());
    for each (Mymap::value_type elem in c3)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an iterator range and an ordering rule
    Mymap c4(c1.begin(), c1.end(),
        cliext::greater_equal<wchar_t>());
    for each (Mymap::value_type elem in c4)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an enumeration
    Mymap c5(   // NOTE: cast is not needed
        (System::Collections::Generic::IEnumerable<
            Mymap::value_type>^)%c3);
    for each (Mymap::value_type elem in c5)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an enumeration and an ordering rule
    Mymap c6(   // NOTE: cast is not needed
        (System::Collections::Generic::IEnumerable<
            Mymap::value_type>^)%c3,
                cliext::greater_equal<wchar_t>());
    for each (Mymap::value_type elem in c6)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct by copying another container
    Mymap c7(c4);
    for each (Mymap::value_type elem in c7)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct by copying a container handle
    Mymap c8(%c3);
    for each (Mymap::value_type elem in c8)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
size() = 0
[a 1] [b 2] [c 3]
size() = 0
[c 3] [b 2] [a 1]
[a 1] [b 2] [c 3]
[c 3] [b 2] [a 1]
[a 1] [b 2] [c 3]
[c 3] [b 2] [a 1]
[c 3] [b 2] [a 1]
[a 1] [b 2] [c 3]

map::mapped_type

与每个键关联的映射值的类型。

语法

typedef Mapped mapped_type;

备注

该类型是模板参数 Mapped 的同义词。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]" using mapped_type
    for (Mymap::iterator it = c1.begin(); it != c1.end(); ++it)
        {   // store element in mapped_type object
        Mymap::mapped_type val = it->second;

        System::Console::Write("{0} ", val);
        }
    System::Console::WriteLine();
    return (0);
    }
1 2 3

map::operator=

替换受控序列。

语法

map<Key, Mapped>% operator=(map<Key, Mapped>% right);

参数

right
用于复制的容器。

注解

成员运算符将 right 复制到对象,然后返回 *this。 用它将受控序列替换为 right 中的受控序列的副本。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // assign to a new container
    Mymap c2;
    c2 = c1;
    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]

map::operator[]

将键映射到其关联的映射值。

语法

mapped_type operator[](key_type key);

参数

key
要搜索的键值。

备注

该成员函数尝试查找与 key 具有等效顺序的元素。 如果找到一个元素,则返回关联的映射值;否则,将插入 value_type(key, mapped_type()) 并返回关联的(默认)映射值。 用于在给定其关联键的情况下查找映射值,或确保在找不到任何值时该键存在条目。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("c1[{0}] = {1}",
        L'A', c1[L'A']);
    System::Console::WriteLine("c1[{0}] = {1}",
        L'b', c1[L'b']);

    // redisplay altered contents
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // alter mapped values and redisplay
    c1[L'A'] = 10;
    c1[L'c'] = 13;
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
c1[A] = 0
c1[b] = 2
[A 0] [a 1] [b 2] [c 3]
[A 10] [a 1] [b 2] [c 13]

map::rbegin

指定反向受控序列的开头。

语法

reverse_iterator rbegin();

备注

该成员函数返回一个反向迭代器,指定受控序列的最后一个元素,或刚超出空序列开头的位置。 因此,它指定反向序列的 beginning。 用于获取一个迭代器,该迭代器指定按相反顺序显示的受控序列的 current 开头,但如果受控序列的长度发生更改,则该迭代器的状态也会发生更改。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect first two items in reversed sequence
    Mymap::reverse_iterator rit = c1.rbegin();
    System::Console::WriteLine("*rbegin() = [{0} {1}]",
        rit->first, rit->second);
    ++rit;
    System::Console::WriteLine("*++rbegin() = [{0} {1}]",
        rit->first, rit->second);
    return (0);
    }
[a 1] [b 2] [c 3]
*rbegin() = [c 3]
*++rbegin() = [b 2]

map::reference

元素的引用的类型。

语法

typedef value_type% reference;

备注

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

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    Mymap::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        {   // get a reference to an element
        Mymap::reference ref = *it;
        System::Console::Write("[{0} {1}] ", ref->first, ref->second);
        }
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

map::rend

指定反向受控序列的末尾。

语法

reverse_iterator rend();

备注

该成员函数返回一个反向迭代器,指向刚超出受控序列开头的位置。 因此,它指定反向序列的 end。 用于获取一个迭代器,该迭代器指定按反向顺序显示的受控序列的 current 末尾,但如果受控序列的长度发生更改,则该迭代器的状态也会发生更改。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect first two items in reversed sequence
    Mymap::reverse_iterator rit = c1.rend();
    --rit;
    --rit;
    System::Console::WriteLine("*-- --rend() = [{0} {1}]",
        rit->first, rit->second);
    ++rit;
    System::Console::WriteLine("*--rend() = [{0} {1}]",
        rit->first, rit->second);
    return (0);
    }
[a 1] [b 2] [c 3]
*-- --rend() = [b 2]
*--rend() = [a 1]

map::reverse_iterator

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

语法

typedef T3 reverse_iterator;

备注

该类型描述了可充当受控序列的反向迭代器的未指定类型 T3 的对象。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]" reversed
    Mymap::reverse_iterator rit = c1.rbegin();
    for (; rit != c1.rend(); ++rit)
        System::Console::Write("[{0} {1}] ", rit->first, rit->second);
    System::Console::WriteLine();
    return (0);
    }
[c 3] [b 2] [a 1]

map::size

对元素数进行计数。

语法

size_type size();

备注

成员函数将返回受控序列的长度。 用于确定受控序列中的当前元素数。 如果只关心序列是否具有非零大小,请参阅 empty()

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // clear the container and reinspect
    c1.clear();
    System::Console::WriteLine("size() = {0} after clearing", c1.size());

    // add elements and clear again
    c1.insert(Mymap::make_value(L'd', 4));
    c1.insert(Mymap::make_value(L'e', 5));
    System::Console::WriteLine("size() = {0} after adding 2", c1.size());
    return (0);
    }
[a 1] [b 2] [c 3]
size() = 0 after clearing
size() = 2 after adding 2

map::size_type

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

语法

typedef int size_type;

备注

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

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

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

map::swap

交换两个容器的内容。

语法

void swap(map<Key, Mapped>% right);

参数

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

注解

成员函数交换 thisright 之间的受控序列。 它在常量时间内执行此操作且不引发异常。 你可以将其用作交换两个容器的内容的快捷方式。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct another container with repetition of values
    Mymap c2;
    c2.insert(Mymap::make_value(L'd', 4));
    c2.insert(Mymap::make_value(L'e', 5));
    c2.insert(Mymap::make_value(L'f', 6));
    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // swap and redisplay
    c1.swap(c2);
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
[d 4] [e 5] [f 6]
[d 4] [e 5] [f 6]
[a 1] [b 2] [c 3]

map::to_array

将受控序列复制到新数组。

语法

cli::array<value_type>^ to_array();

备注

成员函数返回一个包含受控序列的数组。 用于以数组形式获取受控序列的副本。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // copy the container and modify it
    cli::array<Mymap::value_type>^ a1 = c1.to_array();

    c1.insert(Mymap::make_value(L'd', 4));
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // display the earlier array copy
    for each (Mymap::value_type elem in a1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3] [d 4]
[a 1] [b 2] [c 3]

map::upper_bound

查找与指定键匹配的范围的末尾。

语法

iterator upper_bound(key_type key);

参数

key
要搜索的键值。

备注

该成员函数确定受控序列中与 key 具有等效顺序的最后一个元素 X。 如果不存在这样的元素,或者如果 X 是受控序列中的最后一个元素,则返回 end();否则返回用于指定 X 之外的第一个元素的迭代器。 可以用它来定位受控序列中当前与指定键匹配的元素的序列的末尾。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("upper_bound(L'x')==end() = {0}",
        c1.upper_bound(L'x') == c1.end());

    Mymap::iterator it = c1.upper_bound(L'a');
    System::Console::WriteLine("*upper_bound(L'a') = [{0} {1}]",
        it->first, it->second);
    it = c1.upper_bound(L'b');
    System::Console::WriteLine("*upper_bound(L'b') = [{0} {1}]",
        it->first, it->second);
    return (0);
    }
[a 1] [b 2] [c 3]
upper_bound(L'x')==end() = True
*upper_bound(L'a') = [b 2]
*upper_bound(L'b') = [c 3]

map::value_comp

复制两个元素值的排序委托。

语法

value_compare^ value_comp();

备注

该成员函数返回用于对受控序列进行排序的排序委托。 可以用它来对两个元素值进行比较。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    Mymap::value_compare^ kcomp = c1.value_comp();

    System::Console::WriteLine("compare([L'a', 1], [L'a', 1]) = {0}",
        kcomp(Mymap::make_value(L'a', 1),
            Mymap::make_value(L'a', 1)));
    System::Console::WriteLine("compare([L'a', 1], [L'b', 2]) = {0}",
        kcomp(Mymap::make_value(L'a', 1),
            Mymap::make_value(L'b', 2)));
    System::Console::WriteLine("compare([L'b', 2], [L'a', 1]) = {0}",
        kcomp(Mymap::make_value(L'b', 2),
            Mymap::make_value(L'a', 1)));
    System::Console::WriteLine();
    return (0);
    }
compare([L'a', 1], [L'a', 1]) = False
compare([L'a', 1], [L'b', 2]) = True
compare([L'b', 2], [L'a', 1]) = False

map::value_compare

两个元素值的排序委托。

语法

Microsoft::VisualC::StlClr::BinaryDelegate<generic_value, generic_value, bool>
    value_compare;

备注

该类型是委托的同义词,用于确定其值参数的顺序。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    Mymap::value_compare^ kcomp = c1.value_comp();

    System::Console::WriteLine("compare([L'a', 1], [L'a', 1]) = {0}",
        kcomp(Mymap::make_value(L'a', 1),
            Mymap::make_value(L'a', 1)));
    System::Console::WriteLine("compare([L'a', 1], [L'b', 2]) = {0}",
        kcomp(Mymap::make_value(L'a', 1),
            Mymap::make_value(L'b', 2)));
    System::Console::WriteLine("compare([L'b', 2], [L'a', 1]) = {0}",
        kcomp(Mymap::make_value(L'b', 2),
            Mymap::make_value(L'a', 1)));
    System::Console::WriteLine();
    return (0);
    }
compare([L'a', 1], [L'a', 1]) = False
compare([L'a', 1], [L'b', 2]) = True
compare([L'b', 2], [L'a', 1]) = False

map::value_type

元素的类型。

语法

typedef generic_value value_type;

备注

类型是 generic_value 的同义词。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]" using value_type
    for (Mymap::iterator it = c1.begin(); it != c1.end(); ++it)
        {   // store element in value_type object
        Mymap::value_type val = *it;
        System::Console::Write("[{0} {1}] ", val->first, val->second);
        }
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

operator!= (map) (STL/CLR)

列表不等于比较。

语法

template<typename Key,
    typename Mapped>
    bool operator!=(map<Key, Mapped>% left,
        map<Key, Mapped>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

备注

运算符函数返回 !(left == right)。 当两个映射按元素逐个比较时,可以用它来测试 left 的顺序是否与 right 相同。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // assign to a new container
    Mymap c2;
    c2.insert(Mymap::make_value(L'a', 1));
    c2.insert(Mymap::make_value(L'b', 2));
    c2.insert(Mymap::make_value(L'd', 4));

    // display contents " [a 1] [b 2] [d 4]"
    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("[a b c] != [a b c] is {0}",
        c1 != c1);
    System::Console::WriteLine("[a b c] != [a b d] is {0}",
        c1 != c2);
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [d 4]
[a b c] != [a b c] is False
[a b c] != [a b d] is True

operator<(映射)

列表小于比较。

语法

template<typename Key,
    typename Mapped>
    bool operator<(map<Key, Mapped>% left,
        map<Key, Mapped>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

备注

如果对于 !(right[i] < left[i]) 的最低位置 ileft[i] < right[i] 也为 true,则运算符函数返回 true。 否则,它将返回 left->size() < right->size()。 当两个映射按元素逐个比较时,可以用此运算符来测试 left 是否没有排在 right 之前。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // assign to a new container
    Mymap c2;
    c2.insert(Mymap::make_value(L'a', 1));
    c2.insert(Mymap::make_value(L'b', 2));
    c2.insert(Mymap::make_value(L'd', 4));

    // display contents " [a 1] [b 2] [d 4]"
    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("[a b c] < [a b c] is {0}",
        c1 < c1);
    System::Console::WriteLine("[a b c] < [a b d] is {0}",
        c1 < c2);
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [d 4]
[a b c] < [a b c] is False
[a b c] < [a b d] is True

operator<=(映射)

列表小于或等于比较。

语法

template<typename Key,
    typename Mapped>
    bool operator<=(map<Key, Mapped>% left,
        map<Key, Mapped>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

备注

运算符函数返回 !(right < left)。 当两个映射按元素逐个比较时,可以用它来测试 left 是否不在 right 之后。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // assign to a new container
    Mymap c2;
    c2.insert(Mymap::make_value(L'a', 1));
    c2.insert(Mymap::make_value(L'b', 2));
    c2.insert(Mymap::make_value(L'd', 4));

    // display contents " [a 1] [b 2] [d 4]"
    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("[a b c] <= [a b c] is {0}",
        c1 <= c1);
    System::Console::WriteLine("[a b d] <= [a b c] is {0}",
        c2 <= c1);
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [d 4]
[a b c] <= [a b c] is True
[a b d] <= [a b c] is False

operator==(映射)

列表等于比较。

语法

template<typename Key,
    typename Mapped>
    bool operator==(map<Key, Mapped>% left,
        map<Key, Mapped>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

注解

仅当 leftright 控制的序列具有相同的长度并且对于每个位置 ileft[i] == right[i] 时,运算符函数才返回 true。 当两个映射按元素逐个比较时,可以用它来测试 left 的顺序是否与 right 相同。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // assign to a new container
    Mymap c2;
    c2.insert(Mymap::make_value(L'a', 1));
    c2.insert(Mymap::make_value(L'b', 2));
    c2.insert(Mymap::make_value(L'd', 4));

    // display contents " [a 1] [b 2] [d 4]"
    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("[a b c] == [a b c] is {0}",
        c1 == c1);
    System::Console::WriteLine("[a b c] == [a b d] is {0}",
        c1 == c2);
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [d 4]
[a b c] == [a b c] is True
[a b c] == [a b d] is False

operator>(映射)

列表大于比较。

语法

template<typename Key,
    typename Mapped>
    bool operator>(map<Key, Mapped>% left,
        map<Key, Mapped>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

备注

运算符函数返回 right < left。 当两个映射按元素逐个比较时,可以用它来测试 left 的顺序是否在 right 之后。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // assign to a new container
    Mymap c2;
    c2.insert(Mymap::make_value(L'a', 1));
    c2.insert(Mymap::make_value(L'b', 2));
    c2.insert(Mymap::make_value(L'd', 4));

    // display contents " [a 1] [b 2] [d 4]"
    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("[a b c] > [a b c] is {0}",
        c1 > c1);
    System::Console::WriteLine("[a b d] > [a b c] is {0}",
        c2 > c1);
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [d 4]
[a b c] > [a b c] is False
[a b d] > [a b c] is True

operator>=(映射)

列表大于或等于比较。

语法

template<typename Key,
    typename Mapped>
    bool operator>=(map<Key, Mapped>% left,
        map<Key, Mapped>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

备注

运算符函数返回 !(left < right)。 当两个映射按元素逐个比较时,可以用它来测试 left 是否不在 right 之前。

示例

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

typedef cliext::map<wchar_t, int> Mymap;
int main()
    {
    Mymap c1;
    c1.insert(Mymap::make_value(L'a', 1));
    c1.insert(Mymap::make_value(L'b', 2));
    c1.insert(Mymap::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Mymap::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // assign to a new container
    Mymap c2;
    c2.insert(Mymap::make_value(L'a', 1));
    c2.insert(Mymap::make_value(L'b', 2));
    c2.insert(Mymap::make_value(L'd', 4));

    // display contents " [a 1] [b 2] [d 4]"
    for each (Mymap::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("[a b c] >= [a b c] is {0}",
        c1 >= c1);
    System::Console::WriteLine("[a b c] >= [a b d] is {0}",
        c1 >= c2);
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [d 4]
[a b c] >= [a b c] is True
[a b c] >= [a b d] is False