cb37a-_c++_STL_算法_复制元素copy_copy_backward

 

cb37a-_c++_STL_算法_复制元素copy_copy_backward

copy(),同一个容器内部区间的拷贝,或者容器与容器之间的拷贝
copy_backward()//向后copy

注意:
1.没有copy_if算法,可以使用remove_copy_if()算法
2.复制过程中要逆转元素次序,使用reverse_copy()算法
3.把容器内所有元素赋值给另一个容器,要使用赋值操作符或者容器的assign()成员函数
4.复制工程中删除某些元素,使用remove_copy()和remove_copy_if()算法
5.复制中改变元素,使用transform()或者replace_copy()算法

STL算法-修改性算法
for_each()
copy()
copy_backward()
transform()
merge()
swap_ranges()
fill()
fill_n()

generate()
generate_n()
replace()
replace_if()
replace_copy()
replace_copy_if()

 

---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!

Program: D:\Users\txwtech\projects\cb37a\Debug\cb37a.exe
File: d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\vector
Line: 94

Expression: can't decrement vector iterator before begin

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

---------------------------
中止(A) 重试(R) 忽略(I)
---------------------------

copy_backward(ilist.begin(), ilist.end(), ivec.begin());
copy_backward的最后一个参数需要从最后往前面拷贝数据,所以应该是ivec.end().

Expression: can't decrement vector iterator before begin
意思就是指针溢出了,ivec.begin()之前的数据,就超出范围。
copy_backward是从ivec.end()的前一个数开始copy. ivec.end()是指向最后一个的下一个。

 

---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!

Program: D:\Users\txwtech\projects\cb37a\Debug\cb37a.exe
File: d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\list
Line: 187

Expression: cannot dereference end list iterator

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

---------------------------
中止(A) 重试(R) 忽略(I)
---------------------------
Expression: cannot dereference end list iterator,空的容器就会出问题。
使用list迭代器,先定义容量。
比如:list<int> ilist(20);//默认20个0,
copy(ivec.begin(), ivec.end(), ilist2.begin());
copy到ilist必须要保证ilist有足够的空间。
或者使用back_inserter
copy(ivec.begin(), ivec.end(), back_inserter(ilist));//这样就不用考虑容器容量问题

rror C2065: “ostream_iterator”: 未声明的标识符,需要包含:#include <iterator>

  1 /*cb37a-_c++_STL_算法_复制元素copy_copy_backward
  2 
  3 copy(),同一个容器内部区间的拷贝,或者容器与容器之间的拷贝
  4 copy_backward()//向后copy
  5 
  6 注意:
  7 1.没有copy_if算法,可以使用remove_copy_if()算法
  8 2.复制过程中要逆转元素次序,使用reverse_copy()算法
  9 3.把容器内所有元素赋值给另一个容器,要使用赋值操作符或者容器的assign()成员函数
 10 4.复制工程中删除某些元素,使用remove_copy()和remove_copy_if()算法
 11 5.复制中改变元素,使用transform()或者replace_copy()算法
 12 
 13 STL算法-修改性算法
 14 for_each()
 15 copy()
 16 copy_backward()
 17 transform()
 18 merge()
 19 swap_ranges()
 20 fill()
 21 fill_n()
 22 
 23 generate()
 24 generate_n()
 25 replace()
 26 replace_if()
 27 replace_copy()
 28 replace_copy_if()
 29 
 30 
 31 
 32 ---------------------------
 33 Microsoft Visual C++ Runtime Library
 34 ---------------------------
 35 Debug Assertion Failed!
 36 
 37 Program: D:\Users\txwtech\projects\cb37a\Debug\cb37a.exe
 38 File: d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\vector
 39 Line: 94
 40 
 41 Expression: can't decrement vector iterator before begin
 42 
 43 For information on how your program can cause an assertion
 44 failure, see the Visual C++ documentation on asserts.
 45 
 46 (Press Retry to debug the application)
 47 
 48 ---------------------------
 49 中止(A)   重试(R)   忽略(I)
 50 ---------------------------
 51 
 52 copy_backward(ilist.begin(), ilist.end(), ivec.begin());
 53 copy_backward的最后一个参数需要从最后往前面拷贝数据,所以应该是ivec.end().
 54 
 55 Expression: can't decrement vector iterator before begin
 56 意思就是指针溢出了,ivec.begin()之前的数据,就超出范围。 
 57 copy_backward是从ivec.end()的前一个数开始copy. ivec.end()是指向最后一个的下一个。
 58 
 59 
 60 
 61 ---------------------------
 62 Microsoft Visual C++ Runtime Library
 63 ---------------------------
 64 Debug Assertion Failed!
 65 
 66 Program: D:\Users\txwtech\projects\cb37a\Debug\cb37a.exe
 67 File: d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\list
 68 Line: 187
 69 
 70 Expression: cannot dereference end list iterator
 71 
 72 For information on how your program can cause an assertion
 73 failure, see the Visual C++ documentation on asserts.
 74 
 75 (Press Retry to debug the application)
 76 
 77 ---------------------------
 78 中止(A)   重试(R)   忽略(I)
 79 ---------------------------
 80 Expression: cannot dereference end list iterator,空的容器就会出问题。
 81 使用list迭代器,先定义容量。
 82 比如:list<int> ilist(20);//默认20个0,
 83 copy(ivec.begin(), ivec.end(), ilist2.begin());
 84 copy到ilist必须要保证ilist有足够的空间。
 85 或者使用back_inserter
 86 copy(ivec.begin(), ivec.end(), back_inserter(ilist));//这样就不用考虑容器容量问题
 87 
 88 rror C2065: “ostream_iterator”: 未声明的标识符,需要包含:#include <iterator>
 89 
 90 */
 91 
 92 #include <iostream>
 93 #include <algorithm>
 94 #include <vector>
 95 #include <list>
 96 #include <iterator>
 97 
 98 
 99 using namespace std;
100 
101 int main()
102 {
103     list<int> ilist;
104     for (int i = 0; i < 10; ++i)
105         ilist.push_back(i);
106     for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); ++iter)
107         cout << *iter << ' ';
108     cout << endl;
109 
110     vector<int> ivec(ilist.size()*2);
111     for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
112         cout << *iter << ' ';
113     cout << endl;
114 
115     copy(ilist.begin(), ilist.end(), ivec.begin());
116     cout << "copy后的数据" << endl;
117     for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
118         cout << *iter << ' ';
119     cout << endl;
120     //copy_backward的最后一个参数一定是ivec.end().
121     copy_backward(ilist.begin(), ilist.end(), ivec.end());
122     cout << "copy_backward后的数据" << endl;
123 
124     //
125     
126 
127     for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
128         cout << *iter << ' ';
129     cout << endl;
130 
131     //例子2
132     vector<int> ivec2;
133     list<int> ilist2(20);//默认分配空间,20个0;
134     for (int i = 1; i <= 9; ++i)
135         ivec2.push_back(i);
136     copy(ivec2.begin(), ivec2.end(), ilist2.begin());
137 
138     for (list<int>::iterator iter = ilist2.begin(); iter != ilist2.end(); ++iter)
139         cout << *iter << ' ';
140     cout << endl;
141 
142     cout << "copy拷贝到输出流里面" << endl;
143     copy(ivec2.begin(), ivec2.end(), ostream_iterator<int>(cout," "));
144     cout << endl;
145 
146     cout << "rbegin()逆向迭代器拷贝" << endl;
147     copy(ivec2.rbegin(), ivec2.rend(), ilist2.begin());
148     for (list<int>::iterator iter = ilist2.begin(); iter != ilist2.end(); ++iter)
149         cout << *iter << ' ';
150     cout << endl;
151 
152     return 0;
153 }

 

 1 /*cb37b_c++_
 2 
 3 */
 4 
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <vector>
 8 
 9 using namespace std;
10 
11 int main()
12 {
13 
14     vector<char> source(10, '.');
15     for (char c = 'a'; c <= 'f'; c++)
16     {
17         source.push_back(c);
18     }
19     source.insert(source.end(), 10, '.');
20 
21     for (vector<char>::iterator iter = source.begin(); iter != source.end(); ++iter)
22         cout << *iter << ' ';
23     cout << endl;
24 
25     vector<char> c1(source.begin(), source.end());
26     copy(c1.begin() + 10, c1.begin() + 16, c1.begin() + 7);
27     //自己对自己容器修改
28     /*
29     . . . . . . . . . . a b c d e f . . . . . . . . . .
30     . . . . . . . a b c d e f d e f . . . . . . . . . .
31     把a b c d e f从索引7开始拷贝,覆盖。
32     
33     */
34     for (vector<char>::iterator iter = c1.begin(); iter != c1.end(); ++iter)
35         cout << *iter << ' ';
36     cout << endl;
37 
38     vector<char> c2(source.begin(), source.end());
39     copy_backward(c2.begin() + 10, c2.begin() + 16, c2.begin() + 19);
40     //从f开始拷贝,从右边,最后面开始拷贝的。
41     for (vector<char>::iterator iter = c2.begin(); iter != c2.end(); ++iter)
42         cout << *iter << ' ';
43     cout << endl;
44 
45 
46 }

 

posted @ 2020-02-22 17:29  txwtech  阅读(547)  评论(0编辑  收藏  举报