当前位置:   article > 正文

array,vector 的讲解,迭代器讲解_使用反向迭代器求array元素和元素的值

使用反向迭代器求array元素和元素的值
  1. 2.array,vector 的讲解
  2. 2.1array的详细讲解(静态数组)
  3. #include<iostream>
  4. #include<stdlib.h>
  5. #include<array>
  6. #include<vector>
  7. using namespace std;
  8. void main()
  9. {
  10. array<int, 5> myint1 = { 1,2,3,4,5 };//创建一维包含5个元素的整形数组
  11. array<int, 5> myint2 = { 11,12,13,14,15 };//创建一维包含5个元素的整形数组
  12. array<int, 5> myint3 = { 21,22,23,24,25 };//创建一维包含5个元素的整形数组
  13. //下面两种是等价的
  14. array < array<int, 5>, 3 > myint= {myint1, myint2, myint3}; //创建二维数组,其中为35列的整形数组
  15. array < array<int, 5>, 3 > myint = { 1,2,3,4,5,11,12,13,14,15,21,22,23,24,25 };//创建二维数组,其中为35列的整形数组
  16. //打印出数组
  17. for (int i = 0;i < 3;i++)
  18. {
  19. for (int j = 0;j < 5;j++)
  20. {
  21. cout << myint[i][j] << " ";
  22. }
  23. cout << endl;
  24. }
  25. }
  26. 2.2Vector的详细讲解(动态数组)
  27. 2.2.1
  28. void main()
  29. {
  30. vector<int> myvector;//动态数组
  31. vector<string> string1;//动态字符串数组
  32. string1.push_back("notepad");
  33. string1.push_back("cala");
  34. string1.push_back("mspaint");
  35. //string1.clear();//清空
  36. //string1.size();//大小
  37. for (int i = 0;i < string1.size();i++)
  38. {
  39. cout<<string1[i]<<endl;
  40. }
  41. }
  42. 2.2.2 迭代器
  43. /************Vector迭代器************/
  44. void main()
  45. {
  46. vector<string> string1;//动态字符串数组
  47. string1.push_back("notepad");
  48. string1.push_back("cala");
  49. string1.push_back("mspaint");
  50. vector<string>::iterator ibegin, iend; //迭代器
  51. ibegin = string1.begin(); //数据结构的起始点
  52. iend = string1.end(); //结束
  53. for (;ibegin!=iend;ibegin++)
  54. {
  55. string tempsrt = *ibegin;//获取指针指向的数据
  56. system(tempsrt.c_str());//执行指令
  57. }
  58. }
  59. /************Array迭代器************/
  60. void main()
  61. {
  62. array <int, 5> myint = { 1,2,3,4,5 };
  63. array<int, 5> ::iterator ibegin, iend; 正向迭代器
  64. ibegin = myint.begin();
  65. iend = myint.end();
  66. for (;ibegin != iend;ibegin++)
  67. {
  68. cout << *ibegin << " " << endl;
  69. }
  70. //********************************/
  71. array<int, 5>::reverse_iterator rbegin, rend;反向迭代器
  72. rbegin = myint.rbegin();
  73. rend = myint.rend();
  74. for (;rbegin != rend;rbegin++)
  75. {
  76. cout << *rbegin << " " << endl;
  77. }
  78. }
  79. void main()
  80. {
  81. vector<int> myvector(5); //分配5个空间
  82. //
  83. myvector.push_back(1);
  84. myvector.push_back(11);
  85. myvector.push_back(111);
  86. myvector.push_back(11111);
  87. myvector.push_back(2);
  88. myvector.pop_back();//删除最后一个元素
  89. myvector.erase(myvector.begin());//删除第1
  90. myvector.erase(myvector.begin()+3);//删除第4
  91. myvector.erase(myvector.begin()+5);//删除第6
  92. //myvector.clear();//删除所有元素
  93. for (int i=0;i<myvector.size();i++)
  94. {
  95. cout << myvector.at(i)<< " " << endl;
  96. }
  97. }
  98. void main()
  99. {
  100. vector <int> myvector1;
  101. myvector1.push_back(1);
  102. myvector1.push_back(2);
  103. myvector1.push_back(3);
  104. vector <int> myvector2;
  105. myvector2.push_back(22);
  106. vector <int> myvector3;
  107. myvector3.push_back(33);
  108. vector <vector<int>> allvector;
  109. allvector.push_back(myvector1);
  110. allvector.push_back(myvector2);
  111. allvector.push_back(myvector3);
  112. for (int i=0;i<allvector.size();i++)
  113. {
  114. for (int j = 0;j < allvector[i].size();j++)
  115. {
  116. cout << allvector[i][j]<<" " ;
  117. }
  118. cout << endl;
  119. }
  120. }
  121. void main()
  122. {
  123. array<double, 4> mydouble = { 1,2,3,4 };
  124. /* 利用迭代器
  125. array<double, 4> ::iterator ibegin, iend;
  126. ibegin = mydouble.begin();
  127. iend = mydouble.end();
  128. for (;ibegin != iend;ibegin++)
  129. {
  130. cout <<*ibegin<<" ";
  131. }
  132. cout << endl;
  133. */
  134. 使用常规方法
  135. for (int i=0;i<mydouble.size();i++)
  136. {
  137. cout << mydouble[i] << " ";
  138. }
  139. cout << endl;
  140. std::cin.get();
  141. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/390753?site
推荐阅读
相关标签
  

闽ICP备14008679号