std::copy、std::copy_if 用法

std::copy

描述:
       拷贝容器指定范围的元素到另一容器指定位置中。

定义:

template< class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );

template< class InputIt, class OutputIt >
constexpr OutputIt copy( InputIt first, InputIt last, OutputIt d_first );

可能的实现:

template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last, OutputIt d_first)
{
    while (first != last) 
    {
        *d_first++ = *first++;
    }
    return d_first;
}

参数:
       first - 源容器的起始范围迭代器
       last - 源容器的终止范围迭代器
       d_first - 目标容器的起始范围迭代器

返回值:
       目标容器中最后复制元素的下个元素的输出迭代器。

示例:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
	std::string s1 = "Hello world!";
	
	//使用std::back_inserter
	std::string s2;
	std::copy(s1.begin(), s1.end(), std::back_inserter(s2));
	std::cout << s2 << std::endl;//Hello world!

	//使用resize + std::begin
	std::string s3;
	s3.resize(s1.size());
	std::copy(s1.begin(),s1.end(),s3.begin());
	std::cout << s3 << std::endl;//Hello world!
}

std::copy_if

描述:
       拷贝容器指定范围的元素到另一容器指定位置中,并增加筛选条件。

定义:

template< class InputIt, class OutputIt, class UnaryPredicate >
OutputIt copy_if( InputIt first, InputIt last,
                  OutputIt d_first,
                  UnaryPredicate pred );

template< class InputIt, class OutputIt, class UnaryPredicate >
constexpr OutputIt copy_if( InputIt first, InputIt last,
                            OutputIt d_first,
                            UnaryPredicate pred );

可能的实现:

template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt copy_if(InputIt first, InputIt last, 
                 OutputIt d_first, UnaryPredicate pred)
{
    while (first != last) 
    {
        if (pred(*first))
            *d_first++ = *first;
        ++first;
    }
    return d_first;
}

参数:
       first - 源容器的起始范围迭代器
       last - 源容器的终止范围迭代器
       d_first - 目标容器的起始范围迭代器
       pred - 对所要求的元素则返回 ​true 的一元谓词。(筛选条件)

返回值:
       目标容器中最后复制元素的下个元素的输出迭代器。

示例:

int main()
{
	std::string s1 = "Hello world!";
	std::string s3;

	std::copy_if(s1.begin(), s1.end(), std::back_inserter(s3),
		[](char c) { return c == 'l' || c == 'o'; });//只拷贝l和o元素
	std::cout << s3 << std::endl;//llool
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这些都是C++ STL中的算法,用于对容器中的元素进行处理。下面分别给出每个算法的用法和示例: 1. std::remove: 从容器中删除指定的元素 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 2, 4, 2, 5}; auto it = std::remove(vec.begin(), vec.end(), 2); // 删除所有值为2的元素 vec.erase(it, vec.end()); // 删除多余的元素 for (auto x : vec) { std::cout << x << " "; // 输出:1 3 4 5 } return 0; } ``` 2. std::copy_if: 将符合条件的元素复制到新的容器中 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; std::vector<int> odd_vec; std::copy_if(vec.begin(), vec.end(), std::back_inserter(odd_vec), [](int x) { return x % 2 == 1; }); // 复制所有奇数到odd_vec中 for (auto x : odd_vec) { std::cout << x << " "; // 输出:1 3 5 } return 0; } ``` 3. std::partition: 将容器中的元素按照条件分为两部分,满足条件的在前面,不满足条件的在后面 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; auto it = std::partition(vec.begin(), vec.end(), [](int x) { return x % 2 == 1; }); // 将容器中的元素按照奇偶分为两部分 for (auto x : vec) { std::cout << x << " "; // 输出:1 5 3 4 2 } return 0; } ``` 4. std::unique: 删除容器中相邻的重复元素 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 2, 3, 3, 3, 4, 5, 5}; auto it = std::unique(vec.begin(), vec.end()); // 删除相邻的重复元素 vec.erase(it, vec.end()); // 删除多余的元素 for (auto x : vec) { std::cout << x << " "; // 输出:1 2 3 4 5 } return 0; } ``` 5. std::unique_copy: 将容器中相邻的重复元素复制到新的容器中 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 2, 3, 3, 3, 4, 5, 5}; std::vector<int> unique_vec; std::unique_copy(vec.begin(), vec.end(), std::back_inserter(unique_vec)); // 将相邻的重复元素复制到unique_vec中 for (auto x : unique_vec) { std::cout << x << " "; // 输出:1 2 3 4 5 } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值