鱼C论坛

 找回密码
 立即注册
查看: 1070|回复: 2

[已解决]求助 vector内元素互换!

[复制链接]
发表于 2020-7-25 12:56:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
写一个名为swapFrontBack的函数,整数输入vector内。该函数将第一个和第最后一个函数交换。这个函数可以检查vector是否是空的以防错误。用不同的长度测试你的函数。
最佳答案
2020-7-25 14:12:15

  1. template<typename T>
  2. int Swap(T* one, T* another) {
  3.     if (one == nullptr || another == nullptr) {
  4.         return 0;
  5.     }
  6.     if (one == another || *one == *another) {
  7.         return 0;
  8.     }
  9.     T temp = *one;
  10.     *one = *another;
  11.     *another = temp;
  12.     return 1;
  13. }

  14. template<typename T>
  15. int SwapFrontBack(std::vector<T> &v) {
  16.     return Swap(&*v.begin(), &*(v.end()-1) );
  17. }

  18. void PrintInts(std::vector<int> &v) {
  19.     for (auto it = v.begin(); it != v.end(); it++) {
  20.         std::cout << *it << " ";
  21.     }
  22.     std::cout << std::endl;
  23. }

  24. int main() {
  25.     {
  26.         std::vector<int> v{1, 2, 3, 4, 5};
  27.         PrintInts(v);
  28.         SwapFrontBack(v);
  29.         PrintInts(v);
  30.     }
  31.     {
  32.         std::vector<int> v{};
  33.         PrintInts(v);
  34.         SwapFrontBack(v);
  35.         PrintInts(v);
  36.     }
  37.     {
  38.         std::vector<int> v{1};
  39.         PrintInts(v);
  40.         SwapFrontBack(v);
  41.         PrintInts(v);
  42.     }
  43.     return 0;
  44. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-25 14:12:15 | 显示全部楼层    本楼为最佳答案   

  1. template<typename T>
  2. int Swap(T* one, T* another) {
  3.     if (one == nullptr || another == nullptr) {
  4.         return 0;
  5.     }
  6.     if (one == another || *one == *another) {
  7.         return 0;
  8.     }
  9.     T temp = *one;
  10.     *one = *another;
  11.     *another = temp;
  12.     return 1;
  13. }

  14. template<typename T>
  15. int SwapFrontBack(std::vector<T> &v) {
  16.     return Swap(&*v.begin(), &*(v.end()-1) );
  17. }

  18. void PrintInts(std::vector<int> &v) {
  19.     for (auto it = v.begin(); it != v.end(); it++) {
  20.         std::cout << *it << " ";
  21.     }
  22.     std::cout << std::endl;
  23. }

  24. int main() {
  25.     {
  26.         std::vector<int> v{1, 2, 3, 4, 5};
  27.         PrintInts(v);
  28.         SwapFrontBack(v);
  29.         PrintInts(v);
  30.     }
  31.     {
  32.         std::vector<int> v{};
  33.         PrintInts(v);
  34.         SwapFrontBack(v);
  35.         PrintInts(v);
  36.     }
  37.     {
  38.         std::vector<int> v{1};
  39.         PrintInts(v);
  40.         SwapFrontBack(v);
  41.         PrintInts(v);
  42.     }
  43.     return 0;
  44. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-26 16:37:43 | 显示全部楼层

太感谢了!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 23:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表