stl学习记录(2)
#include <iostream> #include <utility> #include <tuple> #include <complex> #include <string> using namespace std; // 代码 改编自 C++标准库——自学教程与参考手册 英文第二版 //==================================== // tuple io template <int IDX,int MAX,typename... Args> struct PRINT_TUPLE{ static void print(ostream& strm, const tuple<Args...>& t){ strm << get<IDX>(t) << (IDX + 1 == MAX ? "" : ","); PRINT_TUPLE<IDX + 1, MAX, Args...>::print(strm,t); } }; template <int MAX,typename... Args> struct PRINT_TUPLE<MAX, MAX, Args...>{ static void print(ostream& strm, const tuple<Args...>& t){ } }; template <typename... Args> ostream& operator << (ostream& strm, tuple<Args...>& t) { strm << "["; PRINT_TUPLE<0, sizeof...(Args), Args...>::print(strm,t); return strm << "]"; } //==================================== class Foo{ public: Foo(tuple<int, float>){ cout << "Foo::Foo(tuple)" << endl; } template <typename... Args> Foo(Args... args){ cout << "Foo::Foo(atgs...)" << endl; } }; int _tmain(int argc, _TCHAR* argv[]) { // cpp11 后 一些新语法 在STL中得使用 tuple<int, double>t(1,2.22); pair<int, Foo>p1(42, t); pair<int, Foo>p2(piecewise_construct, make_tuple(42), t); // 使用 ref() 表示对变量的引用 int i = 0; auto p = make_pair(ref(i), ref(i)); // 创建 pair<int&,int&> ++p.first; ++p.second; cout << "i = " << i << endl; // tie() 演示 pair<char, char> q = make_pair('c','b'); char c; tie(ignore, c) = q; // char c == 'b' // tuple<string, int, int, complex<double>> tt; tuple<int, double,string> t1(41,6.3,"nico"); cout << get<0>(t1) << " "; cout << get<1>(t1) << " "; cout << get<2>(t1) << " "; cout << endl; auto t2 = make_tuple(22,44,"nico"); get<1>(t1) = get<1>(t2); t1 = t2; // tuple io tuple <int, double, string> iot(77, 1.1, "more light"); cout << "io: " << iot << endl; return 0; }
cpp11stl的例子 VS2013下编译 通过
stl序列容器例子
cpp11 vs2013下编译 通过
#include <memory> #include <vector> #include <array> #include <string> #include <list> #include <iostream> #include <functional> #include <forward_list> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { array<string, 5> arrcoll = { "hello", "world" }; for (auto elem : arrcoll) { cout << elem << ' '; } cout << endl; list<char> listcoll; for (char c = 'a'; c <= 'z'; ++c) { listcoll.push_back(c); } for (auto elem : listcoll) { cout << elem << ' '; } cout << endl; //createforward-listcontainerforsomeprimenumbers forward_list<long> coll = { 2, 3, 5, 7, 11, 13, 17 }; //resizetwotimes //-note:poorperformance coll.resize(9); coll.resize(10, 99); //printallelements: for (auto elem : coll) { cout << elem << ' '; } cout << endl; return 0; }
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话