手动跟踪程序和重绘箭头

来源:互联网 发布:wamp配置php环境变量 编辑:程序博客网 时间:2024/05/02 03:10
 Cents(int cents) { m_cents = cents; }         // add Cents + Cents using a friend function      friend Cents operator+(const Cents &c1, const Cents &c2);         // subtract Cents - Cents using a friend function      friend Cents operator-(const Cents &c1, const Cents &c2);         int getCents() const { return m_cents; }  };     // note: this function is not a member function!  Cents operator+(const Cents &c1, const Cents &c2)  {      // use the Cents constructor and operator+(int, int)      // we can access m_cents directly because this is a friend function      return Cents(c1.m_cents + c2.m_cents);  

该算法中最令人困惑的部分是另一个循环(称为嵌套循环)的循环。外环(指数)遍历每个元素一个接一个。每一次迭代中的外环、内环(currentindex)是用来在剩下的阵列中找到最小的元素(从startIndex + 1)。smallestindex跟踪由内环发现最小的元素的索引。然后smallestindex是交换的字符。最后,外环(指数)提出的一个元素,和重复的过程。

提示:如果你有麻烦弄清楚上面的程序是如何工作的,它可以有助于通过一张纸上的一个示例案例。写出发(无序)水平在试卷顶端的数组元素。画箭头指示元素startIndex currentindex,和smallestindex索引。手动跟踪程序和重绘箭头作为指标变化。对于每一个外环的迭代,开始一个新的线显示阵列的当前状态。


nt数组[ 5 ] = { 9,7,5,3,1 };
对(汽车元件阵列)/符使元素的引用到实际的数组元素,防止抄袭了
std::cout <<元<<”;
在上面的示例中,元素将是对当前迭代数组元素的引用,避免了复制的副本。元素的任何变化都会影响到数组的迭代,如果元素是一个正常的变量,则是不可能的。
而且,当然,如果你打算使用它在一个只读的方式使你的元素常量是一个好主意:

   public:      Cents(int cents) { m_cents = cents; }         // add Cents + int using a friend function      friend Cents operator+(const Cents &c1, int value);         // add int + Cents using a friend function      friend Cents operator+(int value, const Cents &c1);            int GetCents() { return m_cents; }  };     // note: this function is not a member function!  Cents operator+(const Cents &c1, int value)  {      // use the Cents constructor and operator+(int, int)      // we can access m_cents directly because this is a friend function      return Cents(c1.m_cents + value);  


0 0