Skip to content

Latest commit

 

History

History
76 lines (62 loc) · 1.31 KB

array.md

File metadata and controls

76 lines (62 loc) · 1.31 KB

อาเรย์ (Array)

std::array

  • แทนอาเรย์แบบเดิมของภาษ C
  • เพิ่ม/ลดข้อมูลตอนรันไทม์(run time)ไม่ได้
#include <iostream>
#include <array>

using namespace std;

int main()
{
    array<int ,5> arr {10 ,20, 30, 40, 50};
    cout << "Array size = " << arr.size() << endl;
    for(int i=0; i<arr.size(); ++i)
    {
        cout << arr[i] << endl;
    }
    return 0;
}

compile time array

  • จองพื้นที่หน่วยความจำบน stack
int ctarr[3]; // allocate on stack

run time array

  • จองพื้นที่หน่วยความจำบน heap
int * rtarr = new int[3]; // allocate on heap

dynamic memory management

  • จองพี้นที่หน่วยความจำ (allocate)
int * a = new int[3];
  • คืนหน่วยความจำ (delete)
delete [] a;

array of objects

class Point
{
    int x, y;
public:
    Point( int x=0, int y=0);
    //...
};

Point * t1 = new Point[4];
Point t1[4];

array of pointers

Point ** t2 = new Point*[ 4 ];
for(int i=0; i<4; ++i )
{
    t2[i] = new Point(0,0);
}
for( int i=0; i<4; ++i )
{
    cout<<*t2[ i ]<<endl;
}