Soft Ware/C++ 언어!!

템블릿 STL<vector>를 이용한 소스

달려가보자 2012. 2. 3. 14:51


#include <iostream>
#include <vector>

using namespace std;


template <typename T>
void vectInsert(vector<T> &v, T a, T b)    //벡터에 요소를 삽입하는 함수
{
 for(int i = 0; i < (b - a) + 1; i++)
 {
  v.push_back(a+i);
 }
}
template <typename T>
void vectDisplay(vector<T> &v)
{
 vector<T> ::iterator it;
 for(it = v.begin();it != v.end(); it++)
  cout << *it;
 cout << endl;
}

int main(void)
{
 int count = 0;
 
 vector<char> vc;
 vector<char> vc1;
 vector<char> vc2;
 vector<int> vi;

 vectInsert<char>(vc1,97,122);
 vectInsert<char>(vc2,65,90);
 vectInsert<int>(vi,0,9);
 cout << "초기상태" << endl;
 cout << "a ~ z : "; vectDisplay(vc1);
 cout << "A ~ Z : "; vectDisplay(vc2);
 cout << "0 ~ 9 : "; vectDisplay(vi);

 cout << "vc1 + vc2" << endl;
 vc = vc1;   
 vc.insert(vc.end(),vc2.begin(),vc2.end());   
 cout << "a ~ Z : "; vectDisplay(vc);

 cout << "vc1과 vc2 요소 교환" << endl;
 vc1.swap(vc2);   
 cout << "A ~ Z : "; vectDisplay(vc1);
 cout << "a ~ z : "; vectDisplay(vc2);
 cout << "a ~ Z : "; vectDisplay(vc);
 return 0;
}