본문 바로가기

Programming/Algorithm

std c++ vector 와 sort 사용법 예제

#include <stdio.h>

#include "iostream"


#include <vector>

#include <algorithm>

using namespace std;


typedef struct point{

int a;

char b;

} t_pos;


bool cmp(t_pos &A, t_pos &B) {

return A.b < B.b;

}


int main()

{

cout << "test" << endl;


vector<t_pos> a;

a.push_back({1,'b'});

a.push_back({2,'t' });

a.push_back({ 3, 'e' });

a.push_back({ 4, 'a' });


sort(a.begin(), a.end(), cmp);

// 삭제

//a.erase(a.begin() + 0);

cout << a.at(0).a << " " << a.at(0).b << endl;;

cout << a.front().a << " " << a.back().b << endl;;


// 순환

for (auto &x : a) {

cout << x.a << " " << x.b << endl;

}


cout << endl;


// 삭제

for (auto &iter = a.begin(); iter != a.end(); iter++) {

cout << (*iter).a << endl;

if ((*iter).a == 2) {

a.erase(iter);

break;

}

}

cout << endl;


// 순환

for (auto &x : a) {

cout << x.a << " " << x.b << endl;

}


// 사이즈

cout << a.size() << endl;

return 0;



'Programming > Algorithm' 카테고리의 다른 글

Design pattern - mediator  (0) 2020.01.17
std::string c++ 활용 예제  (0) 2018.07.11
quick sort  (0) 2016.05.21
visual studio 에서 stack size 유추 / set 하기  (0) 2016.05.12
각종 알고리즘 링크  (0) 2016.03.08