[C/C++标准库]_[初级]_[实用类std::pair]
来源:互联网 发布:人工智能瓶颈问题 编辑:程序博客网 时间:2024/05/21 17:44
std::pair
场景:
1. 在需要元数据信息时,pair发挥比较大的作用,比如对返回值的额外信息描述就可以用pair,不需要额外创建一个关联类.
2. 而且pair是模版类,适合不同类型的对象.
3. 写这么小的类估计会被Java童鞋鄙视吧,呵呵。
说明:
1. 在进行operator==比较时,也是对first,second对象调用operator==比较.
2. 在进行operator < 比较时,先比较first的operator <,如果first ==,那么调用second的operator <.
3.在map里和multimap中使用pair存储key/value.
#include <iostream>#include <fstream>#include <string>#include <string.h>#include <utility>using namespace std;pair<int,string> DoSomeWork(bool flag){if (flag){return std::make_pair(-1,string("Network is disconnect!"));}else{return pair<int,string>(0,"Success.");}}int main(int argc, char const *argv[]){//原始类型如果用这种创建方式,是初始化为0.int i = int();cout << i << endl;pair<int,string> res = DoSomeWork(true);cout << "code: " << res.first << " description: " << res.second << endl;res = DoSomeWork(false);cout << "code: " << res.first << " description: " << res.second << endl;return 0;}
输出:
0code: -1 description: Network is disconnect!code: 0 description: Success.
参考:
1. 《C++ Standard Library, The: A Tutorial and Reference》
0 0
- [C/C++标准库]_[初级]_[实用类std::pair]
- [C/C++标准库]_[初级]_[std::deque和std::vector比较]
- [C/C++标准库]_[初级]_[自定义std::map的key]
- [C/C++标准库]_[初级]_[std::vector的删除元素]
- [C/C++标准库]_[初级]_[std::map的使用细节]
- [C/C++标准库]_[初级]_[使用std::sort排序各种类型数据]
- [C/C++标准库]_[初级]_[如何实现std::string自己的Format(sprintf)函数]
- [C/C++标准库]_[初级]_[std::vector的多线程读写问题]
- [C/C++]_[初级]_[标准字符串std::string 所有函数的用法总结]
- [C/C++11]_[初级]_[实用时间库chrono]
- [C/C++标准库]_[初级]_[使用时间库]
- [C/C++标准库]_[初级]_[signal信号浅析]
- [C/C++标准库]_[初级]_[集合操作]
- [C/C++标准库]_[初级]_[使用string stream]
- [C/C++标准库]_[初级]_[分割字符串Split]
- [C/C++标准库]_[初级]_[unary_function 和 binary_function]
- [C/C++标准库]_[初级]_[unary_function 和 binary_function]
- [C/C++标准库]_[初级]_[标准库里提供的排序算法]
- Python的类之self的重要性
- 重放攻击
- 2014蓝桥杯本科B组C/C++第二题【切面条】
- 预编译
- 最长连续回文串(Longest Palindromic Substring)
- [C/C++标准库]_[初级]_[实用类std::pair]
- 天马行空 PCI理解
- 创建DC画布并在上面画图并将结果缩放到一个按钮上显示
- 临时1
- c++-面向对象与面向过程
- HDU 1147 Pick-up sticks(简单的线段相交)
- 数据结构—链表List
- 将整型数组转成一个完整的整型数字
- 神光03月28日同动股面评