primer C++笔记
查找对象的算法
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
//find(beg, end, val);
//find_if(beg, end, unaryPred);
//find_if_not(beg, end, unaryPred)
void test01()
{
int n1 = 3;
int n2 = 5;
std::vector<int> v{ 0, 1, 2, 3, 4 };
auto result1 = std::find(std::begin(v), std::end(v), n1);
auto result2 = std::find(std::begin(v), std::end(v), n2);
if (result1 != std::end(v)) {
std::cout << "v contains: " << n1 << '\n';
}
else {
std::cout << "v does not contain: " << n1 << '\n';
}
if (result2 != std::end(v)) {
std::cout << "v contains: " << n2 << '\n';
}
else {
std::cout << "v does not contain: " << n2 << '\n';
}
/*v contains : 3
v does not contain : 5*/
}
//count(beg, end, val);
//count_if(beg, end, unaryPred)
void test02()
{
std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
// 确定 std::vector 中有多少个整数匹配目标值。
int target1 = 3;
int target2 = 5;
int num_items1 = std::count(v.begin(), v.end(), target1);
int num_items2 = std::count(v.begin(), v.end(), target2);
std::cout << "number: " << target1 << " count: " << num_items1 << '\n';
std::cout << "number: " << target2 << " count: " << num_items2 << '\n';
// 用 lambda 表达式计量能被 3 整除的元素数。
int num_items3 = std::count_if(v.begin(), v.end(), [](int i) {return i % 3 == 0; });
std::cout << "number divisible by three: " << num_items3 << '\n';
//number: 3 count : 2
// number : 5 count : 0
// number divisible by three : 3
}
//all_of(beg, end, unaryPred);
//any_of(beg, end, unaryPred);
//none_of(beg, end, unaryPred);
#include <functional>
#include <numeric>
void test03()
{
std::vector<int> v(10, 2);
std::partial_sum(v.cbegin(), v.cend(), v.begin());
std::cout << "Among the numbers: ";
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
if (std::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; })) {
std::cout << "All numbers are even\n";
}
if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<int>(),
std::placeholders::_1, 2))) {
std::cout << "None of them are odd\n";
}
struct DivisibleBy
{
const int d;
DivisibleBy(int n) : d(n) {}
bool operator()(int n) const { return n % d == 0; }
};
if (std::any_of(v.cbegin(), v.cend(), DivisibleBy(7))) {
std::cout << "At least one number is divisible by 7\n";
}
/*Among the numbers : 2 4 6 8 10 12 14 16 18 20
All numbers are even
None of them are odd
At least one number is divisible by 7*/
}
//adjacent_find(beg, end);
//adjacent_find(beg, end, binaryPred);
void test04()
{
std::vector<int> v1{ 0, 1, 2, 3, 40, 40, 41, 41, 5 };
auto i1 = std::adjacent_find(v1.begin(), v1.end());
if (i1 == v1.end()) {
std::cout << "no matching adjacent elements\n";
}
else {
std::cout << "the first adjacent pair of equal elements at: "
<< std::distance(v1.begin(), i1) << '\n';
}
auto i2 = std::adjacent_find(v1.begin(), v1.end(), std::greater<int>());
if (i2 == v1.end()) {
std::cout << "The entire vector is sorted in ascending order\n";
}
else {
std::cout << "The last element in the non-decreasing subsequence is at: "
<< std::distance(v1.begin(), i2) << '\n';
}
/*The first adjacent pair of equal elements at : 4
The last element in the non - decreasing subsequence is at : 7*/
}
//search_n(beg, end, count, val);
//search_n(beg, end, count, val, binaryPred);
template <class Container, class Size, class T>
bool consecutive_values(const Container& c, Size count, const T& v)
{
return std::search_n(std::begin(c), std::end(c), count, v) != std::end(c);
}
void test05()
{
const char sequence[] = "1001010100010101001010101";
std::cout << std::boolalpha;
std::cout << "Has 4 consecutive zeros: "
<< consecutive_values(sequence, 4, '0') << '\n';
std::cout << "Has 3 consecutive zeros: "
<< consecutive_values(sequence, 3, '0') << '\n';
/*Has 4 consecutive zeros : false
Has 3 consecutive zeros : true*/
}
//search(beg1, end1, beg2, end2);
//search(beg1, end1, beg2, end2, binaryPred);
template<typename Container>
bool in_quote(const Container& cont, const std::string& s)
{
return std::search(cont.begin(), cont.end(), s.begin(), s.end()) != cont.end();
}
void test06()
{
std::string str = "why waste time learning, when ignorance is instantaneous?";
// str.find() 也能使用
std::cout << std::boolalpha << in_quote(str, "learning") << '\n'
<< in_quote(str, "lemming") << '\n';
std::vector<char> vec(str.begin(), str.end());
std::cout << std::boolalpha << in_quote(vec, "learning") << '\n'
<< in_quote(vec, "lemming") << '\n';
// C++17 重载演示:
/*std::string in = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
" sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
std::string needle = "pisci";
auto it = std::search(in.begin(), in.end(),
std::boyer_moore_searcher(
needle.begin(), needle.end()));
if (it != in.end())
std::cout << "The string " << needle << " found at offset "
<< it - in.begin() << '\n';
else
std::cout << "The string " << needle << " not found\n";*/
/*true
false
true
false
The string pisci found at offset 43*/
}
//find_first_of(beg1, end1, beg2, end2);
//find_first_of(beg1, end1, beg2, end2, binaryPred);
void test07()
{
std::vector<int> v{ 0, 2, 3, 25, 5 };
std::vector<int> t{ 3, 19, 10, 2 };
auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end());
if (result == v.end()) {
std::cout << "no elements of v were equal to 3, 19, 10 or 2\n";
}
else {
std::cout << "found a match at "
<< std::distance(v.begin(), result) << "\n";
}
//found a match at 1
}
//find_end(beg1, end1, beg2, end2);
//find_end(beg1, end1, beg2, end2, binaryPred);
void test08()
{
std::vector<int> v{ 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
std::vector<int>::iterator result;
std::vector<int> t1{ 1, 2, 3 };
result = std::find_end(v.begin(), v.end(), t1.begin(), t1.end());
if (result == v.end()) {
std::cout << "subsequence not found\n";
}
else {
std::cout << "last subsequence is at: "
<< std::distance(v.begin(), result) << "\n";
}
std::vector<int> t2{ 4, 5, 6 };
result = std::find_end(v.begin(), v.end(), t2.begin(), t2.end());
if (result == v.end()) {
std::cout << "subsequence not found\n";
}
else {
std::cout << "last subsequence is at: "
<< std::distance(v.begin(), result) << "\n";
}
/*last subsequence is at : 8
subsequence not found*/
}