码迷,mamicode.com
首页 > 编程语言 > 详细

Beautiful C++ STL

时间:2020-06-07 21:34:14      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:sorted   stl   math   element   osi   match   fun   source   ping   

Headers

<algorithm>
<vector> <array> <list> <stack> <queue> <set> <map> <unordered_set> <unordered_map>
<string>
<iterator>
<utility> <tuple>
<numeric>
<complex> <cmath>
<regex> <chrono>

Count and Count_if

vector<int> vec{ 1,0,2,5,-1,6,1 };
int zeros = count(vec.begin(), vec.end(), 0);
int ones = count(begin(vec), end(vec), 1);

int odds = count_if(vec.begin(), vec.end(), [](int elem) {return elem % 2 != 0; });

bool allof, nonof, anyof;
allof = all_of(vec.begin(), vec.end(), [](int elem) {return elem % 2 != 0; });
nonof = none_of(vec.begin(), vec.end(), [](int elem) {return elem % 2 != 0; });
anyof = any_of(vec.begin(), vec.end(), [](int elem) {return elem % 2 != 0; });

Finding

vector<int> vec{ 1,0,2,5,-1,6,1,8};

auto result = find(vec.begin(), vec.end(), 6);
if (result != vec.end())
{
std::cout << "find " << *result << endl;
}
result = find(result, vec.end(), 1);
if (result != vec.end())
{
std::cout << "find " << *result << endl;
}

auto result = find_if(vec.begin(), vec.end(), [](int elem) {return elem % 2 != 0; });
result = find_if_not(vec.begin(), vec.end(), [](int elem) {return elem % 2 != 0; });

vector<int> primes{ 1,2,3,5,7,11,13 };
result = find_first_of(vec.begin(),vec.end(),primes.begin(),primes.end());

vector<int> subsequence{ 6,1 };
result = search(vec.begin(), vec.end(), subsequence.begin(), subsequence.end());

result = find_end(vec.begin(), vec.end(), subsequence.begin(), subsequence.end());

result = search_n(vec.begin(),vec.end(),6,1);

result = adjacent_find(vec.begin(),vec.end());

Sorting

vector<int> vec{ 4,1,0,1,-2,4,5,7,-6,0,0,9,8 };
auto vec2 = vec;
sort(vec.begin(),vec.end());
sort(vec.begin(), vec.end(), [](int elem1, int elem2) {return elem1 > elem2; });
sort(vec.begin(), vec.end(), [](int elem1, int elem2) {return abs(elem1) > abs(elem2); });

bool sorted = is_sorted(vec.begin(), vec.end());

vector<Student> students{
{"Anne",1,90},
{"Tony",5,89},
{"Alice",4,76},
{"Jike",3,90},
{ "Tom",2,60}
};

stable_sort(students.begin(), students.end(), [](const Student& st1, const Student& st2) {return st1.score > st2.score; });

Find the Largest or Smallest

vector<int> vec{ 2,0,-2,9,-5,-4,3 };
int max_value = *max_element(vec.begin(), vec.end());
int min_value = *min_element(vec.begin(), vec.end());

sort(vec.begin(), vec.end());
//@ lower_bound,upper_bound 必须有序,因为采用二分实现
int negative = *lower_bound(vec.begin(), vec.end(), 0);	//@ 第一个不下于0
int positive = *upper_bound(vec.begin(), vec.end(), 0); //@ 第一个大于0

int max_value_1 = *(vec.end() - 1);
int min_value_1 = *vec.begin();

Partial Sorting

vector<int> vec{ 4,1,0,1,-2,4,5,7,-6,0,0,9,8 };
partial_sort(vec.begin(),vec.begin()+5,vec.end());

int breakpoint = *is_sorted_until(vec.begin(), vec.end());

vector<int> vec2(6);
partial_sort_copy(vec.begin(),vec.end(),vec2.begin(),vec2.end());

vector<int> vec3{ 6,1,2,7,2,5,1,0,9,1 };
nth_element(vec3.begin(), vec3.begin() + 5, vec3.end());

Shuffle

vector<int> vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
random_device rd;
mt19937 generator(rd());
shuffle(vec.begin(),vec.end(),generator);

Comparing

vector<int> va{ 1,2,3,4,5 };
vector<int> vb{ 1,2,0,4 };
bool same = equal(va.begin(), va.end(), vb.begin(), vb.end());

auto first_change = mismatch(va.begin(),va.end(),vb.begin());
int avalue = *(first_change.first);
int bvalue = *(first_change.second);
auto distance = first_change.first - va.begin();

Total the Elements

vector<int> vec{ 1,2,0,4,5 };
int total = accumulate(vec.begin(),vec.end(),0);
total = accumulate(vec.begin(), vec.end(), 0, [](int total,int i) {if (i % 2 == 0)return total + i; return total; });

string s = accumulate(vec.begin(), vec.end(), string{ "the number is:" }, []
(const string& total, int i) {return total + " " + to_string(i); });

for_each

vector<int> vec{ 1,6,3,9,7,2,0,1,2,5 };
for_each(vec.begin(), vec.end(), [](int& elem) {elem = 2; });

Copying

vector<int> source{ 3,6,1,0,-2,5 };
vector<int> dest(source.size());
copy(source.begin(), source.end(),dest.begin());

vector<int> dest2(source.size());
copy_if(source.begin(), source.end(), dest2.begin(), [](int elem) {return elem % 2 == 0; });

copy_n(source.begin(),5,dest2.begin());

copy_backward(source.begin(), source.end(),dest2.end());

vector<int> dest3(source.size());
move(source.begin(), source.begin()+3, dest3.begin());
move_backward(source.begin(), source.end(),dest3.end());

Removing Elements

vector<int> vec{ 1,5,4,2,9,7 };
auto newend = remove(vec.begin(), vec.end(),2);
vec.erase(newend,vec.end());
vec.erase(remove(vec.begin(),vec.end(),7),vec.end());

vector<int> vec2{1,6,2,7,8,9 };
auto new_end2 = remove_if(vec2.begin(), vec2.end(), [](int elem) {return elem % 2 == 0; });
vec2.erase(new_end2, vec2.end());

Creating and Filling Collections

vector<int> vec(10);

fill(vec.begin(), vec.end(), 1);
fill_n(vec.begin(), 5, 2);

iota(vec.begin(), vec.end(), 0);

int index = 10;
generate(vec.begin(), vec.end(), [&index]() {return --index; });
auto source = vec;

index = 1;
generate_n(vec.begin(), 6,[&index]() { return index *= 2; });

Replacing Values

vector<int> vec{ 1,5,4,2,9,7 };
replace(vec.begin(), vec.end(), 2, 88);
replace_if(vec.begin(), vec.end(), [](int elem) {return elem < 16; }, 99);

Transform

vector<int> source{ 1,5,4,2,9,7 };
transform(source.begin(), source.end(), source.begin(), [](int elem) { return elem * 2; });
transform(source.begin(), source.end() - 1, source.begin() + 1, [](int elem) { return elem * 3; });

iota(source.begin(), source.end(),1);
vector<int> vec(10);
transform(source.begin(), source.end(), vec.begin(), vec.begin(), [](int elem1, int elem2) 
{return elem1 + elem2; });

Eliminating Duplicates

std::vector<int> v{ 1,2,1,1,3,3,3,4,5,4 };
//@ unique 只能删除毗邻的相同元素,
//@ 为了删除所有的重复元素,需要先进行排序
std::sort(v.begin(),v.end());
auto source = v;

auto last = std::unique(v.begin(), v.end());
v.erase(last, v.end());

std::vector<int> v2;
unique_copy(source.begin(), source.end(), std::back_inserter(v2));

Reversing and Swapping

vector<int> vec{ 1,4,2,-3,5,7,2,1,0,6 };
string sentence = "hello,world";
reverse(sentence.begin(), sentence.end());

iter_swap(vec.begin(), vec.end()-1);

string orig = "              ";
reverse_copy(sentence.begin(), sentence.end(), orig.begin());

Pre-allocating Vectors is No Fun

vector<int> vec(10);
fill(vec.begin(), vec.end(),1);
fill_n(vec.begin(),6,2);
iota(vec.begin(),vec.end(),2);

Use a Different Iterator

vector<int> v2;
fill_n(back_inserter(v2), 6, 2);
generate_n(back_inserter(v2), 10, [n = 0]()mutable{return n++; });

deque<int> q3;
fill_n(front_inserter(q3), 6, 2);
generate_n(front_inserter(q3), 10, [n = 0]()mutable{return n++; });

Changing Values with Iterators

vector<int> v1 = {0,3,5,6,1,2,9,2};
vector<int> v2;
transform(v1.begin(), v1.end(), back_inserter(v2), [](int elem) {return elem * 2; });

Swapping

int a{ 4 }, b{ 6 };
swap(a, b);

int a{ 4 }, b{ 6 };
swap(a, b);

vector<int> evens{2,4,6,8,10};
vector<int> odds{1,3,5,7,9};
iter_swap(evens.begin(),odds.begin());
iter_swap(evens.begin(), find(odds.begin(), odds.end(), 3));

swap(evens[0],odds[0]);

swap_ranges(evens.begin(), evens.end(),odds.begin());

Rotate

vector<int> vec(6);
iota(vec.begin(),vec.end(),1);

auto two = std::find(vec.begin(),vec.end(),2);
auto four = std::find(vec.begin(), vec.end(), 4);
rotate(two,four,four+1);

Partition

vector<int> vec(8);
iota(vec.begin(), vec.end(), 1);

auto selected = std::stable_partition(vec.begin(), vec.end(), [](int elem) {return elem % 2 != 0; });

Beautiful C++ STL

标签:sorted   stl   math   element   osi   match   fun   source   ping   

原文地址:https://www.cnblogs.com/xiaojianliu/p/13062173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!