Skip to content

Latest commit

ย 

History

History
67 lines (48 loc) ยท 1.46 KB

[C++] Vector Container.md

File metadata and controls

67 lines (48 loc) ยท 1.46 KB

[C++] Vector Container


#include <vector>

์ž๋™์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•ด์ฃผ๋Š” Cpp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, push pop์€ ์Šคํƒ๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์ด๋‹ค.


์ƒ์„ฑ

  • vector<"Type"> v;
  • vector<"Type"> v2(v); : v2์— v ๋ณต์‚ฌ

Function

  • v.assign(5, 2); : 2 ๊ฐ’์œผ๋กœ 5๊ฐœ ์›์†Œ ํ• ๋‹น
  • v.at(index); : index๋ฒˆ์งธ ์›์†Œ ์ฐธ์กฐ (๋ฒ”์œ„ ์ ๊ฒ€ o)
  • v[index]; : index๋ฒˆ์งธ ์›์†Œ ์ฐธ์กฐ (๋ฒ”์œ„ ์ ๊ฒ€ x)
  • v.front(); v.back(); : ์ฒซ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ์›์†Œ ์ฐธ์กฐ
  • v.clear(); : ๋ชจ๋“  ์›์†Œ ์ œ๊ฑฐ (๋ฉ”๋ชจ๋ฆฌ๋Š” ์œ ์ง€)
  • v.push_back(data); v.pop_back(data); : ๋งˆ์ง€๋ง‰ ์›์†Œ ๋’ค์— data ์‚ฝ์ž…, ๋งˆ์ง€๋ง‰ ์›์†Œ ์ œ๊ฑฐ
  • v.begin(); v.end(); : ์ฒซ๋ฒˆ์งธ ์›์†Œ, ๋งˆ์ง€๋ง‰์˜ ๋‹ค์Œ์„ ๊ฐ€๋ฆฌํ‚ด (iterator ํ•„์š”)
  • v.resize(n); : n์œผ๋กœ ํฌ๊ธฐ ๋ณ€๊ฒฝ
  • v.size(); : vector ์›์†Œ ๊ฐœ์ˆ˜ ๋ฆฌํ„ด
  • v.capacity(); : ํ• ๋‹น๋œ ๊ณต๊ฐ„ ํฌ๊ธฐ ๋ฆฌํ„ด
  • v.empty(); : ๋น„์–ด์žˆ๋Š” ์ง€ ์—ฌ๋ถ€ ํ™•์ธ (true, false)
capacity : ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ
size : ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ์›์†Œ ๊ฐœ์ˆ˜

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main(void) {
    vector<int> v;
    
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    
    vector<int>::iterator iter;
    for(iter = v.begin(); iter != v.end(); iter++) {
        cout << *iter << endl;
    }
}


[์ฐธ๊ณ  ์ž๋ฃŒ]