[STL] Vector 사용 예제

by Blogger 하얀쿠아
2012. 6. 3. 16:58 소프트웨어 Note/C++ MFC

//STL vector 예제를 위한 헤더

#include <iostream>

#include <vector>

#include <string>

#include <algorithm> 

#include <iterator>



vector<string> sentence;


sentence.reserve(5);


sentence.push_back("hello,");

sentence.push_back("how");

sentence.push_back("are");

sentence.push_back("you");

sentence.push_back("?");



//공백으로 구분하여 원소출력

copy (sentence.begin(), sentence.end(), ostream_iterator<string>(cout, " "));

cout << endl;


//세부정보

cout << "max size():" << sentence.max_size() << endl;

cout << "size(): " << sentence.size() << endl;

cout << "capacity():" << sentence.capacity() << endl;


//두번째 값과 네번째 값을 교체

swap(sentence[1], sentence[3]);


//"?" 원소 앞에 "always"를 할당한다.

sentence.insert( find(sentence.begin(), sentence.end(), "?"), "always");


//마지막에 "!" 를 할당한다.

sentence.back() = "!";

//공백으로 구분하여 원소 출력

copy(sentence.begin(), sentence.end(), ostream_iterator<string>(cout, " "));

cout << endl;


//세부정보 다시 보여줌

cout << "max size():" << sentence.max_size() << endl;

cout << "size(): " << sentence.size() << endl;

cout << "capacity():" << sentence.capacity() << endl;

이 댓글을 비밀 댓글로