Qaupot Blog
Software Engineering, Trip

Vector

Vector๋Š” Array์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์ด์ง€๋งŒ, ์‹คํ–‰ ์ค‘ ํฌ๊ธฐ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํฐ ์ฐจ์ด๊ฐ€ ์—†์–ด ๋ณด์ด์ง€๋งŒ, Array์— ๋น„ํ•ด ํ›จ์”ฌ ๋ณต์žกํ•œ ๊ตฌํ˜„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

class Number
{
private:
    int val;
public:
    Number() : val(0) { }
    Number(int val) : val(val) { }

    operator int(){ return val; }
};

template <typename T> class Vector
{
private:
    T* data;
    unsigned int dataCapacity, dataSize;
public:
    Vector()
        :dataCapacity(2), dataSize(0)
    {
        this->data = new T[2];
    }
    ~Vector()
    {
        delete[] this->data;
    }
    unsigned int size() { return dataSize; }
    unsigned int capacity() { return dataCapacity; }

    void resize(unsigned int newSize)
    {
        T* newMem = new T[newSize];
        dataSize = (dataSize>newSize ? newSize : dataSize);
        dataCapacity = newSize;

        for (unsigned int i = 0; i < dataSize ; ++i)         
        {
            newMem[i] = this->data[i];
        }

        delete[] this->data;
        this->data = newMem;
    }

    void push_back(T& newData)
    {
        if (dataSize + 1 > dataCapacity)
        {
            resize(dataCapacity * 2);
        }

        this->data[dataSize] = newData;
        ++dataSize;
    }

    void pop_back()
    {
        if (dataSize >0)
            --dataSize;
    }

    T& operator[] (unsigned int subscript)
    {
        return data[subscript];
    }
};

int main(int argc, char** argv)
{
    Vector<Number> dataVector;

    dataVector.push_back(Number(1));
    dataVector.push_back(Number(3));
    dataVector.push_back(Number(6));

    for (unsigned int i = 0; i < dataVector.size(); ++i)
    {
        printf("%d\n", (int)(dataVector[i]));
    }
    printf("Size : %d Capacity %d \n",
        dataVector.size(), dataVector.capacity() );

    dataVector.pop_back();
    for (unsigned int i = 0; i < dataVector.size(); ++i)
    {
        printf("%d\n", (int)(dataVector[i]));
    }
    printf("Size : %d Capacity %d \n",
        dataVector.size(), dataVector.capacity());

    dataVector.resize(1);
    for (unsigned int i = 0; i < dataVector.size(); ++i)
    {
        printf("%d\n", (int)(dataVector[i]));
    }
    printf("Size : %d Capacity %d \n",
        dataVector.size(), dataVector.capacity());
}
1
3
6
Size : 3 Capacity 4
1
3
Size : 2 Capacity 4
1
Size : 1 Capacity 1

์˜ˆ์ œ๋Š” ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ Vector ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณธ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
์ด ์ฝ”๋“œ๋Š” STL์˜ Vector ํด๋ž˜์Šค์™€๋Š” ๋™์ž‘์ด ๋‹ค๋ฅด๋ฉฐ,
Numberํด๋ž˜์Šค์— ์ƒ์„ฑ์ž, ๋ณต์‚ฌ์ƒ์„ฑ์ž, ์†Œ๋ฉธ์ž, ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ๊ฐ๊ฐ ์˜ค๋ฒ„๋กœ๋”ฉ ํ•ด์„œ ์ถœ๋ ฅ๋ฌธ์„ ๋„ฃ์–ด๋ณด๋ฉด, std::vector์™€ ์ฐจ์ด์ ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

STL์—์„œ์˜ Vector

C++์˜ vector ํ—ค๋”์—๋Š” std::vector๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

#include <iostream>
#include <vector>

class Number
{
private:
    int val;
public:
    Number() : val(0) { }
    Number(int val) : val(val) { }

    operator int(){ return val; }
};

int main(int argc, char** argv)
{
    std::vector<Number> dataVector;

    dataVector.push_back(Number(1));
    dataVector.push_back(Number(3));
    dataVector.push_back(Number(6));

    // by index value
    for (auto i = 0; i < dataVector.size(); i++)
    {
        printf("%d\n", (int)(dataVector[i]));
    }
    printf("Size : %d Capacity %d \n",
        dataVector.size(), dataVector.capacity());

    dataVector.pop_back();
    // by iterator
    for (auto iter = dataVector.begin();
        iter != dataVector.end(); iter++)
    {
        printf("%d\n", (int)(*iter));
    }
    printf("Size : %d Capacity %d \n",
        dataVector.size(), dataVector.capacity());

    dataVector.resize(1);
    // by range-based for loops
    for (Number& iter : dataVector)
    {
        printf("%d\n", (int)(iter));
    }
    printf("Size : %d Capacity %d \n",
        dataVector.size(), dataVector.capacity());
}
1
3
6
Size : 3 Capacity 3
1
3
Size : 2 Capacity 3
1
Size : 1 Capacity 3

์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๊ฐ„๋‹จํ•œ std::vector์˜ ์‚ฌ์šฉ๊ณผ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
๋‹จ์ˆœํ•˜๊ฒŒ index ๊ฐ’์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , iterator๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

push_back, pop_back, insert, erase, clear, size, begin, end ๊ณผ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ,
์ž์„ธํ•œ ์‚ฌ์šฉ๋ฒ•์€ STL๊ด€๋ จ ๋ฌธ์„œ๋ฅผ ์‚ดํŽด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

์ด ๋ธ”๋กœ๊ทธ๋Š” ๊ฐœ์ธ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค. ๊ฒŒ์‹œ๊ธ€์€ ์˜ค๋ฅ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ €์ž๋Š” ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๊ฒŒ์‹œ๊ธ€์— ๋ณ„๋„์˜ ๊ณ ์ง€๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ, ํฌ๋ฆฌ์—์ดํ‹ฐ๋ธŒ ์ปค๋จผ์ฆˆ ์ €์ž‘์žํ‘œ์‹œ-๋น„์˜๋ฆฌ-๋ณ€๊ฒฝ๊ธˆ์ง€ 4.0 ๋ผ์ด์„ ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

This blog is personal blog. published posts may contain some errors, but author doing efforts to clear errors.
If post have not notice of license, it under creative commons Attribution-NonCommercial-NoDerivatives 4.0.