当前位置:   article > 正文

C++ STL:algorithm 中的std::copy std::copy_backward函数

std::copy

copy函数:

1. copy函数的模板形式:

template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
  • 1
  • 2

2. 作用:

将范围[first,last)的数据拷贝到result中。注意last地址不拷贝
在这里插入图片描述
函数模板的功能等价于:

template<class InputIterator, class OutputIterator>
  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{
  while (first!=last) {
    *result = *first;
    ++result; ++first;
  }
  return result;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3. 输入参数和返回值:

first、last:对应的是被拷贝序列的首地址和尾地址的后一个地址
Result:目标内存的首地址。
返回值:返回被拷贝序列的尾地址的后一个地址,既last

4. 使用实例

#include <iostream> 
#include <algorithm>
#include <vector>

using namespace std;

int main() 
{
	int myints[] = { 10, 20, 30, 40, 50, 60, 70 };//被拷贝的数据

	cout << "1.拷贝到vector容器中:" << std::endl;
	std::vector<int> myvector(10);

	std::vector<int>::iterator rtnIt = std::copy(myints, myints + 7, myvector.begin());//接收copy的返回值

	std::cout << "vector容器值:";
	for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it){
		std::cout << ' ' << *it;
		//测试返回的地址
		if (it == rtnIt){
			std::cout << "<-此处是返回的地址" << " ";
		}
	}


	std::cout << '\n';
	std::cout << "----------------------------------------------------------------------------------"<<"\n";


	cout << "2.拷贝数组中:" << std::endl;
	int* MyArr = new int[10];

	int* rtnArr = std::copy(myints, myints + 7, MyArr);

	std::cout << "数组值:";
	for (int i = 0; i<10; i++){
		cout.width(8);//改变输出宽度
		std::cout << MyArr[i] << " ";
	}

	std::cout << '\n';
	std::cout << "数组地址:";
	for (int i = 0; i<10; i++){
		std::cout << &MyArr[i] << " ";
	}
	std::cout << '\n';

	std::cout << "返回的地址:" << rtnArr<<"\n";

	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

结果:注意红框处,它表明了copy()返回值的情况。
在这里插入图片描述

注意:使用std::copy时会报错,解决方法是:在预处理器加入(项目属性—>C/C++—>预处理—>预处理器定义):_SCL_SECURE_NO_WARNINGS。

copy_backward函数:

1. copy_backward函数的模板形式:

template <class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2 copy_backward (BidirectionalIterator1 first,
                                        BidirectionalIterator1 last,
                                        BidirectionalIterator2 result);

  • 1
  • 2
  • 3
  • 4
  • 5

2. 作用

相比copy()而言,是从后向前拷贝
在这里插入图片描述
其功能等价于:

template<class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward( BidirectionalIterator1 first,
                                      BidirectionalIterator1 last,
                                      BidirectionalIterator2 result )
{
  while (last!=first) 
	*(--result) = *(--last);
  return result;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3. 输入参数与返回值:

first、last:对应的是被拷贝序列的首地址和尾地址的后一个地址
result:目标内存的尾地址。
返回值:返回被拷贝序列的首地址,既first

4. 使用实例:

#include <iostream> 
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
	int myints[] = { 10, 20, 30, 40, 50, 60, 70 };//被拷贝的数据

	cout << "1.拷贝数组中:" << std::endl;
	int* MyArr = new int[10];

	int* rtnArr = std::copy_backward(myints, myints + 7, MyArr+10);

	std::cout << "数组值:";
	for (int i = 0; i<10; i++){
		cout.width(10);//改变输出宽度
		std::cout << MyArr[i] << " ";
	}

	std::cout << '\n';
	std::cout << "数组地址:";
	for (int i = 0; i<10; i++){
		cout.width(10);
		std::cout << &MyArr[i] << " ";
	}
	std::cout << '\n';

	std::cout << "返回的地址:" << rtnArr << "\n";

	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

打印结果:注意,红框中对比的是返回值
在这里插入图片描述

思考:

以上就是copy()和copy_backward()函数的内容了。
不过我们还需思考一个问题:如果目标地址的大小比被拷贝序列的小会发生什么情况呢?以copy_backward()为例:若int* MyArr = new int[10];的大小不是10,而改为6会发生什么?其打印的结果如下:
在这里插入图片描述
可以发现,尽管大小不够,但还是会自动向前推一个地址拷贝。这样是不好的,因为不按我们的意愿就修改了内存中的值。这一点是值得注意的。

参考:
链接: copy()函数.
链接:copy_backward()函数

本人水平有限,欢迎各位码友批评指正。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/707797
推荐阅读
相关标签
  

闽ICP备14008679号