使用适当的智能指针优化下面的程序
来源:互联网 发布:pp助手mac 编辑:程序博客网 时间:2024/05/18 04:51
#include <iostream> #include <cstring> char *get(const char *s) { int size = std::strlen(s); char *text = new char[size + 1]; std::strncpy(text, s, size + 1); return text; } void print(char *text) { std::cout << text << std::endl; } int main(int argc, char *argv[]) { if (argc < 2) { std::cerr << argv[0] << " <data>" << std::endl; return 1; } char *text = get(argv[1]); print(text); delete[] text; }
#include "boost/shared_ptr.hpp"#include <iostream> #include <cstring> boost::shared_ptr<char> get(const char *s){ boost::shared_ptr<char> txt(new char[std::strlen(s)+1]); strcpy_s(txt.get(), std::strlen(s)+1, s); return txt;}void print(boost::shared_ptr<char> text){ std::cout << text << std::endl;}int main(int argc, char *argv[]){ if (argc < 2) { std::cerr << argv[0] << " <data>" << std::endl; return 1; } boost::shared_ptr<char> text(get(argv[1])); print(text); return 0;}
阅读全文
0 0
- 使用适当的智能指针优化下面的程序
- 优化下面的程序
- 智能指针的使用
- 智能指针的使用
- 智能指针的使用
- 智能指针的使用
- 适当使用指针
- 智能指针 _com_ptr_t的使用
- 使用智能指针的危险
- C++ 智能指针的使用
- 智能指针auto_ptr的使用
- ADO智能指针的使用
- C++ 智能指针的使用
- c++智能指针的使用
- C++智能指针的使用
- 使用智能指针的注意事项
- MYSQL优化-建立适当的索引
- MYSQL适当的优化很有必要
- 导游
- 程序员面试金典——阶乘尾零
- linux如何卸载自带的openJdk,并且安装jdk1.8
- hihocoder 1290 面试题(dp)
- POJ2566 Bound Found
- 使用适当的智能指针优化下面的程序
- javaScript对象
- 十分钟理解Java中的动态代理----强烈推荐
- 史上最全canOpenURL: failed问题解决办法
- 线性表的顺序存储和链式存储差异
- 绑定数据报错 The specified child already has a parent. You must call remove
- 算法设计作业17
- 欢迎使用CSDN-markdown编辑器
- IOS Swift3.0 获取导航栏跟状态栏的高度