0

C++ で動的に割り当てられた文字配列のさまざまなインデックス位置でデータを操作する方法を理解する必要があります。いんちき(キュー/スタック)を作成しています。私が関心を持っている変数は「items」です。

Quack のクラス定義は次のとおりです。

class Quack
{
public:
    static const char   YOUR_NAME[];        // used for printing out programmer's name
    static const bool   PREMIUM_VERSION;    // used to designate Regular vs. Premium

    Quack(int capacity, int growBy = 0);
        // capacity: # of slots in array
        // growBy:   # of slots to add to array when it grows, 0 means "don't grow"
    ~Quack(void);
    bool pushFront(const char ch);      // push an item onto the front
    bool pushBack(const char ch);       // push an item onto the back
    bool popFront(char& ch);            // pop an item off the front
    bool popBack(char& ch);             // pop an item off the back
    void rotate(int r);                 // "rotate" the stored items (see note below)
    void reverse(void);                 // reverse the order of the stored items
    int itemCount(void);                // return the current number of stored items

    void printArray(std::ostream& out)                  // print contents of array
    {
        unsigned int    ch;

        out << "[ ";
        for (int i = 0; i < capacity; i++) {
            ch = static_cast<unsigned char>(items[i]);  // avoid sign extension
            if (ch == '-')                              // print in hex, because using '-' for 0xCD
                goto printHex;
            else if (ch >= '!' && ch <= '}')            // ASCII text character
                out << static_cast<char>(ch) << ' ';
            else if (ch == 0xCD)                        // print 0xCD as '-'
                out << "- ";
            else                                        // print everything else as hex
                goto printHex;
            continue;

        printHex:   
            out << std::setw(2) << std::setfill('0') << std::hex << ch << ' ';
            }
        out << ']' << std::endl;
    }

private:
    char    *items;                     
    int     nItems;                     
    int     capacity;                   
    int     growBy;
    int     *front;
    int     *back;

public:
    friend std::ostream& operator<<(std::ostream& out, Quack *q);
};

さて、Quack コンストラクターは次のとおりです。

Quack::Quack(int capacity, int growBy) :
    capacity(capacity),
    growBy(growBy),
    nItems(0),
    items(new char[capacity]),
    front(NULL),
    back(NULL)
{
}

Quack の各インスタンスには、新しい文字配列へのポインターである char 変数「items」があることを理解しています。私が理解していないのは、配列内のアイテムを参照する方法です。たとえば、新しい文字配列のインデックス位置 0 を参照したい場合、 items(0) と言うのか、 items[0] と言うのか、それともまったく別のものなのか?

ありがとうございました

4

1 に答える 1

0

あなたが正しく理解しているように、itemsは char 配列であり、 の各インスタンスにQuackは独自の variable がありますitems。動的に割り当てられた配列と静的に割り当てられた配列へのアクセスは同じです。それらは連続したメモリ位置に割り当てられます。

char arr[24]; // Allocate 24 bytes in contiguous memory locations in stack  
char* arr = new char[24]; //Allocate 24 bytes in contiguous memory locations
                          // in heap  

次のいずれかの方法で変数にアクセスできます。

1.  Quack q;  
    cout << q.items << endl; //Print the char array  
    cout << q.items[1] << endl; // Print the second index position of items  

2.  Quack* qptr = new Quack();
    cout << q->items << endl; //Print the char array  
    cout << q->items[1] << endl; // Print the second index position of items  
于 2014-10-25T04:19:03.150 に答える