C++ std::copy 用法與範例

本篇 ShengYu 介紹 C++ std::copy 用法與範例,在 C++ 中,標準庫提供了許多強大的工具和函數,其中之一就是 std::copy。這個函數讓我們能夠輕鬆地將一個範圍的元素從一個位置複製到另一個位置。在這篇文章中,我們將探索 std::copy 的基本用法以及一些更進階的技巧。

C++ std::copy 除了基本用法外,它還可以接受不同的迭代器類型,從而可以在不同類型的容器之間進行複製。它還支援複製部分範圍以及將元素插入到容器的特定位置。std::copy 是 C++ 中進行元素複製操作的一個通用且強大的工具。

C++ std::copy 基本用法

首先,讓我們看一下 std::copy 的基本用法。這個函數定義在 <algorithm> 標頭檔案中。它是標準庫中的一個演演算法,使用起來非常簡單:

1
2
3
#include <algorithm>

std::copy(起始位置, 結束位置, 目標位置);

其中,起始位置結束位置 定義了源範圍的區間,而 目標位置 定義了目標範圍的起始位置。

std::copy 複製 vector 到 vector

如果你有兩個 std::vecotr sourcedestination,你可以將 source 的一部分元素複製到 destination,讓我們透過一個簡單的範例來理解這個概念:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
std::vector<int> source = {1, 2, 3, 4, 5};
std::vector<int> destination(5); // 預先分配大小

std::copy(source.begin(), source.end(), destination.begin());

for (const auto& num : destination) {
std::cout << num << " ";
}
std::cout << std::endl;

return 0;
}

在這個範例中,我們將 source 中的元素複製到了 destination 中,最終輸出如下,

1
1 2 3 4 5

std::copy 複製陣列到 vector

當你想要複製一個陣列到一個 std::vector 時,你可以使用 std::copy 函式。以下是一個簡單的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
int arr[] = {2, 4, 6, 8, 10};
std::vector<int> vec(5); // 預先分配容器大小

std::copy(std::begin(arr), std::end(arr), vec.begin());

for (int num : vec) {
std::cout << num << " ";
}
std::cout << std::endl;

return 0;
}

這個程式用 std::copy 把陣列 arr 的內容複製到向量 vec 中,最終輸出如下,

1
2 4 6 8 10

除了基本用法外,std::copy 還有一些進階的用法,讓我們一起繼續往下閱讀看看。

std::copy 使用 std::back_inserter 插入元素到容器末尾

在這個範例中,我們建立了一個空的 destination 向量,然後使用 std::copy 將 source 向量中的元素複製到 destination 中。不同的是,我們將 std::back_inserter(destination) 傳遞給 std::copy 作為目標位置。std::back_inserter 是一個插入迭代器,當它被用於 std::copy 時,它會自動插入元素到容器 destination 的末尾。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <algorithm>

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

std::copy(source.begin(), source.end(), std::back_inserter(destination));

for (const auto& num : destination) {
std::cout << num << " ";
}
std::cout << std::endl;

return 0;
}

std::copy 使用 std::ostream_iterator 將元素複製到輸出流

這個範例中,我們使用 std::copy 將 source 向量中的元素複製到 std::ostream_iterator<int>,這是一個迭代器,它將元素寫入到輸出流 std::cout 中。std::ostream_iterator<int>(std::cout, " ") 中的 " " 空格是指定元素之間的分隔符號,你也可以改用 , 逗號作為分隔符號。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

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

std::copy(source.begin(), source.end(), std::ostream_iterator<int>(std::cout, " "));
//std::copy(source.begin(), source.end(), std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;

return 0;
}

std::copy 複製部分範圍到不同位置

在這個範例中,我們使用 std::copy 將 source 向量中索引從 1 到 3 的元素(不包括索引為 4 的元素)複製到 destination 向量的開始位置。這顯示了 std::copy 可以用於複製範圍的子集到另一個容器的開始位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <algorithm>

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

std::copy(source.begin()+1, source.begin()+4, destination.begin());

for (const auto& num : destination) {
std::cout << num << " ";
}
std::cout << std::endl;

return 0;
}

最終輸出如下,

1
2 3 4

以上這些範例展示了 std::copy 的不同用法,從將元素複製到容器中,到將元素複製到輸出流中,以及從一個範圍複製子集到另一個容器。這些是 std::copy 的一些常見用法範例,它提供了許多靈活的選項,讓我們能夠輕鬆地處理各種複製需求。從基本的複製到不同容器,到複製到輸出流,再到複製部分範圍,std::copy 讓我們的程式碼更加優雅和易於閱讀。根據具體的需求,你可以選擇適合的方法來使用它。

以上就是 C++ std::copy 用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其它相關文章推薦
C/C++ 新手入門教學懶人包
std::back_inserter 用法與範例
std::front_inserter 用法與範例
std::string 用法與範例
std::vector 用法與範例
std::sort 用法與範例