(std::cout, s.data())); _c++ 文件打印 库">
当前位置:   article > 正文

C++print打印与文件封装_c++ 文件打印 库

c++ 文件打印 库

其实是前面两篇文章的改进,我们也不多说了,print这个没点水平估计是看不懂

  1. namespace show_ {
  2. template<typename T,size_t i>
  3. void print(const T(&n)[i], const std::string s=" ") {
  4. std::copy(std::begin(n),std::end(n), std::ostream_iterator<T, char>(std::cout, s.data()));
  5. std::cout << std::endl;
  6. }
  7. template<typename T,size_t size>
  8. void print(const std::array<T,size> v, const std::string s = " ") {
  9. std::copy(std::begin(v), std::end(v), std::ostream_iterator<T, char>(std::cout, s.data()));
  10. std::cout << std::endl;
  11. }
  12. void print(const char* s) {
  13. std::cout << s << std::endl; //重载特殊情况,字符串常量输出
  14. }
  15. template<typename T>
  16. void print(const std::vector<T>n,const std::string s=" ") {
  17. std::copy(std::begin(n), std::end(n), std::ostream_iterator<T, char>(std::cout, s.data()));
  18. std::endl(std::cout);
  19. }
  20. template<typename T>
  21. void print(T v) {
  22. std::cout << v << std::endl;
  23. }
  24. template<typename T>
  25. void print(const std::list<T>& L,std::string s=" ") {
  26. for (auto it = L.begin(); it != L.end(); it++) { //list容器版本
  27. std::cout << *it << s;
  28. }
  29. std::cout << std::endl;
  30. }
  31. template<typename _Type1, typename _Type2, typename... _Types>
  32. void print(_Type1 _Value1, _Type2 _Value2, _Types... _Values)//c++17折叠表达式
  33. requires (sizeof...(_Types) > 0 || (!std::is_same_v<char*, _Type2> && !std::is_same_v<const char*, _Type2>))//requires是c++20
  34. {
  35. std::cout << _Value1 << ',' << _Value2 << ",";
  36. ((std::cout << _Values ), ...);
  37. }
  38. namespace object { //这真是无奈之举,这个匹配,object命名空间内的除了遍历vector和array的数组外,标准数据类型直接打印也可行
  39. template<typename T>
  40. std::ostream& operator<<(std::ostream& os, const std::vector<T>& data)
  41. {
  42. for (auto& str : data)
  43. {
  44. os << str;
  45. }
  46. return os;
  47. }
  48. template<typename T, size_t size>
  49. std::ostream& operator<<(std::ostream& os, const std::array<T, size>& data)
  50. {
  51. for (auto& str : data)
  52. {
  53. os << str<<",";
  54. }
  55. return os;
  56. }
  57. void print() {}
  58. template<typename T, typename...Types>
  59. void print(T first, Types...args) {
  60. std::cout << first << '\n';
  61. print(args...);
  62. return;
  63. }
  64. }
  65. }

这是print,下面是测试的一部分代码

  1. #include"func.h"
  2. using namespace show_;//引入命名空间,所以写不写命名空间都行
  3. int main()
  4. {
  5. int n[10]{ 1,2,3,4,5,6,7,8,9,10 };
  6. std::vector<double>i{ 1.2,3,5,4,8,7,9,10,11,30.8 };
  7. std::cout << ::sort_::average(n, 0, 9) << std::endl;
  8. std::cout << ::sort_::sum(n, 0, 9) << std::endl;
  9. ::sort_::Inverted(n, 0, 9);//逆置
  10. for (auto i : n)std::cout << i << ",";
  11. std::endl(std::cout);
  12. std::cout << ::maxAmin::max(i) << "," << ::maxAmin::max(n) << std::endl;
  13. std::cout << ::maxAmin::min(i) << "," << ::maxAmin::min(n) << std::endl;
  14. show_::print(n,",");
  15. show_::print(n);
  16. show_::print(i);
  17. show_::print(i,"*");
  18. show_::print(1, 2, 3, 4, 5, "666", "阿巴巴");
  19. std::endl(std::cout);
  20. print(1);
  21. show_::print("可怕");
  22. show_::print(5.7);
  23. const std::string str("离谱");
  24. show_::print(str);
  25. print(5);
  26. print(std::move(12));
  27. show_::print(std::move(n));
  28. std::array<int, 5>v{ 1,2,3,4,5 };
  29. print(v);
  30. std::list<double>L{ 1.2,3.4,5.6,7.8,9,10,11,12 };
  31. print(L);
  32. print(L, ",");
  33. int a = 5;
  34. print(&a);
  35. size_t a2 = 10;
  36. print(a2);
  37. print("666666666", 5, 5.7, 8u);
  38. std::cout << std::endl;
  39. std::array<std::string, 1>a1{ "离谱" }, b1{ "6666" }, c1{ "阿巴巴" };//如果数组长度比初始化的长到时候就会一堆逗号
  40. std::vector<std::string>a3{ "离谱2" }, b2{ "66662" }, c2{ "阿巴巴2" };
  41. object::print(a1, b1, c1, 1000);
  42. print("下面是测试");
  43. object::print(a1, b1, c1);
  44. object::print(a3, b2, c2, 66666);
  45. object::print(666666);
  46. object::print(n,"是吗","6666");
  47. object::print("66666", "离谱");
  48. std::array<int, 10>A{ 1,2,3,4,5,6,7,8,9,10 }, B{ 10,9,8,7,6,5,4,3,2,1 };
  49. std::vector<std::string>A1{ "离谱" }, B1{ "6666" }, C1{ "阿巴巴" };
  50. object::print("可以",A, B, 10);
  51. object::print(A1, B1, C1, 666);
  52. return 0;
  53. }
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 能看懂就看吧,下面是文件的命名空间

  1. namespace file_ {
  2. //获取当前时间的字符串
  3. std::string time_() {
  4. time_t timep;
  5. time(&timep);
  6. char tmp[256];
  7. strftime(tmp, sizeof(tmp), "%Y年%m月%d日_%H点%M分%S秒", localtime(&timep));
  8. std::string s{ tmp };
  9. return s;
  10. }
  11. //创建文件夹,默认在同级目录
  12. std::string newFolder(std::string name = time_(), std::string path = "") {
  13. std::string temp = "md ";
  14. temp += path;
  15. temp += name;
  16. //std::cout << "创建文件夹 " << temp << std::endl;
  17. system(temp.data());
  18. return temp.substr(3);
  19. }
  20. //删除文件夹
  21. std::string deleteFolber(std::string path) {
  22. std::string s = "rd ";
  23. system((s += path).data());
  24. return s.substr(3);
  25. }
  26. //以追加模式打开写文件
  27. std::string newWriteFile(std::string name = time_()+=".txt", std::string data = time_(), std::string path = "") {
  28. path += name;
  29. std::ofstream ofs;
  30. ofs.open(path, std::ios::app);
  31. ofs << data;
  32. ofs.close();
  33. return path;
  34. }
  35. //创建新的文件写入,一开始有就删除再创建
  36. void newlyFile(std::string name = time_(), std::string data = time_(), std::string path = "") {
  37. path += name;
  38. std::ofstream ofs;
  39. ofs.open(path, std::ios::trunc);
  40. ofs << data;
  41. ofs.close();
  42. }
  43. //删除文件的数据
  44. void deleteData(std::string name ,std::string path = "") {
  45. path += name;
  46. std::ofstream ofs(path, std::ios::trunc);
  47. ofs.close();
  48. }
  49. //删除文件
  50. bool deleteFile(std::string path) {
  51. if (remove(path.data()) == 0) {
  52. //std::cout << "删除成功" << std::endl;
  53. return true;
  54. }
  55. else {
  56. std::cout << "删除失败" << std::endl;
  57. return false;
  58. }
  59. }
  60. //读取文件
  61. std::string readFile(std::string path) {
  62. std::ifstream ifs;
  63. ifs.open(path, std::ios::in);
  64. if (!ifs.is_open())
  65. {
  66. std::cout << "文件打开失败" << std::endl;
  67. return "";
  68. }
  69. std::string data{};
  70. while (ifs >> data);
  71. ifs.close();
  72. return data;
  73. }
  74. //打印输出
  75. void print(std::string path) {
  76. show_::print(readFile(path));
  77. }
  78. }

MSVC ,C++20,debug,明天见

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/208836
推荐阅读
  

闽ICP备14008679号