verctor v;
vector v(5); - 0으로 초기화된 원소 5개
vector v(5, 2); - 2로 초기화된 원소 5개
vector v2(v1); - v1 복사해서 v2 생성
v.assign(5,2) - 2의 값으로 5개 원소 할당
v.at(idx) / v[idx]
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void){
vector<pair<int, string> > v;
v.push_back(pair<int, string>(90, "예원"));
v.push_back(pair<int, string>(70, "지연"));
v.push_back(pair<int, string>(88, "태경"));
v.push_back(pair<int, string>(99, "정미"));
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i++){
cout << v[i].second << ' ';
}
vector <string> vStr;
vStr.push_back("hello");
vStr.push_back("world");
vector<string>::iterator iter;
for(iter = vStr.begin() ; iter != vStr.end(); iter++){
cout << *iter << ' ';
}
cout << endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool compare(pair<string, pair<int, int> > a,
pair<string, pair<int, int> > b){
if(a.second.first == b.second.first){
return a.second.second > b.second.second;
}else{
return a.second.first > b.second.first;
}
}
int main(void){
vector <pair <string, pair<int, int> > > v;
v.push_back(pair<string, pair<int, int> >("예원", make_pair(99, 19970402)));
v.push_back(pair<string, pair<int, int> >("민태", make_pair(99, 19960202)));
sort(v.begin(), v.end(), compare);
for(int i = 0; i < v.size(); i++){
cout << v[i].first << ' ';
}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
v.assign(5,2);
v.push_back(4);
vector<int>::iterator iter;
for(iter = v.begin(); iter != v.end(); iter++)
cout << *iter;
vector<int>::reverse_iterator riter;
for(riter = v.rbegin(); riter != v.rend(); ++riter)
cout << *riter;
}