Wednesday, November 2, 2016

sort partial_sort and stable and copy sementics

(1) Sort is the starting algo, stable means keep previous order so it only follows
(2) back_inserter convert object to an iterator.
(3) partition move true item in a container to front. false to back.
(4) partial/partition_copy moves and output two iterators.
(5) partial_sort means take 8 and sort until find top 5. so out_copy would be size 5

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>

using namespace std;

struct Person
{
 string FirstName;
 string MiddleName;
 string LastName;
};

int main()
{
 vector<Person> v{ {"Jason","P","Pierepaul"},{"Rob","C","Nokowich"},{"Jane","X","Smith"},{"Joe","A","Smith" }};

 sort(v.begin(), v.end(), [](const Person& a, const Person& b) {return a.MiddleName < b.MiddleName; });
 stable_sort(v.begin(), v.end(), [](const Person& a, const Person& b) {return a.FirstName<b.FirstName; });
 stable_sort(v.begin(), v.end(), [](const Person& a, const Person& b) {return a.LastName<b.LastName; });

 vector<int> v2 = { 42,17,89,22,34,78,63,12,57,99 };
 partial_sort(v2.begin(), v2.begin() + 5, v2.begin() + 8, greater<int>());

 vector<int> out_copy(5);
 partial_sort_copy(v2.begin(), v2.begin()+8, out_copy.begin(), out_copy.end(),greater<int>());

 vector<int> v3 = { 12,89,31,18,7,72,69,50,49,50,51,49 };
 vector<int>::iterator parti_itr = partition(v3.begin(), v3.end(), [](const int i) { return i < 50; });

 vector<int> true_parti, false_parti;
 partition_copy(v3.begin(), v3.end(), back_inserter(true_parti), back_inserter(false_parti), [](const int i) {return i < 50; });


 std::string s;
 getline(cin, s);
    return 0;
}


No comments:

Post a Comment