天天看點

泛型程式設計與STL 第十二章 [會改變操作對象之内容]的算法

作者:明政面朝大海春暖花開

在C++中,泛型程式設計是一種程式設計範式,它允許我們編寫通用的代碼,可以适用于不同類型的資料。STL(Standard Template Library)是C++标準庫中的一個重要組成部分,提供了一系列泛型算法和資料結構,可以大大簡化程式設計任務。

下面是一些與您提到的主題相關的C++代碼示例:

  1. 線性查找:
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> nums = {1, 2, 3, 4, 5};

    auto it = std::find(nums.begin(), nums.end(), 3);
    if (it != nums.end()) {
        // 找到了
    } else {
        // 沒找到
    }

    return 0;
}
           
  1. 子序列比對:
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> nums = {1, 2, 3, 4, 5};
    std::vector<int> subsequence = {3, 4};

    auto it = std::search(nums.begin(), nums.end(), subsequence.begin(), subsequence.end());
    if (it != nums.end()) {
        // 找到了子序列
    } else {
        // 沒找到子序列
    }

    return 0;
}
           
  1. 計算元素個數:
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> nums = {1, 2, 3, 4, 5};

    int count = std::count(nums.begin(), nums.end(), 3);
    // count 等于 1

    return 0;
}
           
  1. for_each:
#include <algorithm>
#include <iostream>
#include <vector>

void printNumber(int num) {
    std::cout << num << " ";
}

int main() {
    std::vector<int> nums = {1, 2, 3, 4, 5};

    std::for_each(nums.begin(), nums.end(), printNumber);  // 輸出:1 2 3 4 5

    return 0;
}
           
  1. 比較兩個Ranges:
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> nums1 = {1, 2, 3};
    std::vector<int> nums2 = {1, 2, 3};

    bool equal = std::equal(nums1.begin(), nums1.end(), nums2.begin(), nums2.end());
    // equal 為 true

    return 0;
}
           
  1. 最大值與最小值:
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> nums = {1, 2, 3, 4, 5};

    auto maxNum = std::max_element(nums.begin(), nums.end());
    // maxNum 指向最大值元素的疊代器,此例中指向 5

    auto minNum = std::min_element(nums.begin(), nums.end());
    // minNum 指向最小值元素的疊代器,此例中指向 1

    return 0;
}
           

這些示例代碼展示了如何在C++中使用泛型程式設計和STL來實作線性查找、子序列比對、計算元素個數、周遊元素、比較兩個Ranges以及找到最大值和最小值。請注意,這隻是一些簡單的示例,您可以根據自己的需求進行更複雜的操作。

泛型程式設計是一種程式設計範式,它允許我們編寫可以适用于不同資料類型的通用代碼。STL(Standard Template Library)是C++标準庫中的一個重要組成部分,提供了許多泛型算法和資料結構,以簡化和加速C++程式的開發。

下面是一些與STL相關的常見操作和算法的示例:

  1. 拷貝某個區間:
#include <algorithm>
#include <vector>

std::vector<int> source = {1, 2, 3, 4, 5};
std::vector<int> destination(source.size());

std::copy(source.begin(), source.end(), destination.begin());
           
  1. 互換元素:
#include <algorithm>
#include <vector>

std::vector<int> numbers = {1, 2, 3};
std::swap(numbers[0], numbers[1]);
           
  1. transform:
#include <algorithm>
#include <vector>

std::vector<int> source = {1, 2, 3};
std::vector<int> destination(source.size());

std::transform(source.begin(), source.end(), destination.begin(), [](int x) {
    return x * 2;
});
           
  1. 替換元素:
#include <algorithm>
#include <vector>

std::vector<int> numbers = {1, 2, 3, 4, 5};
std::replace(numbers.begin(), numbers.end(), 3, 10);
           
  1. 充填整個區間:
#include <algorithm>
#include <vector>

std::vector<int> numbers(5);
std::fill(numbers.begin(), numbers.end(), 42);
           
  1. 移除元素:
#include <algorithm>
#include <vector>

std::vector<int> numbers = {1, 2, 3, 4, 5};
numbers.erase(std::remove(numbers.begin(), numbers.end(), 3), numbers.end());
           
  1. 排列算法:
#include <algorithm>
#include <vector>

std::vector<int> numbers = {3, 1, 2};
std::sort(numbers.begin(), numbers.end());
           
  1. 分割:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

std::vector<int> numbers = {1, 2, 3, 4, 5};
std::vector<int> evenNumbers;
std::vector<int> oddNumbers;

std::partition_copy(numbers.begin(), numbers.end(), std::back_inserter(evenNumbers), std::back_inserter(oddNumbers), [](int x) {
    return x % 2 == 0;
});

// 輸出偶數
for (const auto& number : evenNumbers) {
    std::cout << number << " ";
}
std::cout << std::endl;

// 輸出奇數
for (const auto& number : oddNumbers) {
    std::cout << number << " ";
}
std::cout << std::endl;
           
  1. 随機重排與抽樣:
#include <algorithm>
#include <iostream>
#include <vector>

std::vector<int> numbers = {1, 2, 3, 4, 5};
std::random_shuffle(numbers.begin(), numbers.end());

// 輸出随機重排後的結果
for (const auto& number : numbers) {
    std::cout << number << " ";
}
std::cout << std::endl;

std::vector<int> sample(3);
std::sample(numbers.begin(), numbers.end(), sample.begin(), sample.size(), std::mt19937{std::random_device{}()});

// 輸出抽樣結果
for (const auto& number : sample) {
    std::cout << number << " ";
}
std::cout << std::endl;
           
  1. 一般化之數值算法:
#include <algorithm>
#include <iostream>
#include <vector>

std::vector<int> numbers = {1, 2, 3, 4, 5};
int sum = std::accumulate(numbers.begin(), numbers.end(), 0);

std::cout << "Sum: " << sum << std::endl;
           

以上是一些STL中常見的操作和算法的示例,它們可以幫助您更好地了解泛型程式設計和STL在C++中的應用。請注意,這隻是一小部分示例,STL還提供了許多其他有用的算法和資料結構,可以根據需要進行進一步學習和探索。

繼續閱讀