C ++中指针的索引

时间:2012-11-10 03:08:05

标签: c++ pointers indexing

我有一个使用索引工作的程序。但是,我们应该将其更改为指定的指针。

在我尝试将其切换为指针的下面附加的代码中,我将整个程序中的值取值为index并将其更改为指针。这至少是我的想法。     以下是使用索引的代码:

#include <cstdlib>
#include <iostream>
#include <iomanip>

/*
  Program sorts an array of integers using a selection sort.
  The general algorithm repeatedly finds the smallest number
  in the array and places it at the front of the list.
*/
using namespace std;

const int size = 20;

int find_small_index (int start_index, int numbers []);
void swap_values (int index1, int index2, int numbers []);
void print (int numbers []);

int main(int argc, char *argv[])
{
    // array of numbers
    int numbers [size] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41,
                       54, 128, 62, 44, 12, 1023, 89, 905, 32, -12};
    int start_index;  // current starting spot for search
    int small_index;  // index of the smallest number in the array

    start_index = 0;
    // continue finding the smallest value and placing it
    // at the front of the list
    while (start_index < size - 1)
    {
          small_index = find_small_index (start_index, numbers);
          swap_values (small_index, start_index, numbers);
          start_index++;
    }

    cout << "\n\nThe sorted array is:\n";
    print (numbers);

    cout << "\n\n";

    system("PAUSE");
    return EXIT_SUCCESS;
}

// finds and returns the index of the smallest number remaining in 
// the array
int find_small_index (int start_index, int numbers [])
{
    int small_index, // smallest index to be returned
        index;       // current index being viewed

    small_index = start_index;
    // look at each element
    for (index = start_index + 1; index < size; index++)
        // remember index of smaller value 
        if (numbers [index] < numbers [small_index])
           small_index = index;
    return small_index;
}

// swap the values in the array at indexes index1 and index2
void swap_values (int index1, int index2, int numbers [])
{
     int swapper;

     swapper = numbers [index1];
     numbers [index1] = numbers [index2];
     numbers [index2] = swapper;
}

// prints the array in nice format, 10 numbers per line
void print (int numbers [])
{
     int on_line,  // number of values printed on the line
         index;    // index of current number being printed

     on_line = 0;
     // print each element in the array
     for (index = 0; index < size; index++)
     {
         cout << setw (5) << numbers [index];
         on_line++;
         // if 10 numbers have been printed on the line
         // go to next line
         if (on_line == 10)
         {
            cout << "\n";
            on_line = 0;
         }
     }
}

这是我尝试更改以使用指针但不断出错的代码。有人可以向我解释我做错了什么吗?

#include <cstdlib>
#include <iostream>
#include <iomanip>

/*
  Program sorts an array of integers using a selection sort.
  The general algorithm repeatedly finds the smallest number
  in the array and places it at the front of the list.
*/
using namespace std;

const int size = 20;

int find_small_pointer (int start_pointer, int numbers []);
void swap_values (int index1, int index2, int numbers []);
void print (int numbers []);

int main(int argc, char *argv[])
{
    // array of numbers
    int numbers [size] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41,
                       54, 128, 62, 44, 12, 1023, 89, 905, 32, -12};
    int *start_ptr
        , *start_pointer;  // current starting spot for search
    int *small_ptr
        , *small_pointer;  // index of the smallest number in the array
    int * mover;
    int *size;


    start_ptr = 0;

    // continue finding the smallest value and placing it
    // at the front of the list
    while (start_ptr < size - 1)
    {
          *small_pointer = find_small_pointer (*start_pointer, numbers);
          swap_values (*small_pointer, *start_pointer, numbers);
          *start_pointer++;
    }

    cout << "\n\nThe sorted array is:\n";
    print (numbers);

    cout << "\n\n";

    system("PAUSE");
    return EXIT_SUCCESS;
}

// finds and returns the index of the smallest number remaining in 
// the array
int find_small_pointer (int *start_pointer, int numbers [])
{
    int *small_pointer, // smallest index to be returned
       mover;       // current index being viewed

    small_pointer = start_pointer;
    // look at each element
    for (mover = start_pointer + 1; mover < size; mover++)
        // remember index of smaller value 
        if (numbers [mover] < numbers [small_ptr])
           small_pointer = mover;
    return small_pointer;
}

// swap the values in the array at indexes index1 and index2
void swap_values (int mover1, int mover2, int numbers [])
{
     int swapper;

     swapper = numbers [mover1];
     numbers [mover1] = numbers [mover2];
     numbers [mover2] = swapper;
}

// prints the array in nice format, 10 numbers per line
void print (int numbers [])
{
     int on_line,  // number of values printed on the line
         mover;    // index of current number being printed

     on_line = 0;
     // print each element in the array
     for (mover = 0; mover < size; mover++)
     {
         cout << setw (5) << numbers [mover];
         on_line++;
         // if 10 numbers have been printed on the line
         // go to next line
         if (on_line == 10)
         {
            cout << "\n";
            on_line = 0;
         }
     }
}

2 个答案:

答案 0 :(得分:1)

在你的while循环中

while (start_ptr < size - 1)

您正在将指针与大小进行比较。你应该比较指针。所以,

*start_ptr < size - 1

在将它设置为0之前的几行中也是如此。

*start_ptr = 0

请确保您知道是否要对指针或其指针执行某些操作。

答案 1 :(得分:0)

你有两个不同的东西叫size。第二个应该被命名为其他东西,例如end,这是一个良好的品味和可维护性的问题。更重要的是,它应该被初始化:int *end = numbers + size。然后声明while (start_ptr < end - 1)将表现得合理。

在C ++ 11中,甚至不需要此变量,因为您可以编写:

#include <iterator>
⋮
    while (start_ptr < std::end(numbers) - 1)
    ⋮

另请注意,swapValues是不必要的。你可以写:

#include <utility>
⋮
    std::swap(numbers[small_index], numbers[start_index]);

或者,在指针版本中(对swapValues的调用实际上是错误的,因为你将指针作为整数传递):

    std::swap(*small_pointer, *start_pointer);

代码还有几个问题,但我已经没时间了。遗憾。