STL关联容器:简单的标准库类型-pair类型
时间:2011-12-11
来源:互联网

STL关联容器:简单的标准库类型-pair类型
代码来源:C++ Primer 4
C/C++ code
#include <iostream> #include <string> #include <vector> using namespace std; int main() { pair<string, string> anon; // 包含两个字符串 pair<string, int> word_count; // 包含字符串和整数 pair<string, vector<int> > line; // 包含字符串和一个int容器 pair<string, string> author("James", "Joyce"); // 定义成员时初始化 cout << author.first << " - " << author.second << endl; string firstBook; // 使用 . 访问和测试pair数据成员 if (author.first == "James" && author.second == "Joyce") { firstBook = "Stephen Hero"; cout << firstBook << endl; } typedef pair<string, string> Author; // 简化声明一个作者pair类型 Author proust("Marcel", "Proust"); Author Joyce("James", "Joyce"); pair<string, string> next_auth; string first, last; while (cin >> first >> last) { // 使用make_pair函数生成一个新pair对象 next_auth = make_pair(first, last); // 使用make_pair函数,等价于下面这句 next_auth = pair<string, string> (first, last); cout << next_auth.first << " - " << next_auth.second << endl; if (next_auth.first == next_auth.second) break; // 输入两个相等,退出循环 } cout << "因为pair的数据成员是共有的,因而可以直接读取输入" << endl; while (cin >> next_auth.first >> next_auth.second) { cout << next_auth.first << " - " << next_auth.second << endl; if (next_auth.first == next_auth.second) break; } return 0; }
作者: hongwenjun 发布时间: 2011-12-11
最近看到好多高手的分享,学习了
作者: whoami1978 发布时间: 2011-12-11
典型的键值对
map set中会用到
map set中会用到
作者: elegant87 发布时间: 2011-12-11
C/C++ code
这行中的常量字符串"James"和"Joyce" 是会被编译器优化成统一地址吗?
pair<string, string> author("James", "Joyce"); // 定义成员时初始化 cout << author.first << " - " << author.second << endl; string firstBook; // 使用 . 访问和测试pair数据成员 if (author.first == "James" && author.second == "Joyce") { firstBook = "Stephen Hero"; cout << firstBook << endl; }
这行中的常量字符串"James"和"Joyce" 是会被编译器优化成统一地址吗?
作者: w233052085 发布时间: 2011-12-11

insert()函数:
iterator insert( iterator pos, const pair<KEY_TYPE,VALUE_TYPE> &val );
void insert( input_iterator start, input_iterator end );
pair<iterator, bool> insert( const pair<KEY_TYPE,VALUE_TYPE> &val );
插入val到pos的后面,然后返回一个指向这个元素的迭代器。
插入start到end的元素到map中。
只有在val不存在时插入val。返回值是一个指向被插入元素的迭代器和一个描述是否插入的bool值。
C/C++ code
#include <iostream> #include <map> using namespace std; int main() { map<char, int> mymap; map<char, int>::iterator it; pair<map<char, int>::iterator, bool> ret; // 包含一个迭代器和一个bool值的的pair对象 // 第一个insert函数版本 (single parameter): 单参数 // 只有在val不存在时插入val。返回值是一个指向被插入元素的迭代器和一个描述是否插入的bool值。 mymap.insert(pair<char, int>('a', 100)); mymap.insert(make_pair('z', 200)); // 使用make_pair函数简化插入参数 typedef map<char, int>::value_type valType; mymap.insert(valType('t', 200)); // 或者使用 typedef 简化,提高程序的可读性 ret = mymap.insert(pair<char, int>('z', 500)); // 因为已经存在,不能插入 if (ret.second == false) { cout << "元素'z'已经存在,"; cout << " 他的值是 " << ret.first->second << endl; // ret.firs是迭代器指向map中具有相应键的元素 } // 第二个insert函数版本 (with hint position): 插入val到pos的后面,然后返回一个指向这个元素的迭代器 it = mymap.begin(); mymap.insert(it, pair<char, int>('b', 300)); // 最大效率的插入,'b'直接插入在'a' 后面 mymap.insert(it, pair<char, int>('c', 400)); // 没有最大效率的插入,'c'以'a'位置搜索存储的位置 // 第三个insert函数版本 (range insertion): 范围插入 start到end的元素到map中 map<char, int> anothermap; anothermap.insert(mymap.begin(), mymap.find('c')); // find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 // 遍历容器查看内容: cout << "mymap 包含内容:\n"; for (it = mymap.begin() ; it != mymap.end(); it++) cout << (*it).first << " => " << (*it).second << endl; cout << "anothermap 包含内容:\n"; for (it = anothermap.begin() ; it != anothermap.end(); it++) cout << it->first << " => " << it->second << endl; return 0; }
输出结果:
元素'z'已经存在, 他的值是 200
mymap 包含内容:
a => 100
b => 300
c => 400
t => 200
z => 200
anothermap 包含内容:
a => 100
b => 300
作者: hongwenjun 发布时间: 2011-12-11
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28