【c++ STL之map容器】教程文章相关的互联网学习教程文章

POJ 2503 Babelfish (c++)(map容器)【代码】

题目大意就是给两个单词,前面个单词是后面个单词的翻译,也就是说存在着一种由键到值的映射关系,以输入空行为标志结束录入的对应关系,之后输入单词输出对应翻译。所以此题可以用c++的map容器建立对应关系来解决。 代码如下: 1 #include<iostream>2 #include<map>3 #include<cstdio>4 #include<cstring>5usingnamespace std;6int main(){7char str1[35] ;8char key[35] , value[35] ;9 map<string , string> map_ ; 10while...

c++map按value排序--将map的pair对保存到vector中,然后写比较仿函数+sort完成排序过程。

map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value。假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行存储就是个不错的选择。 我们这样定义,map<string, int>,其中学生姓名用string类型,作为Key;该学生的成绩用int类型,作为value。这样一来,我们可以根据学生姓名快速的查找到他的成绩。 但是,我们除了希望能够查询某个学生的成绩,或许还想看看...

c语言实现hashtable,类似C++的map和iOS的NSDictionary

跟线性数组和链表不同,HashTable是快速查找的数据结构。本文中的HashTable使用链表处理数组。该HashTable可以指定table的长度,提供了遍历的方法。包括table的长度的选择也比较讲究。 cp_int32 nPrime[MAX_HASH_PRIME_ARRAY_NUM] = {17, 37, 79, 163, 331, 673, 1361 };就是说table的长度来取自上面这个数组。比如用户设定了200,那么table的长度就是331,找到第一次比输入值大的数值。可以注意到上面的都是素数。 下面...

[转] C++ STL中map.erase(it++)用法原理解析【代码】

总结一下map::erase的正确用法。 首先看一下在循环中使用vector::erase时我习惯的用法:for(vector<int>::iterator it = vecInt.begin(); it != vecInt.end();) {if(*it == 0){it = vecInt.erase(it);}else{it++;} }程序从一个vector中删除值为0的元素,利用了vector::erase函数根据iterator删除某个元素时会返回下一个元素的iterator的性质: http://www.cplusplus.com/reference/vector/vector/erase/C++98 iterator erase (iterat...

c++ map快速入门【代码】【图】

Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数; map<string , int >mapstring; map<int ,string >mapint; map<sring, char>mapstring; map< char ,string>mapchar; map<char ,int>mapchar; map<int ,char >mapint;2. map添加数据; map<int ,string> maplive; 1.maplive...

c++中map按key和value排序【代码】

1```2 #include<iostream>3 #include<algorithm>4 #include<vector>5 #include<map>6 #include<set>7usingnamespace std;8 9/*按key升序*/10void test01(){ 11 map<string,int,less<string> > map1; 12 map1.insert(pair<string,int>("aba",3)); 13 map1.insert(pair<string,int>("aaa",2)); 14 map1.insert(pair<string,int>("ddd",1)); 15 map1.insert(pair<string,int>("ccc",4)); 1617for(map<string,int>...

C++中unordered_map几种按键查询比较【代码】

unorder_map有3种常见按键查值方法。使用头文件<unordered_map>和<iostream>,以及命名空间std。第一种是按键访问。如果键存在,则返回键对应的值;如果键不存在,则返回0; 1 #include<unordered_map>2 #include<iostream>3 4usingnamespace std;5 6int main() {7 unordered_map<int, int> test_map;8 cout << test_map[0] << endl; // 键0不存在,输出09 test_map[0] = 1; // 加入键0 10 cout << test_map[0] ...

c++ STL map简单使用【代码】

map字典存放键值对内部组成是红黑树 查找 删除 插入复杂度为O(logn) 初始化方式map<int,string> str; 插入方式1.使用pair map<int ,string>str; str.insert(pair<int, string>(1, "one")); 2.value_type方式map<int,string>str; map.insert(map<int,string>::value_type(1,"one"));3.数组方式map<int,string>str; str[1] = "one";insert方式插入关键字存在,无法插入。使用数组可以覆盖关键字的值 遍历时可使用反向迭代器遍历ma...

c++数据结构 --unorder_map【代码】【图】

#include <iostream> #include <unordered_map> usingnamespace std; class Book { private:string num;string name; public:Book() {};Book(string num,string name) { this ->name = name;this ->num = num;};string getNum() { return num;};string getName() { return name;}; }; int main() {unordered_map<string,Book>lib;Book b("001","高级语言程序设计") ;lib[b.getNum()] = b;unordered_map<string,Book>::iterator it;...

c++ map【代码】【图】

map 容器存储键值对,提供了很好的一对一的关系。在内部,元素总按特定的规则有序,通常用二叉搜索树实现。下面是使用示例:#include <cstdio> #include <algorithm> #include <set> #include <map> #include <iostream> usingnamespace std; int main () {map<char,int> mymap;//first insert methodmymap.insert(pair<char,int>(‘a‘,100));mymap.insert(pair<char,int>(‘z‘,200));//insert函数的返回值类型4pair<map<char,in...

c++ json字符串转换成map管理

在cocos2dx for lua中,我们经常通过lua的table传入c++使用,然后早c++层操作数据。实现步骤大致如下:table->string->c++层->通过rapidjson解析->存放在map中管理在lua中,转换table大致如下local tbl = {}tbl["fang"] = 1tbl["jian"] = 1.4tbl["heng"] = truetbl["fjh"] = 12345677tbl["what"] = "fuck"local str = json.encode(tbl)--打印str:{"fang":1,"jian":1.4,"heng":true,"fjh":12345677,"what":"fuck"} 将str传入c+...

C++ map的基本和高级操作

C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值。 1、map简介map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。 2、map的功能自动建立Key - value的对应。key 和 value可以是任意你需要的类型。 根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,...

C++ map 使用erase在windows下崩溃,在linux下没有问题的原因【代码】【图】

注意:此程序在win环境下会出现上述描述的问题;在mac环境下第一种方式是正常运行的。Map.erase有3个重载函数:void erase(iterator position);size_type erase(const key_type& x);  // 这个如果size_type为int,则返回值为1时代表删除成功,为0代表删除失败。void erase(iterator first, iterator last); // 相当于map.clean()。1. 错误示范备注:map是关联式容器,调用erase后,当前迭代器已经失效 std::map<int, int> mma...

C++ map的基本操作和使用

文章转载于:http://www.cnblogs.com/hailexuexi/archive/2012/04/10/2440209.html1、map简介map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。2、map的功能自动建立Key - value的对应。key 和 value可以是任意你需要的类型。 根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,...

C++ 中map容器

C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值。map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。 map的功能自动建立Key - value的对应。key 和 value可以是任意你需要的类型。 根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1...