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๊ด๋ จ ๋ฌธ์๋ฅผ ์ดํด๋ณด์๊ธฐ ๋ฐ๋๋๋ค.