C++/뇌를 자극하는 C++ STL

뇌를 자극하는 C++ STL 이것만은 알고 갑시다 8장

꾸준함. 2018. 1. 11. 01:58

[2번]

/*

다음 그림을 보고 알고리즘을 사용하여 vector의 원소를 제거하세요

: 10 20 30 40 50 N

: 10 20 40 50 N

*/

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

 

int main(void)

{

        vector<int> v;

        v.push_back(10);

        v.push_back(20);

        v.push_back(30);

        v.push_back(40);

        v.push_back(50);

 

        cout << ": ";

        for (vector<int>::size_type i = 0; i < v.size(); i++)

               cout << v[i] << " ";

        cout << endl;

 

        vector<int>::iterator iter_end;

        iter_end = remove(v.begin(), v.end(), 30);

        v.erase(iter_end, v.end()); //완전히 제거

        cout << ": ";

        for (vector<int>::size_type i = 0; i < v.size(); i++)

               cout << v[i] << " ";

        cout << endl;

 

        return 0;

}


[3번]

/*

다음 그림을 보고 알고리즘을 사용하여 vector의 일부 원소를 뒤집는 코드를 작성하세요

: 10 20 30 40 50 60 70 80 90 100 N

: 50 40 30 20 10 60 70 80 90 100 N

*/

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

 

int main(void)

{

        vector<int> v;

        v.push_back(10);

        v.push_back(20);

        v.push_back(30);

        v.push_back(40);

        v.push_back(50);

        v.push_back(60);

        v.push_back(70);

        v.push_back(80);

        v.push_back(90);

        v.push_back(100);

 

        cout << ": ";

        for (vector<int>::size_type i = 0; i < v.size(); i++)

               cout << v[i] << " ";

        cout << endl;

 

        cout << ": ";

        reverse(v.begin(), v.begin() + 5);

        for (vector<int>::size_type i = 0; i < v.size(); i++)

               cout << v[i] << " ";

        cout << endl;

        return 0;

}


[4번]

/*

다음 그림을 보고 알고리즘을 사용하여 vector를 정렬하고 모든 원소가 유일하게 코드를 작성하세요

70 20 32 40 33 60 32 33 90 50

*/

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

 

int main(void)

{

        vector<int> v;

        v.push_back(70);

        v.push_back(20);

        v.push_back(32);

        v.push_back(40);

        v.push_back(33);

        v.push_back(60);

        v.push_back(32);

        v.push_back(33);

        v.push_back(90);

        v.push_back(50);

 

        cout << ": ";

        for (vector<int>::size_type i = 0; i < v.size(); i++)

               cout << v[i] << " ";

        cout << endl;

 

        sort(v.begin(), v.end()); //우선 오름차순으로 정렬 후 중복 제거

        vector<int>::iterator iter_end;

        iter_end = unique(v.begin(), v.end());

        cout << ": ";

        for (vector<int>::iterator iter = v.begin(); iter != iter_end; iter++)

               cout << *iter << " ";

        cout << endl;

 

        return 0;

}

[5번]

/*

다음 vector1의 원소를 vector1에 복사하고 vector1 vector2의 원소의 합을 vector3에 출력하는 코드를 작성하세요

vector1: 10 20 30 40 50 60 70 80 90 100

*/

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

 

int main(void)

{

        vector<int> vector1;

        vector1.push_back(10);

        vector1.push_back(20);

        vector1.push_back(30);

        vector1.push_back(40);

        vector1.push_back(50);

        vector1.push_back(60);

        vector1.push_back(70);

        vector1.push_back(80);

        vector1.push_back(90);

        vector1.push_back(100);

 

        cout << "vector1: ";

        for (vector<int>::size_type i = 0; i < vector1.size(); i++)

               cout << vector1[i] << " ";

        cout << endl;

 

        vector<int> vector2(10);

        vector<int>::iterator iter;

        iter = copy(vector1.begin(), vector1.end(), vector2.begin());

        cout << "복사 잘 되었는지 확인" << endl;

        cout << "vector2: ";

        for (vector<int>::size_type i = 0; i < vector2.size(); i++)

               cout << vector2[i] << " ";

        cout << endl;

 

        vector<int> vector3(10);

        vector<int>::iterator iter_end = transform(vector1.begin(), vector1.end(), vector2.begin(), vector3.begin(), plus<int>()); //vector1 vector2의 합

        cout << "vector3: ";

        for(iter=vector3.begin(); iter!=iter_end; iter++)

               cout << *iter << " ";

        cout << endl;

        return 0;

}

[6번]

/*

다음 v1 v2 v3로 합병하는 코드를 작성하세요

v1: 50 10 30 80 60 N

v2: 20 70 40 N

*/

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

 

int main(void)

{

        vector<int> v1, v2;

        v1.push_back(50);

        v1.push_back(10);

        v1.push_back(30);

        v1.push_back(80);

        v1.push_back(60);

 

        cout << "v1: ";

        for (vector<int>::size_type i = 0; i < v1.size(); i++)

               cout << v1[i] << " ";

        cout << endl;

 

        //합병을 위해서는 정렬이 되어있어야한다

        sort(v1.begin(), v1.end());

 

        v2.push_back(20);

        v2.push_back(70);

        v2.push_back(40);

 

        cout << "v2: ";

        for (vector<int>::size_type i = 0; i < v2.size(); i++)

               cout << v2[i] << " ";

        cout << endl;

 

        sort(v2.begin(), v2.end());

 

        vector<int> v3(8);

        vector<int>::iterator iter_end;

        iter_end = merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());

        cout << "v3: ";

        for (vector<int>::size_type i = 0; i < v3.size(); i++)

               cout << v3[i] << " ";

        cout << endl;

 

        return 0;

}

[7번]

/*

다음에서 전 vector를 후 vector의 순차열로 만드는 코드를 작성하세요

: 10 20 30 40 50 60 70 80 90 100 N

: 70 80 90 100 10 20 30 40 50 60 N

*/

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

 

int main(void)

{

        vector<int> v;

        v.push_back(10);

        v.push_back(20);

        v.push_back(30);

        v.push_back(40);

        v.push_back(50);

        v.push_back(60);

        v.push_back(70);

        v.push_back(80);

        v.push_back(90);

        v.push_back(100);

 

        cout << ": ";

        for (vector<int>::size_type i = 0; i < v.size(); i++)

               cout << v[i] << " ";

        cout << endl;

 

        vector<int>::iterator middle = v.begin() + 6; //70부터 시작해야하므로

        rotate(v.begin(), middle, v.end());

        cout << ": ";

        for (vector<int>::size_type i = 0; i < v.size(); i++)

               cout << v[i] << " ";

        cout << endl;

        return 0;

}

[8번]

/*

다음 v1+v2의 합 집합을 v3에 출력하는 코드를 작성하세요

v1: 50 10 30 80 60 N

v2: 20 70 40 N

*/

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

 

int main(void)

{

        vector<int> v1, v2;

        v1.push_back(50);

        v1.push_back(10);

        v1.push_back(30);

        v1.push_back(80);

        v1.push_back(60);

 

        cout << "v1: ";

        for (vector<int>::size_type i = 0; i < v1.size(); i++)

               cout << v1[i] << " ";

        cout << endl;

 

        //합집합을 위해 정렬

        sort(v1.begin(), v1.end());

 

        v2.push_back(20);

        v2.push_back(70);

        v2.push_back(40);

 

        cout << "v2: ";

        for (vector<int>::size_type i = 0; i < v2.size(); i++)

               cout << v2[i] << " ";

        cout << endl;

 

        sort(v2.begin(), v2.end());

 

        vector<int> v3(10);

        vector<int>::iterator iter_end;

 

        iter_end = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());

        cout << "v3: ";

        for (vector<int>::iterator iter = v3.begin(); iter != iter_end; iter++)

               cout << *iter << " ";

        cout << endl;

 

        return 0;

 

}



개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 뇌를 자극하는 C++ STL

반응형